C Programming - C Preprocessor - Discussion


Point out the error in the program

#define SI(p, n, r) float si; si=p*n*r/100;
int main()
    float p=2500, r=3.5;
    int n=3;
    SI(p, n, r);
    SI(1500, 2, 2.5);
    return 0;

[A]. 26250.00 7500.00
[B]. Nothing will print
[C]. Error: Multiple declaration of si
[D]. Garbage values

Answer: Option C


The macro #define SI(p, n, r) float si; si=p*n*r/100; contains the error. To remove this error, we have to modify this macro to

#define SI(p,n,r) p*n*r/100

Zeal said: (Dec 25, 2010)  
So, two lines must not be in macro declaration huh. Is it?

Wikiok said: (Apr 9, 2011)  
Or you can use a sub blobk { ...} Then "float si;" is not defined twice, but then the result must be stored in a living variable outside that sub block.

Akshay said: (Jun 7, 2011)  
1. C is case sensitive language
2. One can declare two variables viz: p, P
3. At the same time the macro does not distinguish between SI and si.

Shashi said: (Jul 28, 2012)  
@Akshay : you are wrong. SI and si are different in macro.
The error is redeclaration of flaot si.

SI(p, n, r);
SI(1500, 2, 2.5); means

float si; si=p*n*r/100;
float si; si=1500*2*2.5/100;

Here si is re-declared in the second line.you can check the authenticity by commenting the second expression;you will not get any error.

Ankit said: (Feb 21, 2014)  
Then why this program returns no error [http://indiabix.com/c-programming/c-preprocessor/032002] Program -7.

#define SWAP(a, b) int t; t=a, a=b, b=t;
int main()
int a=10, b=12;
SWAP(a, b);
printf("a = %d, b = %d\n", a, b);
return 0;

Praveena said: (Apr 2, 2014)  
Because it is called only once. Where as here SI is being called 2 times. That is the reason we get the error message that is the variable si is going to be declared twice. If SI is called thrice then it is going to be declared thrice which is an error redeclaration of si.

Azi said: (Sep 16, 2014)  
Macros in C are case insensitive.
So SI and si are same.

Check this link for confirmation: http://www.indiabix.com/c-programming/c-preprocessor/035002

Sarfaraz Ahmad said: (Dec 22, 2015)  
Just add braces.

#define SI(p, n, r) {float si; si=p*n*r/100;}

And scope of si will remain in braces, then again declaration will be allowed.

Shubh said: (Mar 21, 2020)  
@Shashi, yes correct explanation!

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.