Can the stringize macro be used here? - c

Using Pelles C I would like to show or log an unsigned char array.
Is it possible to use the stringize macro to show the whole array as hex values instead of iterating through the array with printf(%x)?

No. Stringification only encloses the specified parameters within double quotes.

Unfortunately, you can't. Stringification can convert your identifiers to strings, but not vice versa. Also, it can do nothing to the value a variable contains(neither read nor write), because values can be determined at run time, and macros expand at compile time.
In fact, I think preprocessor tricks such as # and ## should be avoided if possible, because they greatly reduce the readability. #include, #define and #if are enough for the most time.

Related

Printf with predefined macro concatenate with strings has format specifier in C

I'm programming in Windows and MSVC. There are two ways to write a DEBUG_PRINT statement as I know of:
printf(__FUNCTION__": Error code: %x\n", ErrorCode);
printf("%s: Error code: %x\n", __FUNCTION__, ErrorCode);
Is it okay to concatenate predefined macro with strings like this? I don't know if predefined macro like __FUNCTION__ or __LINE__ is a legit string literal. And intuitively, seems like a dangerous way to treat strings like this in C.
And what's the difference between these two? As I used /FAcs compiler option to output the code snippet to assembly, I really can't see much of a difference.
First of all __FUNCTION__ is not in the C standard, you should probably use __func__ instead (except that microsoft has decided to skip support for that in their C compiler).
Second __FUNCTION__/__func__ "macro" is not really a macro (or at least not normally - microsoft's compiler seem to behave differently), it behaves more like a local variable and therefore it isn't a candidate for the string concatenation. You should use string formatting instead (since that will ensure that your code will become more portable).
The __LINE__ macro (is a macro), but it doesn't work well with string concatenation directly since it doesn't expand to a string - it expands to a number (which by the way can be useful in other cases). However you can use the preprocessor to stringify it (the XSTR macro will first expand it's argument and then stringify the result while STR will not expand the it's argument before stringifying it):
#define STR(x) # x
#define XSTR(x) STR(x)
printf("I'm on line #" XSTR(__LINE__));
The __FILE__ macro (which is also a macro) does expand to a string literal which plays well together with string concatenation directly.
The reason you don't see any difference is that the compiler knows what printf does and can use that for optimization. It would figure out that it doesn't need to rely on printf code to expand the %s at runtime since it can do it at compile time.
The former will concatenate the function name in __FUNCTION__ with the format string at compile-time. The second will format it into the output at runtime.
This assumes it's a pre-defined macro (it's not standard). MSVC has __FUNCTION__ as a proper string literal, GCC does not.
__LINE__ is supported by GCC, and expands to a decimal integer, i.e. not a string.
For performance reasons, I would suggest always using the first way when possible, i.e. when the two strings are compile-time constant. There will be a price to pay, as usual: the string pool will be larger, since each use of the macro creates a unique format string. For desktop-class systems, this is probably neglible.
The difference is that in the 1st case the string literal gets concatenated with the format string during the compiler translation phases, while in the second case, it gets read in during run-time. So the first method is much faster.
If you know that the macro is a pre-defined string literal, I don't see anything wrong with the code.
That being said, I have no idea what __FUNCTION__ is. There is a standard C macro __func__ but it is not a string literal and should be treated like a const char array.
__LINE__ is a standard C macro that gives the source file line as an integer.
Thanks for answering. Looks like the first way is a legit literal string concatenate, it just does it in build-time, and it's faster, just space consuming.
I think I'll stick in the first way. Thanks again.

Do preprocessor macro definitions need to be in CAPS in a header file?

