in Programming in C
335 views
0 votes
0 votes
tokens passed to macros are treated as int, float or string?
in Programming in C
by
335 views

4 Comments

it may be anything, but note that they didn't evaluate while sending.

i mean, if you call  f(2,3+5,7) ===> to f(int a, int b, int c) ===> before actual parameters binding to the formal parameters, the expressions get evaluated ===> a=2, b=8,c=7

if in the case of macro, 3+5 directly bind, as it is

0
0
I was trying sending floating point numbers as parameters. It was showing error. why?
0
0
post the code
0
0
#include<stdio.h>
#define ADD(a,b) a+b
int main()
{
    printf("%f",ADD(5.50,5.50));
    return 0;
}

I got my mistake.. i was using %d instead of %f

It's working now

but thanks for the above info.. it's helpful :)
0
0

1 Answer

0 votes
0 votes

Arguments to macros are simply passed as text, and after preprocessing

Your code become     printf("%f",ADD(5.50,5.50));
to
    printf("%f",5.50+5.50);
And now compiler considers them as double, addition is in double, and %f is for double hence prints double value.
Why%f is double?
https://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf

Why 5.50 is double and not float?

https://stackoverflow.com/questions/4353780/why-floating-point-value-such-as-3-14-are-considered-as-double-by-default-in-msv

Related questions