c-preprocessor

How to catch undefined preprocessor macro with gcc?

喜夏-厌秋 提交于 2019-12-10 16:26:57
问题 I've been working on a piece of code that had an overlooked derp in it: #include<stdio.h> #include<stdlib.h> #include<limits.h> #define MAX_N_LENGTH /*function prototypes*/ int main(){ ... } It should be easy to spot with the context removed: #define MAX_N_LENGTH should have read #define MAX_N_LENGTH 9 . I have no idea where that trailing constant went. Since that macro was only used in one place in the form of char buf[ MAX_N_LENGTH + 1] , it was extremely difficult to track down and debug

Test if preprocessor symbol is defined inside macro

坚强是说给别人听的谎言 提交于 2019-12-10 16:18:26
问题 The usual way to test whether a preprocessor symbol is defined is using #ifdef . However, #ifdef cannot be used in a macro. What I need is a way to check in a macro if an argument of that macro is a defined preprocessor symbol. For example: #define TRACE(x,y) if(IS_DEFINED(x)){ std::cout << y; } Here, TRACE takes two arguments, the first x should be the name of a preprocessor symbol. If such symbol is defined, the second argument should be printed. The non-existing IS_DEFINED function/macro

How to properly split a C program in files and include then?

末鹿安然 提交于 2019-12-10 15:43:36
问题 I organized my program splitting every entity in its own file. Which is something like this. main.c #include "student.h" #include "subject.h" #include "classroom.h" #define PI 3.14 int sum(int a, int b); student.h typedef struct st student; student.c #include "student.h" subject.h typedef struct sb subject; subject.c #include "subject.h" classroom.h typedef struct cr classroom; classroom.c #include "classroom.h" My problem is, inside classroom I need student and subject . How should I include

do { } while(0) vs. if (1) { } in macros [duplicate]

大城市里の小女人 提交于 2019-12-10 14:53:27
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why are there sometimes meaningless do/while and if/else statements in C/C++ macros? When one needs to execute multiple statements within preprocessor macro, it's usually written like #define X(a) do { f1(a); f2(a); } while(0) so when this macro is used inside expressions like: if (...) X(a); it would not be messed up. The question is: wherever I've seen such expression, it's always do { ... } while(0); . Is

C++ preprocessor conditional parameter

[亡魂溺海] 提交于 2019-12-10 14:45:53
问题 Please note C++03! any C++11 solutions are not good for me, but do post them just for knowledge sake. I know the preprocessor can do things like: #define FOO 4 #if FOO == 4 cout<<"hi"<<endl; #endif What I need is: #define BAR(X)\ #if X == 4\ cout<<"hi"<<endl;\ #endif main.cpp BAR(4) I don't see why all the needed information wouldn't be available in preprocessor time. So, Please tell me how to achieve this kind of behavior. edit 1: A normal if condition won't work for my case, because I also

C Macro for minimum of two numbers

痴心易碎 提交于 2019-12-10 14:43:28
问题 I want to make a simple macro with #define for returning the smaller of two numbers. How can i do this in C ? Suggest some ideas, and see if you can make it more obfuscated too. 回答1: For slightly obfuscated, try this: #define MIN(a,b) ((((a)-(b))&0x80000000) >> 31)? (a) : (b) Basically, it subtracts them, and looks at the sign-bit as a 1-or-0. If the subtraction results in a negative number, the first parameter is smaller. 回答2: Typically: #define min(a, b) (((a) < (b)) ? (a) : (b)) Be warned

Get a list of #define variables

为君一笑 提交于 2019-12-10 14:33:04
问题 Even though it should be impossible due to #define being a pre-processor directive I'd like to ask: Is it possible to get a list of the #define 'd variables within the actual program? Respectively, a list of conditional compilation symbols, defined within the project's properties. Why would I need that? I'm managing extensions by using symbols. I'm trying to get a List of them to add them in my about window like Enabled Extensions: CUSTOMER1_ABC_EXTENSION CUSTOMER2_XYZ_EXTENSION Without

Are comments always processed before the preprocessor? [duplicate]

感情迁移 提交于 2019-12-10 14:26:29
问题 This question already has answers here : In which step of compilation are comments removed? (2 answers) Closed 2 years ago . /* #define FOO */ #ifdef FOO #define BAR "pirate" #else #define BAR "ninja" #endif int main() { printf(BAR); getchar(); } In this code FOO is not defined (Visual Studio 2008). I assume that comments are processed first, then preprocessor, and then code. Are comments always processed before the preprocessor? Is this part of a standard? 回答1: I assume that comments are

Append items to an array with a macro, in C

自作多情 提交于 2019-12-10 14:14:50
问题 I have an array ( C language) that should be initialized at compile time. For example: DECLARE_CMD(f1, arg); DECLARE_CMD(f2, arg); The DECLARE_CMD is called from multiple files. I want this to be preprocessed in. my_func_type my_funcs [] = { &f1, &f2 } It is possible, with a macro, to append items to an static array? I am using C99 (with GNU extensions) on gcc4. 回答1: NOTE: in your question there are semicolons at the end of every line. This will seriously interfere with any attempt to use

How are chained macros resolved in C?

主宰稳场 提交于 2019-12-10 13:59:23
问题 If I want to use preprocessor #define statements for easy definition and calculation of constants and common functions and take advantage of less RAM overhead (as opposed to using const values). However, I am unsure as to how they are resolved if many macros are used together. I'm designing my own DateTime code handling, similar to linux timestamps but for a game with tick updates that represent 1/60th of a second. I would prefer to declare values chained, but wonder if hard coded valued