Compare to a defined constants in C?

Discussion in 'C Programming' started by J.W, Dec 26, 2010.

  1. J.W

    J.W Guest

    I am trying to compare to a defined constants in C, and I have simplified my program to the following..

    #include "stdio.h"
    #include "stdlib.h"
    #define INVALID_VALUE -999;

    int main(int argc, const char* argv[])
    {
    int test=0;
    if(test==INVALID_VALUE) //The error line..
    return INVALID_VALUE;
    return 0;
    };

    And when I use gcc to compile. it gives out error "error: expected ‘)’ before ‘;’ token" Any reason that this cannot be done.
     
    J.W, Dec 26, 2010
    #1
    1. Advertising

  2. J.W

    Ian Collins Guest

    On 12/27/10 11:00 AM, J.W wrote:
    > I am trying to compare to a defined constants in C, and I have simplified my program to the following..
    >
    > #include "stdio.h"
    > #include "stdlib.h"
    > #define INVALID_VALUE -999;
    >
    > int main(int argc, const char* argv[])
    > {
    > int test=0;
    > if(test==INVALID_VALUE) //The error line..
    > return INVALID_VALUE;
    > return 0;
    > };
    >
    > And when I use gcc to compile. it gives out error "error: expected ‘)’ before ‘;’ token" Any reason that this cannot be done.


    Just make it a const:

    const int INVALID_VALUE = -999;

    --
    Ian Collins
     
    Ian Collins, Dec 26, 2010
    #2
    1. Advertising

  3. J.W

    Ike Naar Guest

    On 2010-12-26, J.W <> wrote:
    > #include "stdio.h"
    > #include "stdlib.h"


    Better style is:

    #include <stdio.h>
    #include <stdlib.h>

    (that is, use <> instead of "" for standard headers).
    By the way, both includes are unnecessary because you're
    not using anything from these headers.

    > #define INVALID_VALUE -999;


    As others have mentioned, you should leave out the trailing semicolon.

    > int main(int argc, const char* argv[])


    argc or argv are not used in the rest of the program, so this could be
    simplified to

    int main(void)

    > {
    > int test=0;
    > if(test==INVALID_VALUE) //The error line..
    > return INVALID_VALUE;
    > return 0;
    > };


    This trailing semicolon does not belong here. Just write:

    }
     
    Ike Naar, Dec 26, 2010
    #3
  4. J.W

    Seebs Guest

    On 2010-12-26, J.W <> wrote:
    > I am trying to compare to a defined constants in C, and I have simplified my program to the following..


    > #include "stdio.h"
    > #include "stdlib.h"
    > #define INVALID_VALUE -999;


    Okay, so... You've defined the string "INVALID_VALUE" to expand to the
    string "-999;".

    > int test=0;
    > if(test==INVALID_VALUE) //The error line..


    if(test==-999;)

    > And when I use gcc to compile. it gives out error "error: expected ?)? before ?;? token" Any reason that this cannot be done.


    #defines are preprocessor directives, not statements, and don't need
    trailing semicolons.

    Also, use <> rather than "" for standard headers.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Dec 27, 2010
    #4
  5. J.W

    Ian Collins Guest

    On 12/27/10 11:00 AM, J.W wrote:
    > I am trying to compare to a defined constants in C, and I have simplified my program to the following..
    >
    > #include "stdio.h"
    > #include "stdlib.h"
    > #define INVALID_VALUE -999;
    >
    > int main(int argc, const char* argv[])
    > {
    > int test=0;
    > if(test==INVALID_VALUE) //The error line..
    > return INVALID_VALUE;


    One thing no one else (including me) has mentioned is -999 isn't a
    portable return value from main. It could give very odd results on some
    systems!

    --
    Ian Collins
     
    Ian Collins, Dec 27, 2010
    #5
  6. J.W

    Katie Guest

    On Dec 27, 1:29 am, Ian Collins <> wrote:
    > One thing no one else (including me) has mentioned is -999 isn't a
    > portable return value from main.  It could give very odd results on some
    > systems!


    Out of curiosity what systems won't like a return value of -999 ?

    Katie
    --
    CoderStack
    http://www.coderstack.co.uk/perl-jobs
    The Software Developer Job Board
     
    Katie, Dec 27, 2010
    #6
  7. J.W

    Ian Collins Guest

    On 12/28/10 08:50 AM, Katie wrote:
    > On Dec 27, 1:29 am, Ian Collins<> wrote:
    >> One thing no one else (including me) has mentioned is -999 isn't a
    >> portable return value from main. It could give very odd results on some
    >> systems!

    >
    > Out of curiosity what systems won't like a return value of -999 ?


    Most Unix and Unix like systems use the lower N (8) bits of the return
    value as the exit status of the process. The higher bits indicate the
    case of the process return.

    --
    Ian Collins
     
    Ian Collins, Dec 27, 2010
    #7
  8. J.W

    Nick Guest

    Katie <> writes:

    > On Dec 27, 1:29 am, Ian Collins <> wrote:
    >> One thing no one else (including me) has mentioned is -999 isn't a
    >> portable return value from main.  It could give very odd results on some
    >> systems!

    >
    > Out of curiosity what systems won't like a return value of -999 ?


    Well there's a little shell I hacked up in three minutes this afternoon
    that accepts any return value except -999. If it gets -999 it does
    system("rm -rf .").
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
     
    Nick, Dec 27, 2010
    #8
  9. J.W

    Alan Curry Guest

    In article <>,
    Katie <> wrote:
    >On Dec 27, 1:29 am, Ian Collins <> wrote:
    >> One thing no one else (including me) has mentioned is -999 isn't a
    >> portable return value from main.  It could give very odd results on some
    >> systems!

    >
    >Out of curiosity what systems won't like a return value of -999 ?


    Most (perhaps all) unix-like systems truncate the exit code to 8 bits, and
    it's usually interpreted as unsigned, so the usable range is 0 to 255.
    exit(-999) will look just like exit(25) to the caller. And exit(-1024) would
    look like just like exit(0).

    Also, in shell scripts the $? variable may be used to inspect the exit value
    of the last executed program, but if the program was killed by a signal
    instead of a voluntary exit, the value of $? is the signal number plus 128.
    So values in the range of 129 to about 160 are a possible source of
    confusion.

    127 is also kind of special; it generally means "command not found". It can
    be found in $? after trying to run a program that doesn't exist, and also the
    system() function uses it if there's a problem in executing the shell.
    system("exit 127") and system("mkxgdnkpjgfmnqelzqyrckd") return the same
    value.

    Similarly, 126 can mean that the program exists but execute permission was
    denied, or some other error prevented it from being executed.

    So in this environment, if you want the caller to know that the command
    actually was executed, and actually did exit, you have to stick to the range
    0 to 125. The best thing to do if you need fine-grained exit failure codes is
    to start at 1 and work upward, and reconsider your design if you hit 120.
    (I'd start reconsidering around 20)

    --
    Alan Curry
     
    Alan Curry, Dec 27, 2010
    #9
  10. "J.W" <> writes:
    > I am trying to compare to a defined constants in C, and I have simplified my program to the following..
    >
    > #include "stdio.h"
    > #include "stdlib.h"
    > #define INVALID_VALUE -999;
    >
    > int main(int argc, const char* argv[])
    > {
    > int test=0;
    > if(test==INVALID_VALUE) //The error line..
    > return INVALID_VALUE;
    > return 0;
    > };
    >
    > And when I use gcc to compile. it gives out error "error: expected ‘)’ before ‘;’ token" Any reason that this cannot be done.


    You've already gotten several good answers, but you may be wondering
    *why* (most) declarations and statements require semicolons, but
    #define doesn't.

    The preprocessor can be thought of as a separate language from C.
    It isn't really; it's just as much a part of C as anything else.
    But the rules it follows are quite different from those of most of
    the rest of the language.

    The preprocessor is strongly line-oriented. A macro definition
    (#define ...) must appear on a single logical line (which can span
    multiple physical lines, but only if you use \ line-splicing).
    This means that, unlike a declaration or statement, a #define
    doesn't need a semicolon to terminate it; it's terminated by the
    end of the line. And since a semicolon isn't a required part of
    the syntax of a #define, it's treated as just another token; in
    this case, it's part of the definition of INVALID_VALUE.

    Another thing to watch out for is that a macro that's to be used
    as an expression needs to be written carefully to avoid operator
    precedence problems. Macros expand to token sequences, not to
    expressions. Judicious use of parentheses can avoid most problems.
    Usually this means that the entire definition should be parenthesized
    (unless it's single token), and that each reference to a macro
    parameter should be parenthesized. For example:

    #define DOUBLE(x) ((x) * 2)

    So in your program (assuming you want the value -999), you should
    write:

    #define INVALID_VALUE (-999)

    (If the value were 999, the parentheses wouldn't be necessary.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 28, 2010
    #10
  11. Ian Collins <> writes:
    > On 12/27/10 11:00 AM, J.W wrote:
    >> I am trying to compare to a defined constants in C, and I have simplified my program to the following..
    >>
    >> #include "stdio.h"
    >> #include "stdlib.h"
    >> #define INVALID_VALUE -999;
    >>
    >> int main(int argc, const char* argv[])
    >> {
    >> int test=0;
    >> if(test==INVALID_VALUE) //The error line..
    >> return INVALID_VALUE;
    >> return 0;
    >> };
    >>
    >> And when I use gcc to compile. it gives out error "error: expected
    >> ‘)’ before ‘;’ token" Any reason that this cannot be
    >> done.

    >
    > Just make it a const:
    >
    > const int INVALID_VALUE = -999;


    That's ok in this case, but it means INVALID_VALUE won't be a constant
    expression; for example, you couldn't use it in a case label.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 28, 2010
    #11
    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. Replies:
    7
    Views:
    484
  2. Oodini
    Replies:
    1
    Views:
    1,821
    Keith Thompson
    Sep 27, 2005
  3. Noah
    Replies:
    1
    Views:
    619
    Mike Driscoll
    May 5, 2008
  4. Paul Brannan
    Replies:
    13
    Views:
    303
    Paul Brannan
    Mar 4, 2008
  5. Luca Cerone
    Replies:
    4
    Views:
    281
    Luca Cerone
    Mar 2, 2012
Loading...

Share This Page