Why can't I use const globally?

This is a discussion on Why can't I use const globally? within the C Programming forums, part of the General Programming Boards category; Instead of define i was hoping I could use const to declare global constants. When I remove my #defines and ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    Why can't I use const globally?

    Instead of define i was hoping I could use const to declare global constants. When I remove my #defines and replace them with const int however, I get compiler errors.

    Can someone explain why?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Well perhaps if you posted actual error messages....

    For one thing, this works
    #define SIZE 10
    int array[SIZE];

    Whereas this doesn't (but it does in C++)
    const int size = 10;
    int array[size];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Well the actual error messages aren't all that exciting:

    Code:
    //#define INPUTMAX  255 compiles fine.
    const int INPUTMAX=255;

    assign1b.c:79: error: variable-sized object may not be initialized
    assign1b.c:79: warning: excess elements in array initializer

    So const variable's are fine as long as they aren't an array initializer?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    So are either of those lines actually line 79 of assign1b.c ?

    My guess is that you'll find an array at that point in the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Yeah definitely. But why is it that C doesn't support the use of constants in that context? And are there any other exceptions I should know about?

    I liked the typesafing of const in C++, but since it seems C's constants are poorly implemented I am just better off enlisting the preprocessor with #define?

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by keira View Post
    Yeah definitely. But why is it that C doesn't support the use of constants in that context? And are there any other exceptions I should know about?

    I liked the typesafing of const in C++, but since it seems C's constants are poorly implemented I am just better off enlisting the preprocessor with #define?
    Yes, although some recommend type-safe methods, since you could really define INPUTMAX as anything, integer, string etc.

    Such methods the "enum hack"
    Code:
    enum whatever_t {
        INPUTMAX = 255
    };
    And I think there are a couple of other ways... anyway see #define vs const
    Last edited by zacs7; 09-14-2007 at 08:20 PM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,164
    Quote Originally Posted by keira View Post
    Yeah definitely.
    You may think that, keira, but you're wrong.

    Salem's response reflected the fact that the error messages you're getting are typically associated with a messed up declaration of an array. They have no meaning for declarations of constants, such as the code you showed.
    Quote Originally Posted by keira View Post
    But why is it that C doesn't support the use of constants in that context?
    The reason is historical. When C was first created, #define's were the normally accepted ways to introduce compile time constants. The notion of const (as a keyword, or as a concept) did not appear until C was standardised (more accurately, it appeared while the standard was being developed, but in practical terms the main point of appearance of const was the standard. The actual meaning of const in C has a few meanings different from what was introduced in C++.
    Quote Originally Posted by keira View Post
    And are there any other exceptions I should know about?
    I'm sure there are, particularly if you expect C to behave like C++ (which is what I suspect you're doing). There are also some incompatibilities between C standards, and between the latest C standard and C++.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    There are a vast number of differences between C and C++
    http://david.tribble.com/text/cdiffs.htm

    In C, const just means "tell me if I try to modify this memory location". The compiler doesn't regard the assigned value as special in any way. For example, trying to use it as an array size.
    The symbol with the const qualifier ALWAYS occupies a slot in memory.

    In C++, const is a lot closer to the idea of #define, and the compiler always knows what that value is. As such, you can use it for array sizes and any other place where the size must be known at compile time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,164
    Quote Originally Posted by Salem View Post
    There are a vast number of differences between C and C++
    http://david.tribble.com/text/cdiffs.htm
    That link is a good summary of the differences between C99 and C++. It does not really consider the incompatibilities between C++ and C89/90 - the definitive description of those incompatibilities is supplied in an annex of the C++ standard.
    Quote Originally Posted by Salem View Post
    In C, const just means "tell me if I try to modify this memory location". The compiler doesn't regard the assigned value as special in any way. For example, trying to use it as an array size.
    The symbol with the const qualifier ALWAYS occupies a slot in memory.

    In C++, const is a lot closer to the idea of #define, and the compiler always knows what that value is. As such, you can use it for array sizes and any other place where the size must be known at compile time.
    While I know what you mean, the eyes of the C++ standards committee would be spinning if they literally interpreted that characterisation of const. LOL! There is no involvement of the preprocessor (for C or C++) with const.

    A more formal description is that const symbols;

    1) have internal linkage in C++ (unless they also explicitly declared extern) but they have external linkage in C; and

    2) can be treated by the compiler as compile-time constants in C++, but not in C.

    The first means that, if a const is declared in a header file, it can be initialised (given a value) within a header file in C++, but must be defined only once (eg in a single source file) in C.

    The second is the reason that a symbol which is a const integer cannot be used at compile time as an array size.

    Another consequential incompatibility between C and C++ is that a const object in C++ must be initialised in every source file (unless is it also declared extern) but that is not required in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21