in Programming in C
1,002 views
3 votes
3 votes
const int perplexed = 2;
#define perplexed 3
main()
{
#ifdef perplexed
#undef perplexed
#define perplexed 4
#endif 
printf("%d",perplexed);
}

a. 0 b. 2 c. 4  d. none of the above

in Programming in C
by
1.0k views

1 Answer

3 votes
3 votes
Best answer

Lets see how the code works line by line ==>


const int perplexed = 2;

Declares and defines a constant variable perplexed , hence, memory is allocated and stored with value 2 which can't be changed .


#define perplexed 3

Defines a macro , so whenever purplexed occurs , substitute with 3. It doesn't follow scope rules and no memory allocation is done for macro statements.


#ifdef perplexed

This command checks whether a macro purplexed id defined or not. If defined ( returns true ) , directs control to the next line of code.

#ifdef macro

//some code here

#endif

This block will be executed only, when ifdef returns true for macro, otherwise fails to compile the block.


#undef perplexed

This command removes the macro puplexed, if it exists and directs the control to the next line of code.


#define perplexed 4

This command redefines the macro purplexed with substitution value = 4.


printf("%d",perplexed);

becomes 

printf("%d",4 ); 

This will return 4.


#define perplexed 4 ==> If we remove this line , then scope of const int is used. Now printf will print 2.

selected by
by

Related questions