c-preprocessor

#define causes an “expected primary-expression” error

可紊 提交于 2019-12-05 09:48:05
#define N 10; int main() { int x; for (int i=0; i<N; i++) x = i; return 0; } Result of compiling this in g++: test-define.cpp: In function ‘int main()’: test-define.cpp:7:22: error: expected primary-expression before ‘;’ token test-define.cpp:7:22: error: expected ‘)’ before ‘;’ token test-define.cpp:7:24: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive] test-define.cpp:7:24: note: (if you use ‘-fpermissive’ G++ will accept your code) test-define.cpp:7:27: error: expected ‘;’ before ‘)’ token But it compiles fine when I change line 7 to for (int i=0; i<10; i++) . Why is

What is the # for when formatting using %s

我的未来我决定 提交于 2019-12-05 09:15:48
I came across this example of an assertion and was wondering what the # is for: #define ASSERT( x ) if ( !( x ) ) { \ int *p = NULL; \ DBGPRINTF("Assert failed: [%s]\r\n Halting.", #x); \ *p=1; \ } It is the "stringize" preprocessing operator. It takes the tokens passed as the argument to the macro parameter x and turns them into a string literal. #define ASSERT(x) #x ASSERT(a b c d) // is replaced by "a b c d" #x is the stringification directive #define Stringify(x) #x means Stringify(abc) will be substituted with "abc" as in #define initVarWithItsOwnName(x) const char* p = #x int main() {

g++ -E option output

萝らか妹 提交于 2019-12-05 08:23:58
Using this option I receive files after preprocessing. There are many lines like: # 91 "/usr/include/stdint.h" 3 4 What do the numbers mean? First I thought that #91 is the number of line where file is included, but that is not it. And about 3 4 I have no idea at all. According to the official documentation , the line is of the format: # linenum filename flags The linenum specifies that the following line originated in filename at that line number. Then there are four flags: 1 - Start of a new file 2 - Returning to a file 3 - System header file 4 - Treat as being wrapped in extern "C" So let's

Why only define a macro if it's not already defined?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 08:20:15
问题 All across our C code base, I see every macro defined the following way: #ifndef BEEPTRIM_PITCH_RATE_DEGPS #define BEEPTRIM_PITCH_RATE_DEGPS 0.2f #endif #ifndef BEEPTRIM_ROLL_RATE_DEGPS #define BEEPTRIM_ROLL_RATE_DEGPS 0.2f #endif #ifndef FORCETRIMRELEASE_HOLD_TIME_MS #define FORCETRIMRELEASE_HOLD_TIME_MS 1000.0f #endif #ifndef TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS #define TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS 50.0f #endif What is the rationale of doing these define checks instead of just

Token pasting in C

自闭症网瘾萝莉.ら 提交于 2019-12-05 08:17:52
After reading about VA_NARG I tried to implement function overloading depending on number of arguments in C using macros. Now the problem is: void hello1(char *s) { ... } void hello2(char *s, char *t) { ... } // PP_NARG(...) macro returns number of arguments :ref to link above // does not work #define hello(...) hello ## PP_NARG(__VA_ARGS__) int main(void) { hello("hi"); // call hello1("hi"); hello("foo","bar"); // call hello2("foo","bar"); return 0; } I've read this from C-faq. But still could not get it to work... Jens Gustedt This is because of the evaluation rules for macros. You would

How to get unique values at preprocessing across files

核能气质少年 提交于 2019-12-05 08:13:01
PROBLEM I need a way to generate unique values using a preprocessor directive. The aim is that each time the macro is called, it will have a unique integral identifier. But it should retain it's value across files. Kind of like a preprocessor counter for the number of times a call is made to the function. FURTHER INFO The macro I am using is: #define LOG_MSG(a) log_msg(?) 'a' is a string that the user wants to print. log_msg is a custom function used to print a message on the UART The '?' if the part I need help with. This macro would be defined at a single place only. During the preprocessing

Error when compiling Ruby 1.8.7 from source: math.c:37: error: missing binary operator before token “(”

早过忘川 提交于 2019-12-05 08:09:11
This is really odd: : josh@josh; wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7.tar.bz2 : josh@josh; tar xvjf ruby-1.8.7.tar.bz2 : josh@josh; cd ruby-1.8.7/ : josh@josh; CFLAGS='-O0 -g -Wall' ./configure --disable-pthread : josh@josh; make gcc -O0 -g -Wall -DRUBY_EXPORT -D_GNU_SOURCE=1 -I. -I. -c array.c [...] gcc -O0 -g -Wall -DRUBY_EXPORT -D_GNU_SOURCE=1 -I. -I. -c math.c math.c: In function ‘domain_check’: math.c:37: error: missing binary operator before token "(" make: *** [math.o] Error 1 Sure enough, math.c cannot be compiled: : josh@josh; gcc -O0 -g -Wall -DRUBY_EXPORT -D_GNU

#ifdef with multiple tokens, is this legal?

萝らか妹 提交于 2019-12-05 08:07:29
Today I came across some C++ code that contains an #ifdef clause like this: #ifdef DISABLE_UNTIL OTHER_CODE_IS_READY foo(); #endif Note the space between "DISABLE_UNTIL" and "OTHER_CODE_IS_READY". Essentially there are two tokens specified in the #ifdef line. My question is, is this legal C++ code? (g++ compiles it without any errors, and it apparently just ignores the second token). And if it is legal, should the second token have any effect? [C++11 16.1] , [C++11 16.5] and, incidentally, [C99 6.10.1/4] all say that this is invalid. if-group : # if constant-expression new-line group opt #

Is it possible to sort arrays using preprocessor?

廉价感情. 提交于 2019-12-05 07:54:26
I have a number of very long arrays. No run-time sort is possible. It is also time consuming to sort them manually. Moreover, new elements can be added in any order later, so I would like to sort them by value using C preprocessor or maybe there is any compilers flag (GCC)? For example: sometype S[] = { {somevals, "BOB", someothervals}, {somevals, "ALICE", someothervals}, {somevals, "TIM", someothervals}, } must be sorted so: sometype S[] = { {somevals, "ALICE", someothervals}, {somevals, "BOB", someothervals}, {somevals, "TIM", someothervals}, } SOLVED Ok, here is my solution: Manually copy

C macro : turn a number into a string

谁说胖子不能爱 提交于 2019-12-05 07:49:38
I have a table that defines symbols appearance on a 5x7 dot display. Something like: extern UINT8 symbols[][5] = { {0x0,0x0,0x0,0x0,0x0}, {0x0,0x0,0x5F,0x0,0x0}, {0x0,0x7,0x0,0x7,0x0}, {0x14,0x7F,0x14,0x7F,0x14}, // etc. The leading part of the table matches ASCII table, followed by a set of special symbols, e.g. an arrow, or a check-mark. To reference those I have a list of macros: #define SYMBOL_LEFT_ARROW 120 // 120 is the entry in the table #define SYMBOL_RIGHT_ARROW (SYMBOL_LEFT_ARROW+1) #define SYMBOL_UP_ARROW (SYMBOL_RIGHT_ARROW+1) Now I need to say something like (won't compile): const