Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#define strange

Tags:

c

#include<stdio.h>
#include<conio.h>
#define square(v) v*v
void main()
{
int p=3;
int s=square(++p);
printf("%d %d",s,p);
getch();
}

output 25 5 Why 16 4 is not coming as output? (Advance thanks)

like image 843
rohan lukose Avatar asked Sep 12 '26 08:09

rohan lukose


1 Answers

A macro is basically a text copy and paste. Therefore your ++ is being duplicated.

The macro is being expanded as:

s = ++p * ++p;

That's the danger of macros. (in this case, it also invokes undefined behavior)

like image 159
Mysticial Avatar answered Sep 13 '26 23:09

Mysticial