c-preprocessor

Is there a way to both check a macro is defined and it equals a certain value at the same time

懵懂的女人 提交于 2019-12-29 06:39:29
问题 I regularly use object-like preprocessor macros as boolean flags in C code to turn on and off sections of code. For example #define DEBUG_PRINT 1 And then use it like #if(DEBUG_PRINT == 1) printf("%s", "Testing"); #endif However, it comes a problem if the header file that contains the #define is forgotten to be included in the source code. Since the macro is not declared, the preprocessor treats it as if it equals 0, and the #if statement never runs. When the header file is forgotten to be

Test for empty macro definition

帅比萌擦擦* 提交于 2019-12-29 05:47:06
问题 I've got a set of debug macros in tracing.hh. Whether it generates code and output is controlled by a macro flag in the real source code: // File: foo.cc #define TRACING 0 #include "tracing.hh" // Away we go . . . TRACEF("debug message"); The flag TRACING should have a value; I usually toggle between 0 and 1. Within tracing.h, #ifdef TRACING will tell me that tracing was defined. #if TRACING controls the definition of functional macros like TRACEF() But what if TRACING has no value? Then #if

Making something both a C identifier and a string?

旧巷老猫 提交于 2019-12-29 03:21:12
问题 Say you want to generate a matched list of identifiers and strings enum { NAME_ONE, NAME_TWO, NAME_THREE }; myFunction(NAME_ONE, "NAME_ONE"); myFunction(NAME_TWO, "NAME_TWO"); myFunction(NAME_THREE, "NAME_THREE"); ..without repeating yourself, and without auto-generating the code, using C/C++ macros Initial guess: You could add an #include file containing myDefine(NAME_ONE) myDefine(NAME_TWO) myDefine(NAME_THREE) Then use it twice like: #define myDefine(a) a, enum { #include "definitions" }

Can CPP preprocessing statement in Fortran be indented?

风流意气都作罢 提交于 2019-12-29 01:47:07
问题 I am fairly new to use Fortran preprocessing statement and have a question which is probably pretty native. Can Fortran preprocessing statement be indented? I tested using Gfortran 4.8.1 on Linux (openSUSE Leap) and it turned out I it can not be indented at all. The following code main.f90 works with gfortran -cpp main.f90 -o main : program main implicit none #ifdef DEBUG print *, "I am in debug mode" #endif print *, "hello world!" end program main But the following throws an error: program

How to know if __uint128_t is defined [duplicate]

亡梦爱人 提交于 2019-12-29 01:34:53
问题 This question already has answers here : Is there a 128 bit integer in gcc? (3 answers) Closed 8 months ago . We can use the preprocessor to know if unsigned long long is defined: #include <limits.h> #ifndef ULLONG_MAX typedef unsigned long t_mask; #else typedef unsigned long long t_mask; #endif But how to know if __uint128_t is defined? 回答1: You can try the following. I do not know how reliable this is, but it might be the easiest way. #ifdef __SIZEOF_INT128__ // do some fancy stuff here

Confused about C macro expansion and integer arithmetic [duplicate]

試著忘記壹切 提交于 2019-12-28 03:13:04
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: A riddle (in C) I have a couple of questions regarding the following snippet: #include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int array[] = {23,34,12,17,204,99,16}; int main() { int d; for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]); return 0; } Here the output of the code does not print the array elements as expected. But when I add a typecast of (int) the the macro

C circular dependency

十年热恋 提交于 2019-12-28 03:07:18
问题 I have this problem with circular dependency in C , I looked around the other questions about this topic but really couldn't find the answer. I have this first struct named vertex: #ifndef MapTest_vertex_h #define MapTest_vertex_h #include "edgelist.h" //includes edgelist because it's needed typedef struct { char* name; float x, y; edgelist* edges; } vertex; #endif The second struct is the edgelist which is included by the vertex. #ifndef edgelist_h #define edgelist_h #include "edge.h" /

Detecting Endianness

僤鯓⒐⒋嵵緔 提交于 2019-12-28 02:53:31
问题 I'm currently trying to create a C source code which properly handles I/O whatever the endianness of the target system. I've selected "little endian" as my I/O convention, which means that, for big endian CPU, I need to convert data while writing or reading. Conversion is not the issue. The problem I face is to detect endianness, preferably at compile time (since CPU do not change endianness in the middle of execution...). Up to now, I've been using this : #if __BYTE_ORDER__ == __ORDER_LITTLE

What is the possible use for “#define for if (false) {} else for”?

久未见 提交于 2019-12-27 23:39:43
问题 In another question, I just spotted this little pearl of C wisdom: #define for if (false) {} else for which caused MSVC to spit out "constant expression" warnings for a quite valid statement: for (int i = 0; i <= 10; i++) {...} I understand why MSVC is complaining because it expands to: if (false) {} else for (int i = 0; i <= 10; i++) {...} I just don't understand why the developers would use that little snippet. Anyone have an idea? 回答1: It's to fix a bug in old versions of Visual C++ (v6.0

How to make a variadic macro for std::cout?

旧时模样 提交于 2019-12-27 15:47:31
问题 How would I make a macro that took a variable amount of arguments, and prints its out using std::cout? Sorry if this is a noob question, couldn't find anything that clarified variadic macros after searching around for the answer. Conceptual Example: #include <iostream> #define LOG(...) std::cout << ... << ... << std::endl int main() { LOG("example","output","filler","text"); return 0; } would output: exampleoutputfillertext 回答1: You do not need preprocessor macros to do this. You can write it