I'm confused by Const

Discussion in 'C Programming' started by shawn@133sh.com, Jan 20, 2004.

  1. Guest

    As far as i know, keyword "const" is invented to replace "define" identifier.
    But ...

    test1.c
    ------------------------------
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char **argv)
    {
    const int MAX_CHAR_NUM=10;
    char name[MAX_CHAR_NUM]="Computer";
    printf("My name is %s.\n", name);
    exit(EXIT_SUCCESS);
    }

    test2.c
    -----------------------------
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char **argv)
    {
    enum width {MAX_CHAR_NUM=10, OTHER};
    char name[MAX_CHAR_NUM]="Computer";
    printf("My name is %s.\n", name);
    exit(EXIT_SUCCESS);
    }

    why test2.c can be compiled and test1.c not?
    And what's the difference between c const and c++ const?
    Thanks.
     
    , Jan 20, 2004
    #1
    1. Advertising

  2. Richard Bos Guest

    () wrote:

    > As far as i know, keyword "const" is invented to replace "define" identifier.


    It isn't. const is used to make an actual, in-memory object read-only,
    while #define is used to create a compile-time, source code only, text
    replacement.

    > test1.c


    > const int MAX_CHAR_NUM=10;
    > char name[MAX_CHAR_NUM]="Computer";


    > test2.c


    > enum width {MAX_CHAR_NUM=10, OTHER};
    > char name[MAX_CHAR_NUM]="Computer";


    > why test2.c can be compiled and test1.c not?


    Because enum constants are compile-time constants, whereas const objects
    are read-only, but not, strictly speaking, constant. The name of the
    qualifier is confusing, it's true.

    > And what's the difference between c const and c++ const?


    C++ consts _are_ compile-time constant, AFAIK, but I'm not a C++ expert
    so ICBW.

    Richard
     
    Richard Bos, Jan 20, 2004
    #2
    1. Advertising

  3. On 20 Jan 2004 03:59:47 -0800, ()
    wrote:

    > As far as i know, keyword "const" is invented to replace "define" identifier.


    No.

    > But ...
    >
    > test1.c
    > ------------------------------
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char **argv)
    > {
    > const int MAX_CHAR_NUM=10;
    > char name[MAX_CHAR_NUM]="Computer";
    > printf("My name is %s.\n", name);
    > exit(EXIT_SUCCESS);
    > }
    >
    > test2.c
    > -----------------------------
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char **argv)
    > {
    > enum width {MAX_CHAR_NUM=10, OTHER};
    > char name[MAX_CHAR_NUM]="Computer";
    > printf("My name is %s.\n", name);
    > exit(EXIT_SUCCESS);
    > }
    >
    > why test2.c can be compiled and test1.c not?


    Because in C89 a "const int" doesn't qualify as an expression for
    array dimensioning while an enum does. An enum is an alias for some
    int value known to the compiler from the definition of this enum while
    a const int is not - even if the compiler "sees" the value to which it
    is initialized.

    > And what's the difference between c const and c++ const?


    One of the differences is that in C++ a const int may be used for
    array dimensioning (if its value is "visible" to the compiler
    according to a definition of "visible" given by the C++ Standard).
    Specifically

    const int MAX_CHAR_NUM=10;
    char name[MAX_CHAR_NUM]="Computer";

    would be OK in C++.

    --
    Horst
     
    Horst Kraemer, Jan 20, 2004
    #3
    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:
    11
    Views:
    1,143
  2. Javier
    Replies:
    2
    Views:
    603
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,159
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    927
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    552
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page