How to pad strings using preprocessor macrosWhat is the difference between String and string in C#?How do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptImprove INSERT-per-second performance of SQLite?How to check whether a string contains a substring in JavaScript?Does Python have a string 'contains' substring method?How do I convert a String to an int in Java?Why is char[] preferred over String for passwords?
A workplace installs custom certificates on personal devices, can this be used to decrypt HTTPS traffic?
Have I saved too much for retirement so far?
Is there an Impartial Brexit Deal comparison site?
Lightning Web Component - do I need to track changes for every single input field in a form
Resetting two CD4017 counters simultaneously, only one resets
How to color a zone in Tikz
What is the term when two people sing in harmony, but they aren't singing the same notes?
Word describing multiple paths to the same abstract outcome
Why are all the doors on Ferenginar (the Ferengi home world) far shorter than the average Ferengi?
Simple recursive Sudoku solver
Can a Bard use an arcane focus?
The most efficient algorithm to find all possible integer pairs which sum to a given integer
What was required to accept "troll"?
A known event to a history junkie
Should a half Jewish man be discouraged from marrying a Jewess?
Female=gender counterpart?
Can I use my Chinese passport to enter China after I acquired another citizenship?
Is infinity mathematically observable?
Do all polymers contain either carbon or silicon?
Can I create an upright 7-foot × 5-foot wall with the Minor Illusion spell?
Was the picture area of a CRT a parallelogram (instead of a true rectangle)?
Freedom of speech and where it applies
Could solar power be utilized and substitute coal in the 19th century?
Is there an wasy way to program in Tikz something like the one in the image?
How to pad strings using preprocessor macros
What is the difference between String and string in C#?How do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptImprove INSERT-per-second performance of SQLite?How to check whether a string contains a substring in JavaScript?Does Python have a string 'contains' substring method?How do I convert a String to an int in Java?Why is char[] preferred over String for passwords?
Is it possible to pad a string with spaces (or any character) using only preprocessor macros? If so, how?
Example:
#define SOME_STR "v1.1"
#define STR_PAD(str, len) // <-- padding defined string
#define SOME_STR_PADDED STR_PAD(SOME_STR, 10) // evaluates to "v1.1 "
I know that there are simple solutions during runtime, but my question is how to pad a string during compile time.
c string c-preprocessor
add a comment |
Is it possible to pad a string with spaces (or any character) using only preprocessor macros? If so, how?
Example:
#define SOME_STR "v1.1"
#define STR_PAD(str, len) // <-- padding defined string
#define SOME_STR_PADDED STR_PAD(SOME_STR, 10) // evaluates to "v1.1 "
I know that there are simple solutions during runtime, but my question is how to pad a string during compile time.
c string c-preprocessor
Than may be reword you question so that you are asking the real question? Quote from you:...my question is if it is possible to pad...
and my answer is - Yes, it is possible.
– SergeyA
Mar 21 at 14:47
I asked if it is possible (a) and if it is possible, how to do it (b). English is not my native language, but I thought that it's getting across. I'll reword the question that everyone will understand. Thank you.
– MemAllox
Mar 21 at 14:49
Very interesting question! It does not seem possible in the general case where bothstr
andlen
are unknown, and also whenstr
alone is unknown. If both the length ofstr
is known andlen
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.
– chqrlie
Mar 21 at 15:17
Though it's already been answered as such, I want to make a distinction to make something clear. The preprocessor cannot determine the length of a string constant, so it cannot do what is asked. But there's a lot more that goes on during compilation; asking if the preprocessor can pad versus can you pad at compile time are two different questions (with different answers).
– H Walters
Mar 21 at 16:06
add a comment |
Is it possible to pad a string with spaces (or any character) using only preprocessor macros? If so, how?
Example:
#define SOME_STR "v1.1"
#define STR_PAD(str, len) // <-- padding defined string
#define SOME_STR_PADDED STR_PAD(SOME_STR, 10) // evaluates to "v1.1 "
I know that there are simple solutions during runtime, but my question is how to pad a string during compile time.
c string c-preprocessor
Is it possible to pad a string with spaces (or any character) using only preprocessor macros? If so, how?
Example:
#define SOME_STR "v1.1"
#define STR_PAD(str, len) // <-- padding defined string
#define SOME_STR_PADDED STR_PAD(SOME_STR, 10) // evaluates to "v1.1 "
I know that there are simple solutions during runtime, but my question is how to pad a string during compile time.
c string c-preprocessor
c string c-preprocessor
edited Mar 21 at 14:52
MemAllox
asked Mar 21 at 14:45
MemAlloxMemAllox
133114
133114
Than may be reword you question so that you are asking the real question? Quote from you:...my question is if it is possible to pad...
and my answer is - Yes, it is possible.
– SergeyA
Mar 21 at 14:47
I asked if it is possible (a) and if it is possible, how to do it (b). English is not my native language, but I thought that it's getting across. I'll reword the question that everyone will understand. Thank you.
– MemAllox
Mar 21 at 14:49
Very interesting question! It does not seem possible in the general case where bothstr
andlen
are unknown, and also whenstr
alone is unknown. If both the length ofstr
is known andlen
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.
– chqrlie
Mar 21 at 15:17
Though it's already been answered as such, I want to make a distinction to make something clear. The preprocessor cannot determine the length of a string constant, so it cannot do what is asked. But there's a lot more that goes on during compilation; asking if the preprocessor can pad versus can you pad at compile time are two different questions (with different answers).
– H Walters
Mar 21 at 16:06
add a comment |
Than may be reword you question so that you are asking the real question? Quote from you:...my question is if it is possible to pad...
and my answer is - Yes, it is possible.
– SergeyA
Mar 21 at 14:47
I asked if it is possible (a) and if it is possible, how to do it (b). English is not my native language, but I thought that it's getting across. I'll reword the question that everyone will understand. Thank you.
– MemAllox
Mar 21 at 14:49
Very interesting question! It does not seem possible in the general case where bothstr
andlen
are unknown, and also whenstr
alone is unknown. If both the length ofstr
is known andlen
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.
– chqrlie
Mar 21 at 15:17
Though it's already been answered as such, I want to make a distinction to make something clear. The preprocessor cannot determine the length of a string constant, so it cannot do what is asked. But there's a lot more that goes on during compilation; asking if the preprocessor can pad versus can you pad at compile time are two different questions (with different answers).
– H Walters
Mar 21 at 16:06
Than may be reword you question so that you are asking the real question? Quote from you:
...my question is if it is possible to pad...
and my answer is - Yes, it is possible.– SergeyA
Mar 21 at 14:47
Than may be reword you question so that you are asking the real question? Quote from you:
...my question is if it is possible to pad...
and my answer is - Yes, it is possible.– SergeyA
Mar 21 at 14:47
I asked if it is possible (a) and if it is possible, how to do it (b). English is not my native language, but I thought that it's getting across. I'll reword the question that everyone will understand. Thank you.
– MemAllox
Mar 21 at 14:49
I asked if it is possible (a) and if it is possible, how to do it (b). English is not my native language, but I thought that it's getting across. I'll reword the question that everyone will understand. Thank you.
– MemAllox
Mar 21 at 14:49
Very interesting question! It does not seem possible in the general case where both
str
and len
are unknown, and also when str
alone is unknown. If both the length of str
is known and len
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.– chqrlie
Mar 21 at 15:17
Very interesting question! It does not seem possible in the general case where both
str
and len
are unknown, and also when str
alone is unknown. If both the length of str
is known and len
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.– chqrlie
Mar 21 at 15:17
Though it's already been answered as such, I want to make a distinction to make something clear. The preprocessor cannot determine the length of a string constant, so it cannot do what is asked. But there's a lot more that goes on during compilation; asking if the preprocessor can pad versus can you pad at compile time are two different questions (with different answers).
– H Walters
Mar 21 at 16:06
Though it's already been answered as such, I want to make a distinction to make something clear. The preprocessor cannot determine the length of a string constant, so it cannot do what is asked. But there's a lot more that goes on during compilation; asking if the preprocessor can pad versus can you pad at compile time are two different questions (with different answers).
– H Walters
Mar 21 at 16:06
add a comment |
2 Answers
2
active
oldest
votes
Very interesting question! It does not seem possible in the general case where both str
and len
are unknown, and also when str
alone is unknown. If both the length of str
is known and len
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.
Here is an illustration:
// compile time padding: str must be a string constant and len <= 4 + strlen(str)
#define STR_PAD(str, len) (((len) + 1 <= sizeof str) ? str :
((len) == sizeof str) ? str " " :
((len) == sizeof str + 1) ? str " " :
((len) == sizeof str + 2) ? str " " :
str " ")
I think, you mean "len is < 4 + sizeof str" in your comment
– Ctx
Mar 21 at 15:26
1
@Ctx: good catch. There was also an off by one error due tosizeof(str) == strlen(str) + 1
– chqrlie
Mar 21 at 15:47
That's a good idea! I didn't think of the ternary expression.
– MemAllox
Mar 21 at 16:47
add a comment |
In the first place, the preprocessor can convert tokens to strings, but it cannot modify existing strings at all. What it can do is place string literals next to each other ("foo" "bar"
), which is translated at compile time -- albeit not formally by the preprocessor -- equivalently to the concatenated string ("foobar"
).
You could, therefore do something like this:
#define VERSION "v1.1"
#define APPEND_10_SPACES(s) s " "
printf("%s", APPEND_10_SPACES(VERSION));
With considerably more difficulty, you could arrange to append a number of spaces specified by a macro argument, too. Your web searches should turn up several results pertaining to making the preprocessor mimic iteration.
What the preprocessor absolutely cannot do, however, is determine or use the length of a string literal, so as to allow you to do something equivalent to padding a literal to a specific length. If you need that, then you could potentially rely on a workaround along these lines:
char padded[] = APPEND_10_SPACES(VERSION);
padded[10] = '';
You do not that way get your desired string as a literal, but you do get it, at the cost of allocating up to 10 more bytes than you need, and truncating the original string if it was in fact longer than 10 characters.
Thank you for the clarification. Of course you are right, at compile time there is more going on than running the preprocessor. In fact I want to append a varying number of spaces corresponding to @chqrlie's answer. As you mentioned, you could probably write a wrapper so to mimic a loop.
– MemAllox
Mar 21 at 16:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55283084%2fhow-to-pad-strings-using-preprocessor-macros%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Very interesting question! It does not seem possible in the general case where both str
and len
are unknown, and also when str
alone is unknown. If both the length of str
is known and len
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.
Here is an illustration:
// compile time padding: str must be a string constant and len <= 4 + strlen(str)
#define STR_PAD(str, len) (((len) + 1 <= sizeof str) ? str :
((len) == sizeof str) ? str " " :
((len) == sizeof str + 1) ? str " " :
((len) == sizeof str + 2) ? str " " :
str " ")
I think, you mean "len is < 4 + sizeof str" in your comment
– Ctx
Mar 21 at 15:26
1
@Ctx: good catch. There was also an off by one error due tosizeof(str) == strlen(str) + 1
– chqrlie
Mar 21 at 15:47
That's a good idea! I didn't think of the ternary expression.
– MemAllox
Mar 21 at 16:47
add a comment |
Very interesting question! It does not seem possible in the general case where both str
and len
are unknown, and also when str
alone is unknown. If both the length of str
is known and len
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.
Here is an illustration:
// compile time padding: str must be a string constant and len <= 4 + strlen(str)
#define STR_PAD(str, len) (((len) + 1 <= sizeof str) ? str :
((len) == sizeof str) ? str " " :
((len) == sizeof str + 1) ? str " " :
((len) == sizeof str + 2) ? str " " :
str " ")
I think, you mean "len is < 4 + sizeof str" in your comment
– Ctx
Mar 21 at 15:26
1
@Ctx: good catch. There was also an off by one error due tosizeof(str) == strlen(str) + 1
– chqrlie
Mar 21 at 15:47
That's a good idea! I didn't think of the ternary expression.
– MemAllox
Mar 21 at 16:47
add a comment |
Very interesting question! It does not seem possible in the general case where both str
and len
are unknown, and also when str
alone is unknown. If both the length of str
is known and len
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.
Here is an illustration:
// compile time padding: str must be a string constant and len <= 4 + strlen(str)
#define STR_PAD(str, len) (((len) + 1 <= sizeof str) ? str :
((len) == sizeof str) ? str " " :
((len) == sizeof str + 1) ? str " " :
((len) == sizeof str + 2) ? str " " :
str " ")
Very interesting question! It does not seem possible in the general case where both str
and len
are unknown, and also when str
alone is unknown. If both the length of str
is known and len
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.
Here is an illustration:
// compile time padding: str must be a string constant and len <= 4 + strlen(str)
#define STR_PAD(str, len) (((len) + 1 <= sizeof str) ? str :
((len) == sizeof str) ? str " " :
((len) == sizeof str + 1) ? str " " :
((len) == sizeof str + 2) ? str " " :
str " ")
edited Mar 23 at 1:05
answered Mar 21 at 15:22
chqrliechqrlie
61.9k848105
61.9k848105
I think, you mean "len is < 4 + sizeof str" in your comment
– Ctx
Mar 21 at 15:26
1
@Ctx: good catch. There was also an off by one error due tosizeof(str) == strlen(str) + 1
– chqrlie
Mar 21 at 15:47
That's a good idea! I didn't think of the ternary expression.
– MemAllox
Mar 21 at 16:47
add a comment |
I think, you mean "len is < 4 + sizeof str" in your comment
– Ctx
Mar 21 at 15:26
1
@Ctx: good catch. There was also an off by one error due tosizeof(str) == strlen(str) + 1
– chqrlie
Mar 21 at 15:47
That's a good idea! I didn't think of the ternary expression.
– MemAllox
Mar 21 at 16:47
I think, you mean "len is < 4 + sizeof str" in your comment
– Ctx
Mar 21 at 15:26
I think, you mean "len is < 4 + sizeof str" in your comment
– Ctx
Mar 21 at 15:26
1
1
@Ctx: good catch. There was also an off by one error due to
sizeof(str) == strlen(str) + 1
– chqrlie
Mar 21 at 15:47
@Ctx: good catch. There was also an off by one error due to
sizeof(str) == strlen(str) + 1
– chqrlie
Mar 21 at 15:47
That's a good idea! I didn't think of the ternary expression.
– MemAllox
Mar 21 at 16:47
That's a good idea! I didn't think of the ternary expression.
– MemAllox
Mar 21 at 16:47
add a comment |
In the first place, the preprocessor can convert tokens to strings, but it cannot modify existing strings at all. What it can do is place string literals next to each other ("foo" "bar"
), which is translated at compile time -- albeit not formally by the preprocessor -- equivalently to the concatenated string ("foobar"
).
You could, therefore do something like this:
#define VERSION "v1.1"
#define APPEND_10_SPACES(s) s " "
printf("%s", APPEND_10_SPACES(VERSION));
With considerably more difficulty, you could arrange to append a number of spaces specified by a macro argument, too. Your web searches should turn up several results pertaining to making the preprocessor mimic iteration.
What the preprocessor absolutely cannot do, however, is determine or use the length of a string literal, so as to allow you to do something equivalent to padding a literal to a specific length. If you need that, then you could potentially rely on a workaround along these lines:
char padded[] = APPEND_10_SPACES(VERSION);
padded[10] = '';
You do not that way get your desired string as a literal, but you do get it, at the cost of allocating up to 10 more bytes than you need, and truncating the original string if it was in fact longer than 10 characters.
Thank you for the clarification. Of course you are right, at compile time there is more going on than running the preprocessor. In fact I want to append a varying number of spaces corresponding to @chqrlie's answer. As you mentioned, you could probably write a wrapper so to mimic a loop.
– MemAllox
Mar 21 at 16:45
add a comment |
In the first place, the preprocessor can convert tokens to strings, but it cannot modify existing strings at all. What it can do is place string literals next to each other ("foo" "bar"
), which is translated at compile time -- albeit not formally by the preprocessor -- equivalently to the concatenated string ("foobar"
).
You could, therefore do something like this:
#define VERSION "v1.1"
#define APPEND_10_SPACES(s) s " "
printf("%s", APPEND_10_SPACES(VERSION));
With considerably more difficulty, you could arrange to append a number of spaces specified by a macro argument, too. Your web searches should turn up several results pertaining to making the preprocessor mimic iteration.
What the preprocessor absolutely cannot do, however, is determine or use the length of a string literal, so as to allow you to do something equivalent to padding a literal to a specific length. If you need that, then you could potentially rely on a workaround along these lines:
char padded[] = APPEND_10_SPACES(VERSION);
padded[10] = '';
You do not that way get your desired string as a literal, but you do get it, at the cost of allocating up to 10 more bytes than you need, and truncating the original string if it was in fact longer than 10 characters.
Thank you for the clarification. Of course you are right, at compile time there is more going on than running the preprocessor. In fact I want to append a varying number of spaces corresponding to @chqrlie's answer. As you mentioned, you could probably write a wrapper so to mimic a loop.
– MemAllox
Mar 21 at 16:45
add a comment |
In the first place, the preprocessor can convert tokens to strings, but it cannot modify existing strings at all. What it can do is place string literals next to each other ("foo" "bar"
), which is translated at compile time -- albeit not formally by the preprocessor -- equivalently to the concatenated string ("foobar"
).
You could, therefore do something like this:
#define VERSION "v1.1"
#define APPEND_10_SPACES(s) s " "
printf("%s", APPEND_10_SPACES(VERSION));
With considerably more difficulty, you could arrange to append a number of spaces specified by a macro argument, too. Your web searches should turn up several results pertaining to making the preprocessor mimic iteration.
What the preprocessor absolutely cannot do, however, is determine or use the length of a string literal, so as to allow you to do something equivalent to padding a literal to a specific length. If you need that, then you could potentially rely on a workaround along these lines:
char padded[] = APPEND_10_SPACES(VERSION);
padded[10] = '';
You do not that way get your desired string as a literal, but you do get it, at the cost of allocating up to 10 more bytes than you need, and truncating the original string if it was in fact longer than 10 characters.
In the first place, the preprocessor can convert tokens to strings, but it cannot modify existing strings at all. What it can do is place string literals next to each other ("foo" "bar"
), which is translated at compile time -- albeit not formally by the preprocessor -- equivalently to the concatenated string ("foobar"
).
You could, therefore do something like this:
#define VERSION "v1.1"
#define APPEND_10_SPACES(s) s " "
printf("%s", APPEND_10_SPACES(VERSION));
With considerably more difficulty, you could arrange to append a number of spaces specified by a macro argument, too. Your web searches should turn up several results pertaining to making the preprocessor mimic iteration.
What the preprocessor absolutely cannot do, however, is determine or use the length of a string literal, so as to allow you to do something equivalent to padding a literal to a specific length. If you need that, then you could potentially rely on a workaround along these lines:
char padded[] = APPEND_10_SPACES(VERSION);
padded[10] = '';
You do not that way get your desired string as a literal, but you do get it, at the cost of allocating up to 10 more bytes than you need, and truncating the original string if it was in fact longer than 10 characters.
edited Mar 21 at 15:44
answered Mar 21 at 15:15
John BollingerJohn Bollinger
84.3k74279
84.3k74279
Thank you for the clarification. Of course you are right, at compile time there is more going on than running the preprocessor. In fact I want to append a varying number of spaces corresponding to @chqrlie's answer. As you mentioned, you could probably write a wrapper so to mimic a loop.
– MemAllox
Mar 21 at 16:45
add a comment |
Thank you for the clarification. Of course you are right, at compile time there is more going on than running the preprocessor. In fact I want to append a varying number of spaces corresponding to @chqrlie's answer. As you mentioned, you could probably write a wrapper so to mimic a loop.
– MemAllox
Mar 21 at 16:45
Thank you for the clarification. Of course you are right, at compile time there is more going on than running the preprocessor. In fact I want to append a varying number of spaces corresponding to @chqrlie's answer. As you mentioned, you could probably write a wrapper so to mimic a loop.
– MemAllox
Mar 21 at 16:45
Thank you for the clarification. Of course you are right, at compile time there is more going on than running the preprocessor. In fact I want to append a varying number of spaces corresponding to @chqrlie's answer. As you mentioned, you could probably write a wrapper so to mimic a loop.
– MemAllox
Mar 21 at 16:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55283084%2fhow-to-pad-strings-using-preprocessor-macros%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Than may be reword you question so that you are asking the real question? Quote from you:
...my question is if it is possible to pad...
and my answer is - Yes, it is possible.– SergeyA
Mar 21 at 14:47
I asked if it is possible (a) and if it is possible, how to do it (b). English is not my native language, but I thought that it's getting across. I'll reword the question that everyone will understand. Thank you.
– MemAllox
Mar 21 at 14:49
Very interesting question! It does not seem possible in the general case where both
str
andlen
are unknown, and also whenstr
alone is unknown. If both the length ofstr
is known andlen
is bounded by some reasonable fixed value, one could generate a compound ternary expression that compiles to a single string constant.– chqrlie
Mar 21 at 15:17
Though it's already been answered as such, I want to make a distinction to make something clear. The preprocessor cannot determine the length of a string constant, so it cannot do what is asked. But there's a lot more that goes on during compilation; asking if the preprocessor can pad versus can you pad at compile time are two different questions (with different answers).
– H Walters
Mar 21 at 16:06