Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C preprocessor #if expression

I am a bit confused on the type of expression we can use with the #IF preprocessor in the C language. I tried the following code, and it isn't working. Please explain and provide examples for expressions that can be used with the preprocessor.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int c=1;

#if c==1
    #define check(a) (a==1)?a:5
    #define TABLE_SIZE 100
#endif

int main()
{
    int a = 0, b;
    printf("a = %d\n", a);
    b = check(a);
    printf("a = %d %d\n", a, TABLE_SIZE);
    system("PAUSE");
    return 0;
}
like image 856
Rohit Jain Avatar asked Jun 15 '11 18:06

Rohit Jain


People also ask

What is preprocessor in C programming?

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

What are the types of C preprocessor?

There are 4 Main Types of Preprocessor Directives:Macros. File Inclusion. Conditional Compilation. Other directives.

What is preprocessor in C with example?

The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your program before it is compiled. These transformations can be the inclusion of header files, macro expansions, etc. All preprocessing directives begin with a # symbol. For example, #define PI 3.14.

What is the use of a preprocessor?

In computer science, a preprocessor (or precompiler) is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers.


2 Answers

The preprocessor cannot use variables from the C program in expressions - it can only act on preprocessor macros. So when you try to use c in the preprocessor you don't get what you might expect.

However, you also don't get an error because when the preprocessor tries to evaluate an identifier that isn't defined as a macro, it treats the identifier as having a value of zero.

So when you hit this snippet:

#if c==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif

The c used by the preprocessor has nothing to do with the variable c from the C program. The preprocessor looks to see if there's a macro defined for c. Since there isn't, it evaluates the following expression:

#if 0==1

which is false of course.

Since you don't appear to use the variable c in your program, you can do the following to get behavior in line with what you're trying:

#define C 1

#if C==1
#define check(a) (a==1)?a:5
#define TABLE_SIZE 100
#endif

(Note that I also made the macro name uppercase in keeping with convention for macro names.)

like image 125
Michael Burr Avatar answered Oct 27 '22 15:10

Michael Burr


The preprocessor is run on the text, before any compilation is done. It doesn't know how to parse C. What you probably wanted instead of int c=1; was

#define C 1

and the test works the way you had it:

#if C == 1

The key here is that this is all defined before compile time. The preprocessor doesn't care about C variables, and certainly doesn't care what their values are.

Note that the convention is to have preprocessor macro names defined in ALL_CAPS.

like image 20
nmichaels Avatar answered Oct 27 '22 16:10

nmichaels