In the code I'm writing, I have been told to define a variable in a header file in the following way:
#define CLR_BLACK 0x0000
and since this is the only example I've been given, I was wondering whether all variables defined in a header file with the #define command need to be in caps. For example, would the following be valid?
#define videoBuffer (u16*)0x6000000
No. You can use any combination of alphanumeric characters and underscores. Don't start with a number.
However a variable name like videoBuffer would be difficult to distinguish from regular variables (without syntax coloring). That's why most people either use all caps for preprocessor macros or start them with a lower case k, like this: kMyPreprocessorMacro
EDIT: Those are not "global variables" by the way (as you tagged). They're preprocessor macros. Basically an automatic find and replace mechanism that is run at compile time.
No.
#define is a pre-processor macro. It replaces every occurrence of the first string after it with whatever comes after the string. The first string does not need to be in caps.
No, but it's a common and useful convention so if you're reading the code you can see what's a macro and what isn't. See C++ #ifndef for include files, why is all caps used for the header file?

Is it possible to convert a C string literal to uppercase using the preprocessor (macros)?

Ignoring that there are sometimes better non-macro ways to do this (I have good reasons, sadly), I need to write a big bunch of generic code using macros. Essentially a macro library that will generate a large number of functions for some pre-specified types.
To avoid breaking a large number of pre-existing unit tests, one of the things the library must do is, for every type, generate the name of that type in all caps for printing. E.g. a type "flag" must be printed as "FLAG".
I could just manually write out constants for each type, e.g.
#define flag_ALLCAPSNAME FLAG
but this is not ideal. I'd like to be able to do this programatically.
At present, I've hacked this together:
char capname_buf[BUFSIZ];
#define __MACRO_TO_UPPERCASE(arg) strcpy(capname_buf, arg); \
for(char *c=capname_buf;*c;c++)*c = (*c >= 'a' && *c <= 'z')? *c - 'a' + 'A': *c;
__MACRO_TO_UPPERCASE(#flag)
which does what I want to some extent (i.e. after this bit of code, capname_buf has "FLAG" as its contents), but I would prefer a solution that would allow me to define a string literal using macros instead, avoiding the need for this silly buffer.
I can't see how to do this, but perhaps I'm missing something obvious?
I have a variadic foreach loop macro written (like this one), but I can't mutate the contents of the string literal produced by #flag, and in any case, my loop macro would need a list of character pointers to iterate over (i.e. it iterates over lists, not over indices or the like).
Thoughts?
It is not possible in portable C99 to have a macro which converts a constant string to all uppercase letters (in particular because the notion of letter is related to character encoding. An UTF8 letter is not the same as an ASCII one).
However, you might consider some other solutions.
customize your editor to do that. For example, you could write some emacs code which would update each C source file as you require.
use some preprocessor on your C source code (perhaps a simple C code generator script which would emit a bunch of #define in some #include-d file).
use GCC extensions to have perhaps
#define TO_UPPERCASE_COUNTED(Str,Cnt)
#define TO_UPPERCASE(Str) TO_UPPERCASE_COUNTED(Str,__COUNT__) {( \
static char buf_##Cnt[sizeof(Str)+4]; \
char *str_##Cnt = Str; \
int ix_##Cnt = 0; \
for (; *str_##Cnt; str_##Cnt++, ix_##Cnt++) \
if (ix_##Cnt < sizeof(buf_##Cnt)-1) \
buf_##Cnt[ix_##Cnt] = toupper(*str_##Cnt); \
buf_##Cnt; )}
customize GCC, perhaps using MELT (a domain specific language to extend GCC), to provide your __builtin_capitalize_constant to do the job (edit: MELT is now an inactive project). Or code in C++ your own GCC plugin doing that (caveat, it will work with only one given GCC version).
It's not possible to do this entirely using the c preprocessor. The reason for this is that the preprocessor reads the input as (atomic) pp-tokens from which it composes the output. There's no construct for the preprocessor to decompose a pp-token into individual characters in any way (no one that would help you here anyway).
In your example when the preprocessor reads the string literal "flag" it's to the preprocessor basically an atomic chunk of text. It have constructs to conditionally remove such chunks or glue them together into larger chunks.
The only construct that allows you in some sense to decompose a pp-token is via some expressions. However these expressions only can work on arithmetic types which is why they won't help you here.
Your approach circumvents this problem by using C language constructs, ie you do the conversion at runtime. The only thing the preprocessor does then is to insert the C code to convert the string.

how can i declare variables via macros?

first of all, I'm using MS's Visual Studio and using C language.
Recently I need to declare variables with just one same statement which likes a macro.
However as you know, I can declare just one variable which have same name.
for example, this is not possible.
int iVar1;
int iVar1; // this is not possible.
so I thought about macros include __LINE__ , if I can use this predefined macro, I can declare lots of variables via just one macro statement.
But it was difficult to make.
I made macro like this.
#define MY_LINE_VARIABLE int g_iLine##__LINE__##Var = 0;
but after compile, i could get this variable named 'g_iLine_LINE_Var' instead of 'g_iLine123Var'
I want to know that is this possile, and how can i make it.
Furthermore, I need to use __FILE__ macro if possible. but this macro might be changed with string data. so I can not be sure.
Any advice will be helpful.
Thank you for your help in advance.
As #Chris Lutz has rightly said that, there might be a better way to accomplish what you want. Consider asking what you want to achieve.
But if you are just curious, this is the way to do:
#define var(z) int g_iLine##z##var = 0
#define decl(x) var(x)
#define MY_LINE_VARIABLE decl(__LINE__)
MY_LINE_VARIABLE;
MY_LINE_VARIABLE;
From this link :
After the preprocessor expands a macro name, the macro's definition
body is appended to the front of the remaining input, and the check
for macro calls continues. Therefore, the macro body can contain calls
to other macros.
So in your case :
MY_VARIABLE_LINE is converted to int g_iLine__LINE__Var;. But now __LINE__ is not a valid token anymore and is not treated as a predefined macro.
Aditya's code works like this:
MY_VARIABLE_LINE is converted to decl(__LINE__) which is converted to var(123) which is converted to int giLine123var = 0.
Edit: This is for GNU C

Can you capitalize a pasted token in a macro?

In a C macro, is it possible to capitalize a pasted-in token? For example, I currently have the following macro:
#define TEST(name, keyword) \
test_##name:
TEST_##keyword##_KEYWORD
I would invoke this as follows:
TEST(test1, TEST1)
which would yield the following:
test_test1:
TEST_TEST1_KEYWORD
Now, instead of having to type the same name twice (once with all lower case characters, and again with all upper case characters), is there any way that I could do either of the following, and either change the token into all uppercase letters or all lowercase letters?
TEST(test1) or TEST(TEST1)
Thanks,
Ryan
As far as I'm aware, the only operations that can be done on tokens in the C preprocessor (at least ISO/ANSI standard) is to replace, 'stringify' or concatenate them. I'm also unaware of any GCC or MSVC extensions that will let you do what you want.
However, people have been coming up with clever (or oddball) ways to do magical (or horrible) things with macros, so I wouldn't be surprised if someone surprises me.
You could do something like the following (untested, probably typos...)
#define NORMALIZE(TOK) NORMALIZE_ ## TOK
and then for each of the writings that may occur do
#define NORMALIZE_test1 test1
#define NORMALIZE_TEST1 test1
then use the NORMALIZE macro inside your real macro something like
#define TEST(name, keyword) \
test_ ## NORMALIZE(name): \
TEST_ ## NORMALIZE(keyword) ##_KEYWORD
(but maybe you'd have to do some intermediate helper macros until you
get all concatenations right)
It's not possible because the preprocessor works on an input stream of pp-token and has no construct that allows you to decompose these in a meaningful manner.
What the preprocessor has is constructs to replace pp-tokens with macro expansions, concatenate them, remove them (entirely) etc.
This means that your only hope for uppercasing is to start with individual characters and uppercase these and then glue everything together. Uppercasing individual characters is quite straight forward as you only have a finite set to work with. Glueing them together on the other hand would be possible, at least if you limit yourself to a fixed maximal length. You would end up in a macro that would be used like this:
TEST(t,e,s,t,1)

Resources