C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Point Out Errors (Q.No. 1)
1.
Point out the error in the program
#include<stdio.h>
#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;
}
26250.00 7500.00
Nothing will print
Error: Multiple declaration of si
Garbage values
Answer: Option
Explanation:

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

Discussion:
9 comments Page 1 of 1.

Zeal said:   1 decade ago
So, two lines must not be in macro declaration huh. Is it?

Wikiok said:   1 decade ago
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:   1 decade ago
Doubt...........
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.
Why?

Shashi said:   1 decade ago
@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:   1 decade ago
Then why this program returns no error [http://indiabix.com/c-programming/c-preprocessor/032002] Program -7.

#include<stdio.h>
#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:   1 decade ago
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:   1 decade ago
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:   10 years ago
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:   5 years ago
@Shashi, yes correct explanation!

Post your comments here:

Your comments will be displayed after verification.