Please see my code:
#include <stdint.h>
int main(int argc, char *argv[])
{
unsigned char s = 0xffU;
char ch = 0xff;
int val = 78;
((int8_t) + (78)); /*what does this mean*/
INT8_C(val); /*equivalent to above*/
signed char + 78; /*not allowed*/
return 0;
}
I find that the macro definition in <stdint.h> is:
#define INT8_C(val) ((int8_t) + (val))
What is the meaning or significance of this plus sign?
The snippet:
is an expression, one that takes the value
78, applies the unary+, then casts that to anint8_ttype, before throwing it away. It is no real different to the legal expressions:which also evaluate the expressions then throw away the result (though these will possibly be optimised out of existence if the compiler can tell that there are no side effects).
These “naked” expressions are perfectly valid in C and generally useful only when they have side effects, such as with
i++, which calculatesiand throws it away with the side effect being that it increments the value.The way you should be using that macro is more along the lines of:
The reason for the seemingly redundant unary
+operator can be found in the standard. Quoting C996.5.3.3 Unary arithmetic operators /1:And, in
6.2.5 Types, /18:In other words, the unary
+prevents you from using all the other data types in the macro, such as pointers, complex numbers or structures.And, finally, the reason your:
snippet doesn’t work is because it’s not the same thing. This one is starting to declare a variable of type
signed charbut chokes when it gets to the+since that’s not a legal variable name. To make it equivalent to your working snippet, you would use:which is the casting of the value
+78to typesigned char.And, as per C99
7.8.14 Macros for integer constants /2, you should also be careful with using non-constants in those macros, they’re not guaranteed to work:6.4.4.1simply specifies the various integer formats (decimal/octal/hex) with the various suffixes (U,UL,ULL,L,LLand the lower-case equivalents, depending on the type). The bottom line is that they have to be constants rather than variables.For example,
glibchas:which will allow your
INT8_Cmacro to work fine but the textINT64_C(val)would be pre-processed into eithervalLorvalLL, neither of which you would want.