Priority of preprocessor C macros

I want to print last defined macros in my code: #define VAR 1234 #undef VAR #define VAR 1111 int main(){ printf("%d",VAR); while(1); } The output is 1111 If I want to define in another library. But when I redefine VAR in an external library outp

Iterate via struct by variable name

UPDATE: 6 months later, and I just came across this answer: Is it legal to index into a struct?: Answer by Slava . I think this is a MUCH better solution than any of the ones provided here, as there is absolutely no undefined behavior. Hopefully this

gcc -E does not expand C11 _Generic expressions

In a C11 library project I have a couple of macro functions that are exposed under a shared macro name using generics, like this: #define signum(operand) _Generic( (operand), \ unsigned long long: __signum_i4, unsigned long: __signum_i3, unsigned int

Define a C flag as elegantly as in assembly language

Flags handling in C feels cumbersome, compared to assembly. I am looking for a way to make the C code as readable as assembly. In Assembly: #define powerOn flagsByte,0 ... bsf powerOn ; Turn on the power bcf powerOn ; Turn off the power btfsc powerOn

Need help writing an EMPTY_OR macro

I'm trying to code a macro called EMPTY_OR, which will return the first argument, but if it's empty, it will return the second one. Here's what I have so far: #include <iostream> #define EMPTY_OR(x, y) ( (sizeof(#x) > sizeof("")) ? (x)

Comparison of #define giving an incorrect result

I have defined one variable which gives me output as below. iPhone 4 >> 4 iPhone 5 >> 5 iPad >> 999 Function used is as below. #define iPhone4Or5 [[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].siz

SWAP macro (t, x, y) exchanging two arguments of type t

So I am basically trying to make a SWAP(t,x,y) macro that exchanges two arguments of type t. I am trying to think of going around the problem when these two arguments are of the form v[i++] and w[f(x)] , i.e. SWAP(int, v[i++], w[f(x)]). The code belo

Preprocessor C using the closing hook of a parent macro

I have this code which works: #include <stdio.h> #define A(x) x B #define B(x) C(x, #define C(x,y) y x) int main( void ) { printf( A("1") ("2") "3" ); } It prints 132 (the point of the A macro is to swap the thing which

Preprocessor template classes and directives

A template class is a multitude of possible classes, so I was asking me: the preprocessor directives #ifndef MY_CLASS #define MY_CLASS template<typename T> class My_Class {}; #endif are necessaries?The include guards prevent repeated inclusion of th

Compiling Previously Preprocessed File Outputs Changes Results

I have a source file which I preprocess using the options -E and -P (using GCC 4.1.2 for a vxWorks-based embedded platform). All other options are the same as when I compile the file. These options are: -Wall -march=pentium -nostdinc -O0 -fno-builtin

#include an image in ansi C

I am writing a program that creates an image file. I want the "original" image to be hard-coded in the program's binary. I was thinking... char image[] = { #include"image.jpg" } But I need to somehow convert the image into a format tha

Is it legal to redefine a C ++ keyword?

In this article from Guru of the week, it is said: It is illegal to #define a reserved word. Is this true? I can't find anything in the norm, and I have already seen programmers redefining new, for instance.17.4.3.1.1 Macro names [lib.macro.names] 1

Preprocessor C: concatenate int to string

I'm trying to figure out how I can concatenate a #define'd int to a #define'd string using the C Preprocessor. My compiler is GCC 4.1 on CentOS 5. The solution should also work for MinGW. I'd like to append a version number onto a string, but the onl

#define with space

Is it possible to write define with spaces such as: #define replace to replacement here I want to replace "replace to" with "replacement here". EDIT: I want to test private members: I did write #define private public but it didn't work

Objective-C arithmetic guidelines

My issue is based around Objective-C directives, and subtracting them. Below are my directives. #define IS_IPAD FALSE #define RECT_WIDTH IS_IPAD ? 725 : 280 #define RECT_PADDING IS_IPAD ? 50 : 10 If I NSLog("@%d", RECT_WIDTH - RECT_PADDING), the

Replacing macros using compiler options

I need to be override certain macro definition by my header file. And I am not allowed to change source code. And I have to use gcc, but if anyone is aware of something similar on any other compiler then also it will help. Here is what I exactly need

What is the difference in practice between inline and #define?

As the title says; what's the difference in practice between the inline keyword and the #define preprocessor directive?#define is a preprocessor tool and has macro semantics. Consider this, if max(a,b) is a macro defined as #define max(a,b) ((a)>(b)?

Verification when two headers are included at the same time

I need to do an assertion based on two related macro preprocessor #define's declared in different header files... The codebase is huge and it would be nice if I could find a place to put the assertion where the two headers are already included, to av

The GCC dump preprocessor defines

Is there a way for gcc/g++ to dump its preprocessor defines from the command line? I mean things like __GNUC__, __STDC__, and so on.Yes, use -E -dM options instead of -c. Example (outputs them to stdout): gcc -dM -E - < /dev/null From the gcc manual:

Macro and function with the same name

I have the following code #define myfunc(a,b) myfunc(do_a(a), do_b(b)) void myfunc(int a, int b) { do_blah(a,b); } int main() { int x = 6, y = 7; myfunc(x,y); return 0; } I want the pre-processor to expand function myfunc only at calling. Required co

How do I know if an integer C variable is signed?

As an exercise, I'd like to write a macro which tells me if an integer variable is signed. This is what I have so far and I get the results I expect if I try this on a char variable with gcc -fsigned-char or -funsigned-char. #define ISVARSIGNED(V) (V