#define fst 1
int main()
     #if( fst ==1 )
              printf("Dummy FST\n");
 return 0;

if i replace the statement in line 4 with #if fst also the code is working.

can any one tell me what is the correct way , advantages and disadvantages.

8 Years
Discussion Span
Last Post by Dave Sinkula

You forgot the #endif The correct way of doing what? What are you trying to do?

i am trying to use conditional macro , and want to check a defined value as in line number 4.

i used #if ( fst == 1) then the code is working and if i replace the statement #if ( fst == 1) with #if fst then also the code is working .

i just want to know which one is correct and good to use.


You should be using

#ifdef fst

in your code check instead of

#if (fst == 1)
#define fst 1

The above line is an indicator to replace fst with 1 whenever seen.

So (fst == 1) translates to (1==1)

The #define is used for many purposes like ,
1)For defining constants.
2)For conditional excution of code.
3)For making sure that multiple inclusions of header files are avoided.

You can also write macro code using this directive.

Edited by thomas_naveen: typo


I tend to use this type of construct:

#if defined MACRO && MACRO > 0

because you really shouldn't be doing this

#if MACRO > 0

if MACRO is not defined.

And just doing

#ifdef MACRO


#if defined MACRO

would be true even if MACRO were defined as 0.

Edited by Dave Sinkula: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.