hi why can not i us #if in the macro?

Discussion in 'C Programming' started by baumann@pan, Apr 11, 2005.

  1. baumann@pan

    baumann@pan Guest

    hi all,

    what's the problem of the macro below?

    #define COMMENT_LINE(p) \
    #if 0\
    (p);\
    #endif

    when i compiled it in vs.net, it complains

    c99.cpp(12) : error C2162: formal paramer of macro needed
    c99.cpp(17) : error C2059: syntax error : "constant"
    c99.cpp(17) : error C2065: "endif" : undeclared symbol


    i don;t know why.
    baumann@pan, Apr 11, 2005
    #1
    1. Advertising

  2. baumann@pan

    Alex Fraser Guest

    "baumann@pan" <> wrote in message
    news:...
    > what's the problem of the macro below?
    >
    > #define COMMENT_LINE(p) \
    > #if 0\
    > (p);\
    > #endif


    You cannot use preprocessor directives in a #define. That is just the way
    the language is defined. If you explain what you are trying to achieve,
    maybe someone can suggest an alternative solution.

    Alex
    Alex Fraser, Apr 11, 2005
    #2
    1. Advertising

  3. In article <>,
    baumann@pan <> wrote:

    >what's the problem of the macro below?
    >
    >#define COMMENT_LINE(p) \
    >#if 0\
    > (p);\
    >#endif


    You seem to be expecting the macro to expand into something containing
    the #if directive, but it doesn't work like that.

    The lines are pasted together first, producing

    #define COMMENT_LINE(p) #if 0 (p);#endif

    which is syntactically incorrect.

    -- Richard
    Richard Tobin, Apr 11, 2005
    #3
  4. baumann@pan wrote on 11/04/05 :
    > what's the problem of the macro below?
    >
    > #define COMMENT_LINE(p) \
    > #if 0\
    > (p);\
    > #endif


    This is not C. You can't have proprocessor thingies in macros (# is
    already used for something else).

    > when i compiled it in vs.net, it complains
    >
    > c99.cpp(12) : error C2162: formal paramer of macro needed


    Your extension should be .c if you intent to compile a C source.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "C is a sharp tool"
    Emmanuel Delahaye, Apr 14, 2005
    #4
  5. In article <>,
    Emmanuel Delahaye <> wrote:

    >> when i compiled it in vs.net, it complains


    >> c99.cpp(12) : error C2162: formal paramer of macro needed


    >Your extension should be .c if you intent to compile a C source.


    That's usually true in practice -- but it's not part of the
    standard ;-)
    --
    "Who Leads?" / "The men who must... driven men, compelled men."
    "Freak men."
    "You're all freaks, sir. But you always have been freaks.
    Life is a freak. That's its hope and glory." -- Alfred Bester, TSMD
    Walter Roberson, Apr 14, 2005
    #5
  6. baumann@pan

    Richard Bos Guest

    "Emmanuel Delahaye" <> wrote:

    > baumann@pan wrote on 11/04/05 :
    > > what's the problem of the macro below?
    > >
    > > #define COMMENT_LINE(p) \
    > > #if 0\
    > > (p);\
    > > #endif

    >
    > This is not C. You can't have proprocessor


    _Pre_processor.

    > > when i compiled it in vs.net, it complains
    > >
    > > c99.cpp(12) : error C2162: formal paramer of macro needed

    >
    > Your extension should be .c if you intent to compile a C source.


    Depends on the compiler, surely? With gcc, this is true, but that means
    nothing for any other compiler.

    Richard
    Richard Bos, Apr 15, 2005
    #6
  7. (Richard Bos) writes:
    > "Emmanuel Delahaye" <> wrote:
    >> baumann@pan wrote on 11/04/05 :

    [...]
    >> > when i compiled it in vs.net, it complains
    >> >
    >> > c99.cpp(12) : error C2162: formal paramer of macro needed

    >>
    >> Your extension should be .c if you intent to compile a C source.

    >
    > Depends on the compiler, surely? With gcc, this is true, but that means
    > nothing for any other compiler.


    Most C compilers (all the ones I know of) use ".c" as the normal
    suffix for C source files; ".cpp" is probably C++. Yes, it depends on
    the compiler, but it's a widespread convention.

    --
    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, Apr 15, 2005
    #7
  8. On Fri, 15 Apr 2005 06:15:37 GMT, in comp.lang.c ,
    (Richard Bos) wrote:

    >> Your extension should be .c if you intent to compile a C source.

    >
    >Depends on the compiler, surely? With gcc, this is true, but that means
    >nothing for any other compiler.


    microsoft copmilers assume its C++ unless its suffix is .c. Some sun
    compilers differentiate between .C and .c.

    Yuck.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Apr 15, 2005
    #8
  9. Mark McIntyre <> writes:
    > On Fri, 15 Apr 2005 06:15:37 GMT, in comp.lang.c ,
    > (Richard Bos) wrote:
    >
    >>> Your extension should be .c if you intent to compile a C source.

    >>
    >>Depends on the compiler, surely? With gcc, this is true, but that means
    >>nothing for any other compiler.

    >
    > microsoft copmilers assume its C++ unless its suffix is .c. Some sun
    > compilers differentiate between .C and .c.


    Somewhat OT, but most Unix compilers use ".c" for C source and either
    ".C" or ".cpp" for C++ source. (Windows compilers don't use ".C" for
    C++ because the file system can't distinguish between ".C" and ".c".)

    --
    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, Apr 15, 2005
    #9
  10. Mark McIntyre wrote on 15/04/05 :
    > On Fri, 15 Apr 2005 06:15:37 GMT, in comp.lang.c ,
    > (Richard Bos) wrote:
    >
    >>> Your extension should be .c if you intent to compile a C source.

    >>
    >> Depends on the compiler, surely? With gcc, this is true, but that means
    >> nothing for any other compiler.

    >
    > microsoft copmilers assume its C++ unless its suffix is .c. Some sun
    > compilers differentiate between .C and .c.


    Eclipse/CDT for example...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "C is a sharp tool"
    Emmanuel Delahaye, Apr 16, 2005
    #10
  11. Keith Thompson wrote on 15/04/05 :
    > Somewhat OT, but most Unix compilers use ".c" for C source and either
    > ".C" or ".cpp" for C++ source. (Windows compilers don't use ".C" for
    > C++ because the file system can't distinguish between ".C" and ".c".)


    Windows XP does, and you must be careful when using Eclipse/CDT,
    because .C invokes g++...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    ..sig under repair
    Emmanuel Delahaye, Apr 16, 2005
    #11
  12. On Fri, 15 Apr 2005 21:31:37 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >Mark McIntyre <> writes:
    >> On Fri, 15 Apr 2005 06:15:37 GMT, in comp.lang.c ,
    >> (Richard Bos) wrote:
    >>
    >>>> Your extension should be .c if you intent to compile a C source.
    >>>
    >>>Depends on the compiler, surely? With gcc, this is true, but that means
    >>>nothing for any other compiler.

    >>
    >> microsoft copmilers assume its C++ unless its suffix is .c. Some sun
    >> compilers differentiate between .C and .c.

    >
    >Somewhat OT, but most Unix compilers use ".c" for C source and either
    >".C" or ".cpp" for C++ source. (Windows compilers don't use ".C" for
    >C++ because the file system can't distinguish between ".C" and ".c".)


    that should read "couldn't, prior to about 1999". All modern windows
    OSen can do this quite handily, though the command interpreter still
    is too stupid.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Apr 16, 2005
    #12
  13. On Sat, 16 Apr 2005 08:39:58 +0200, in comp.lang.c , "Emmanuel
    Delahaye" <> wrote:

    >Keith Thompson wrote on 15/04/05 :
    >> Somewhat OT, but most Unix compilers use ".c" for C source and either
    >> ".C" or ".cpp" for C++ source. (Windows compilers don't use ".C" for
    >> C++ because the file system can't distinguish between ".C" and ".c".)

    >
    >Windows XP does, and you must be careful when using Eclipse/CDT,
    >because .C invokes g++...


    the reason I mentioned this is because we just got bitten on the a*se
    by precisely this 'bug'. We ported a library back from Solaris to XP,
    and the compile kept falling over mysteriously...


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Apr 16, 2005
    #13
  14. Mark McIntyre <> writes:
    > On Fri, 15 Apr 2005 21:31:37 GMT, in comp.lang.c , Keith Thompson
    > <> wrote:

    [...]
    >>Somewhat OT, but most Unix compilers use ".c" for C source and either
    >>".C" or ".cpp" for C++ source. (Windows compilers don't use ".C" for
    >>C++ because the file system can't distinguish between ".C" and ".c".)

    >
    > that should read "couldn't, prior to about 1999". All modern windows
    > OSen can do this quite handily, though the command interpreter still
    > is too stupid.


    Still a bit OT ...

    Yes, Windows distinguishes between ".c" and ".C", but not in the same
    way that Unix does. A file name retains whatever case was specified
    when it was created, but you can't have two files in the same
    directory named "foo.c" and "foo.C"; if you have a file called
    "foo.c", you can successfully open it either as "foo.c" or as "foo.C".

    Presumably this means that you can compile a given source file either
    as C or as C++ just by changing the name by which you refer to it
    (without actually changing the name of the file). (I say "presumably"
    because I don't actually use Windows compilers.) Not a huge deal, but
    something to watch out for.

    To make it topical, all this stuff affects the semantics of fopen(),
    which is part of the reason the C standard doesn't say much about what
    file names look like other than being strings.

    --
    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, Apr 17, 2005
    #14
  15. On Sun, 17 Apr 2005 20:34:18 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >Yes, Windows distinguishes between ".c" and ".C", but not in the same
    >way that Unix does. A file name retains whatever case was specified
    >when it was created, but you can't have two files in the same
    >directory named "foo.c" and "foo.C"; if you have a file called
    >"foo.c", you can successfully open it either as "foo.c" or as "foo.C".


    FWIW I seem to recall that you can, but creating them via the
    high-level functions or command line gui is pretty tricky.

    >Presumably this means that you can compile a given source file either
    >as C or as C++ just by changing the name by which you refer to it
    >(without actually changing the name of the file). (I say "presumably"
    >because I don't actually use Windows compilers.) Not a huge deal, but
    >something to watch out for.


    In fact no - all the compilers I know of are case-insensitive when it
    comes to filenames. This hs coincidentally how I know of this issue -
    we had some C++ we ported from solaris & linux back to windows, and a
    couple of files didn't compile properly with gcc. Turned out they were
    named ".C".


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
    Mark McIntyre, Apr 17, 2005
    #15
    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. Dead RAM
    Replies:
    20
    Views:
    1,083
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    558
    Jack Klein
    Sep 21, 2003
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    851
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,733
    Smokey Grindel
    Dec 2, 2006
  5. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    483
    Mark McIntyre
    Nov 22, 2005
Loading...

Share This Page