5

Why doesn't this C program compile? What is wrong with this?

I have tried it on wxDevC++ and Turbo C++ 3.0.

Main.c

#include<stdio.h>
#include<conio.h>

const int SIZE = 5;

int main(int argc, char ** argv)
{    
    char array[SIZE] = {'A', 'B', 'C', 'D', 'E'};

    printf("Array elements are,\n");
    int i=0;

    for(i=0 ; i<SIZE ; ++i)
    {
        printf("%c  ", array[i]);
    }

    getch();

    return 0;
}

Error Messages on the both of the compilers are similar.

f:\_Source-Codes\main.c In function `main':

8 f:\_Source-Codes\main.c variable-sized object may not be initialized
user366312
  • 16,949
  • 65
  • 235
  • 452

4 Answers4

15

Array size in C89/90 language must be specified by an integral constant expression (in general true for C99 as well). A const int object in C is not a constant expression, which is why you can't use it to specify array size. Note: this is one prominent difference between C and C++.

In C language the term constant refers to literal constants, i.e. 5, 10.2, 0xFF, 'a' and so on (enum constants are also constants, to be precise). const int object, once again, is not a constant in C and cannot be used to build constant expressions.

If you want to pre-declare a named constant to be used as array size in C, you have to use either #define or enum. The same applies to case labels, bit-field sizes and every other context requiring a constant expression.

See this for more details.

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Is this allowed in C++? If I did `const int SIZE = someFunction();`, the compiler wouldn't know at compile-time how much stack-space is needed for `char array[SIZE]`... – BlueRaja - Danny Pflughoeft Jul 02 '10 at 17:01
  • @BlueRaja: In C++ constant expressions can use `const` objects *initialized with constant expressions*. In your case, your `const` object is not initialized with a constant expression, which is why it is by itself not a constant. – AnT stands with Russia Jul 02 '10 at 17:02
  • @AndreyT, Then what is the use of const-keyword in C? – user366312 Jul 02 '10 at 17:21
  • @Andrey: any links as to what constitutes a *constant expression*? What if someFunction() is defined as `const int someFunction() { return 5; }` - would SIZE then be considered initialized with a constant expression? – BlueRaja - Danny Pflughoeft Jul 02 '10 at 17:25
  • @JMSA: `const` only means that you can't change the value of the variable later - the initial value itself may not be a constant expression. @BlueRaja: I guess that depends on whether the compiler decides to expand `someFunction` inline. – casablanca Jul 02 '10 at 17:39
  • @JMSA: `const` keyword in C is used to declare objects that should not be changed at run-time (and to declare const-qualified access paths to objects). It doesn't introduce compile-time constants, which is why it has significantly more limited use in C than in C++. – AnT stands with Russia Jul 02 '10 at 17:42
  • @BlueRaja: No. In todays C++ the result of a function call is never a constant expression, regardless of whether it is inline or not. In future C++ standard a new keyword is coming up (`constexpr` or something like that) that would allow you to declare functions that can be used in constant expressions. This is exactly what would apply to your function. – AnT stands with Russia Jul 02 '10 at 17:43
8

if the compiler is treating it as a '.c' file, the int i declaration needs to before any executable lines, speficically, before the printf.

EDIT, now that you showed the error message:

The compiler does not consider SIZE as a constant when compiling main. You could use #define SIZE 5 as a work-around.

According to K&R 2nd Ed. :

"The purpose of const is to announce objects that may be placed in read-only memory. ... Except that it should diagnose explicit attempts to change const objects, a compiler may ignore [the const] qualifier".

So declaring const int SIZE = 5 does not make SIZE a constant-expression, which is what an array size specifier requires.

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • Yes. But why doesn't const work? What does ANSI standard says? – user366312 Jul 02 '10 at 16:15
  • Correct, the declaration has to proceed the method. – Jamie Keeling Jul 02 '10 at 16:28
  • 2
    maybe because Turbo C++ 3.0 is almost 20 years old –  Jul 02 '10 at 16:30
  • @fuzzy lollipop, OK but I have also tested it on wxDevC++ and VS2008. – user366312 Jul 02 '10 at 16:33
  • 1
    @AShelly: Strictly speaking, your quite from K&R is irrelevant. A `const` object in C language is *never* a *constant*. The term *constant* in C has a completely different meaning. *Constant* in C terminology is what we call *literal* in C++ (like `5`, `0x1` or `'a'`). A `const int` object is not a constant in C and cannot be used where a *constant expression* is required. This is always true, regardless of the compiler and of whether it ignores "something" or not. – AnT stands with Russia Jul 02 '10 at 16:36
  • @AndreyT: Diving into K&R a little further, I see that it says the same thing. Editing my answer to clarify. – AShelly Jul 02 '10 at 16:56
5

Try replacing

const int SIZE = 5;

with

#define SIZE 5

Most C compilers don't allow declaring static arrays whose sizes are contained in variables (i.e. the array size is determined at runtime).

MAK
  • 26,140
  • 11
  • 55
  • 86
3

try this:

char array[] = {'A', 'B', 'C', 'D', 'E'};
Andrey
  • 59,039
  • 12
  • 119
  • 163
  • @overslacked, This is not true. The size depends first on the constant specified. `char array[2] = {'a','b','c'};` generates an error. `char array[3] = {'a','b'};` generates a 3 element array. The size is only infered from the initializer list if the `[]`s are empty. – AShelly Jul 02 '10 at 16:29