in Programming in C
381 views
0 votes
0 votes
#include<stdio.h>
#define A -B
#define B -C
#define C 5

int main()
{
  printf("The value of A is %dn", A); 
  return 0;
} 

what is the output?????

in Programming in C
by
381 views

2 Comments

$5.$
0
0

how it 5 bro??

i think after the preprocessing the statement becomes 

printf("The value of A is %dn", --5); 

so --5 means  4 .

can you explain how it is 5..........

0
0

1 Answer

0 votes
0 votes

Short answer The value of A is 5n

Possible Explanation:
#define macro would replace the respective constant with A, B and C with provided expression/value. So for our problem let's evaluate. (keep in mind, while replacing #define macro don't add/remove space or anything, put as it is)

So printf statement would become:
printf (" %d ", - -5 ) | See extra space it would be there while we were expending B.

Now you can see resultant value would be 5. 

Also, you can see macro processed input to compiler using $ gcc file.c -E.

Let me know your thoughts or if something I missed.

edited by

3 Comments

how space came (- -5)???
0
0

#define placed white spaces when it was processed! 
 

#define NUM 7

int main () {

 printf ("%d", NUM+NUM);

 return 0;

}

See the post-processed code you will observe —
int main () {
  printf ("%d", 7 +7);
  return 0;
}

White space by #define!

Now deduce why 7 +7); instead of 7 + 7 )

0
0
so #defne places white space when there is a operator  during processing...

is it right..?
0
0