declaring two or moer arguments in macros (C)

Thread Starter

aamirali

Joined Feb 2, 2012
412
1. When i passed two variables in macro it showed error:
like

#define mul(x,y) (x*y)

main()
{
mul (5,10)
}

2. It showed x& y are undefined.
3. When I use this method, it return a value. can i collect it like
val = mul(5.10)
 

Papabravo

Joined Feb 24, 2006
21,227
What exactly was the error? Macro in this context specifically means "text substitution". It does not mean "value substitution". This common misunderstanding leads to numerous problems.
 

Thread Starter

aamirali

Joined Feb 2, 2012
412
Hi what does it mean macro means text substitution & value substitution? When I pass value like in macro, its value is substitutd
 

Papabravo

Joined Feb 24, 2006
21,227
The C preprocessor is the program that processes your source file before handing it to the compiler. It handles macros by substituting the string text for the argument text and rescanning to see if there are further substitutions required. When there are no more substitutions to be made, the resultant text is passed to the compiler. In this process there is no such thing as converting a string to a value or generating even so much as a single instruction of code.

No doubt your compiler will give you the opportunity to view the output of the preprocessor. Look for that feature to try for a deeper understanding. If not look for a public domain version of "cpp.exe" which is just the C-preprocessor. Put in a C source file and examine the output file.
 

ErnieM

Joined Apr 24, 2011
8,377
Hi what does it mean macro means text substitution & value substitution? When I pass value like in macro, its value is substitutd
Well, not really. When you use:

#define mul(x,y) (x*y)

the pre-processor uses the text at positions x and y for the substitutions, not the values. This may best be shown by passing something like

mul (a+3,b+4)

this will expand to:

a + 3 * b + 4 = a + 3b + 4

which may be a completely unexpected result.
 

raviypujar

Joined May 20, 2012
9
if the code pasted here is original code then I think error may be due to missing semi colon at the end me mul.

-Ravi

<SNIP>
 
Last edited by a moderator:
Top