declaring two or moer arguments in macros (C)

Discussion in 'General Electronics Chat' started by aamirali, May 28, 2012.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    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)
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    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.
     
  3. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    Hi what does it mean macro means text substitution & value substitution? When I pass value like in macro, its value is substitutd
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    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.
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    The OP still hasn't told us what the original error was so all this is just speculation
     
  7. raviypujar

    New Member

    May 20, 2012
    9
    2
    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: Jun 17, 2012
Loading...