Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between preprocessor directives #if and #ifdef

What is the difference (if any) between the two following preprocessor control statements.

#if 

and

#ifdef 
like image 703
Konrad Avatar asked Sep 27 '10 10:09

Konrad


People also ask

What are the different preprocessor directives?

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

What is d difference between preprocessor directives and header files?

Preprocessor directives appear in source code. There are many different directives. One of them is #include , which is used to include a header file. Header files contain a collection of declarations, often for functions and types (and sometimes variables) found in a library.

What are preprocessor directives in C definition?

What Does Preprocessor Directive Mean? Preprocessor directives are lines included in a program that begin with the character #, which make them different from a typical source code text. They are invoked by the compiler to process some programs before compilation.

What is the difference between preprocessing and compilation?

Question1: What is Difference between Preprocessor and Compiler? Answer: Though, the preprocessor is the first to look at the source code file and performs several preprocessing operations before it's compiled by the compiler. Nevertheless, compiler sets the source code file, say “hello.


2 Answers

You can demonstrate the difference by doing:

#define FOO 0 #if FOO   // won't compile this #endif #ifdef FOO   // will compile this #endif 

#if checks for the value of the symbol, while #ifdef checks the existence of the symbol (regardless of its value).

like image 180
Greg Hewgill Avatar answered Sep 19 '22 00:09

Greg Hewgill


#ifdef FOO 

is a shortcut for:

#if defined(FOO) 

#if can also be used for other tests or for more complex preprocessor conditions.

#if defined(FOO) || defined(BAR) 
like image 38
ereOn Avatar answered Sep 18 '22 00:09

ereOn