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?
Has the Hulk always been able to talk?
Dangerous workplace travelling
How does summation index shifting work?
What to use instead of cling film to wrap pastry
When an imagined world resembles or has similarities with a famous world
How does the reduce() method work in Java 8?
Is there an age requirement to play in Adventurers League?
What do "Sech" and "Vich" mean in this sentence?
Change in "can't be countered" wording
How should I tell my manager I'm not paying for an optional after work event I'm not going to?
Adding command shortcuts to /bin
My first c++ game (snake console game)
Is any special diet a treatment of autism?
Should I simplify my writing in a foreign country?
Is “snitty” a popular American English term? What is its origin?
Proper use of Wikipedia code sample in open source projects
Voltage Balun 1:1
chromatic descent on minor chord
Why didn't this character get a funeral at the end of Avengers: Endgame?
Are the Night's Watch still required?
Checking if two expressions are related
Handling Null values (and equivalents) routinely in Python
Is Benjen dead?
Indentation Tex
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?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
135114
135114
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
64.7k852109
64.7k852109
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
87.4k74281
87.4k74281
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