fix code

Discussion in 'C Programming' started by aarklon@gmail.com, Mar 8, 2006.

  1. Guest

    Hi all,

    the following is a question which i found on a book,the reader is asked
    to predict the output

    #include<stdio.h>

    #define SUM(F_NAME,DATA_TYPE,L)\
    void F_NAME(DATA_TYPE x,DATA_TYPE y)\
    {\
    DATA_TYPE add;\
    add = x + y;\
    printf("The summation of "#DATA_TYPE""\
    " values is %"#L"\n",add);\
    }

    void sum_int(int,int);
    void sum_float(float,float);
    int main(void)
    {
    sum_int(3,5);
    sum_float(3.1,5.3);

    return 0;
    }

    SUM(sum_int,int,d);
    SUM(sum_float,float,f);

    Output is given as

    the summation of int values is 8
    the summation of float values is 8.400000


    But I am getting the error as # operator is not followed by a macro
    argument name.
    can anybody suggest ways to fix this code??
    , Mar 8, 2006
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote On 03/08/06 13:59,:
    > Hi all,
    >
    > the following is a question which i found on a book,the reader is asked
    > to predict the output
    >
    > #include<stdio.h>
    >
    > #define SUM(F_NAME,DATA_TYPE,L)\
    > void F_NAME(DATA_TYPE x,DATA_TYPE y)\
    > {\
    > DATA_TYPE add;\
    > add = x + y;\
    > printf("The summation of "#DATA_TYPE""\
    > " values is %"#L"\n",add);\


    Insert a space between the L and the following ".

    The sequence L"\n" is a wide string literal that
    will (eventually) produce a zero-terminated array of
    wide characters. This sequence is recognized during
    translation phase 3; macro processing doesn't happen
    until phase 4. By that time, the L is long gone so
    the macro processing encounters

    string_literal # wide_string_literal

    .... and the error results. Separating the L from what
    follows means the combination is no longer recognized
    as a wide string literal, so during macro expansion
    you have

    string_literal # L string_literal

    .... as intended.


    > }
    >
    > void sum_int(int,int);
    > void sum_float(float,float);
    > int main(void)
    > {
    > sum_int(3,5);
    > sum_float(3.1,5.3);
    >
    > return 0;
    > }
    >
    > SUM(sum_int,int,d);
    > SUM(sum_float,float,f);


    Another improvement would be to get rid of the
    semicolons in these two lines. After macro expansion
    you'll have

    void sum_int(...) {
    ...
    }
    ;
    void sum_float(...) {
    ...
    }
    ;

    --
    Eric Sosman, Mar 8, 2006
    #2
    1. Advertising

  3. Guest

    Can you explain what is meant by phase 4.
    as far as i know the different phases of a compiler are

    lexical analysis
    syntax analysis
    semantic analysis
    intermediate code generation
    code optimization
    code generation

    is it during intermediate code generation phase?
    , Mar 8, 2006
    #3
  4. Ben Pfaff Guest

    writes:

    > Can you explain what is meant by phase 4.


    The C standard divides translation into 8 phases. Phase 4 is
    this:

    4. Preprocessing directives are executed, macro invocations
    are expanded, and _Pragma unary operator expressions are
    executed. If a character sequence that matches the
    syntax of a universal character name is produced by token
    concatenation (6.10.3.3), the behavior is undefined. A
    #include preprocessing directive causes the named header
    or source file to be processed from phase 1 through phase
    4, recursively. All preprocessing directives are then
    deleted.

    --
    "I'm not here to convince idiots not to be stupid.
    They won't listen anyway."
    --Dann Corbit
    Ben Pfaff, Mar 8, 2006
    #4
  5. Micah Cowan Guest

    writes:

    > Hi all,
    >
    > the following is a question which i found on a book,the reader is asked
    > to predict the output
    >
    > #include<stdio.h>
    >
    > #define SUM(F_NAME,DATA_TYPE,L)\
    > void F_NAME(DATA_TYPE x,DATA_TYPE y)\
    > {\
    > DATA_TYPE add;\
    > add = x + y;\
    > printf("The summation of "#DATA_TYPE""\
    > " values is %"#L"\n",add);\
    > }
    >
    > void sum_int(int,int);
    > void sum_float(float,float);
    > int main(void)
    > {
    > sum_int(3,5);
    > sum_float(3.1,5.3);
    >
    > return 0;
    > }
    >
    > SUM(sum_int,int,d);
    > SUM(sum_float,float,f);
    >
    > Output is given as
    >
    > the summation of int values is 8
    > the summation of float values is 8.400000
    >
    >
    > But I am getting the error as # operator is not followed by a macro
    > argument name.
    > can anybody suggest ways to fix this code??


    Yes.

    1. Remove the final semicolons from your invocations of SUM(). Does
    the original have these?
    2. Use a space between the L and the " following it, or use a
    different letter.

    Your problem is that the sequence {L"} is the start of a /wide string
    literal/. The L will never be tokenized into an identifier, and
    therefore isn't recognized as the macro parameter.

    HTH,
    -Micah
    Micah Cowan, Mar 8, 2006
    #5
  6. Eric Sosman <> writes:
    > wrote On 03/08/06 13:59,:
    >> Hi all,
    >>
    >> the following is a question which i found on a book,the reader is asked
    >> to predict the output
    >>
    >> #include<stdio.h>
    >>
    >> #define SUM(F_NAME,DATA_TYPE,L)\
    >> void F_NAME(DATA_TYPE x,DATA_TYPE y)\
    >> {\
    >> DATA_TYPE add;\
    >> add = x + y;\
    >> printf("The summation of "#DATA_TYPE""\
    >> " values is %"#L"\n",add);\

    >
    > Insert a space between the L and the following ".
    >
    > The sequence L"\n" is a wide string literal that
    > will (eventually) produce a zero-terminated array of
    > wide characters. This sequence is recognized during
    > translation phase 3; macro processing doesn't happen
    > until phase 4. By that time, the L is long gone so
    > the macro processing encounters
    >
    > string_literal # wide_string_literal
    >
    > ... and the error results. Separating the L from what
    > follows means the combination is no longer recognized
    > as a wide string literal, so during macro expansion
    > you have
    >
    > string_literal # L string_literal
    >
    > ... as intended.


    Or use an identifier other than L.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 9, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Dopierala Jr.
    Replies:
    0
    Views:
    344
    Ken Dopierala Jr.
    Oct 1, 2004
  2. Patrick Philippot
    Replies:
    0
    Views:
    446
    Patrick Philippot
    Apr 14, 2006
  3. Xah Lee
    Replies:
    22
    Views:
    1,105
    Tim Roberts
    Mar 21, 2006
  4. Xah Lee
    Replies:
    23
    Views:
    1,041
    Tim Roberts
    Mar 21, 2006
  5. Xah Lee
    Replies:
    21
    Views:
    763
    Tim Roberts
    Mar 21, 2006
Loading...

Share This Page