2

Consider:

#include <stdio.h>

int const NAMESIZE = 40;
int const ADDRSIZE = 80;
typedef char NameType[NAMESIZE];
typedef char AddrType[ADDRSIZE];

typedef struct
{
    NameType name;
    AddrType address;
    double salary;
    unsigned int id;
} EmpRecType;

int main(int * argc, char * argv[])
{
    EmpRecType employee;
    return 0;
}

If I use #define instead of const it compiles.

This is the error:

employee.c:5:14: error: variably modified 'NameType' at file scope
employee.c:6:14: error: variably modified 'AddrType' at file scope

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    What error does it give? – Jeremy Sep 03 '11 at 22:53
  • 1
    possible duplicate of [Variably modified array at file scope](http://stackoverflow.com/questions/1712592/variably-modified-array-at-file-scope) – user703016 Sep 03 '11 at 22:55
  • 2
    Shouldn't it be `const int` instead of `int const`? – Benoit Garret Sep 03 '11 at 22:55
  • `int const` is valid in C++ - not sure if they also made the same thing valid in C. – Ken Wayne VanderLinde Sep 03 '11 at 22:56
  • maybe so .. I switched them around and it still doesn't compile.. I'm using gcc 4.5.3 – first_order_logic Sep 03 '11 at 22:58
  • @Benoit Garret: The `const` keyword is left associative, i.e. it acts upon what's left to it. The exception is if `const` is the first token in a statement, but then and only then. This semantics is the same in C and C++. – datenwolf Sep 03 '11 at 23:44
  • It isn't a good duplicate target (it is for Objective-C). The one for C is *[Variably modified array at file scope in C](https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c)*. – Peter Mortensen Jul 29 '23 at 09:15
  • Does this answer your question? [Variably modified array at file scope in C](https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c) – Toby Speight Jul 29 '23 at 12:04

2 Answers2

9

One of the differences between C and C++ is that in C++ a const int object is a constant, i.e. it can be used to form constant expressions. In C, on the other hand, a const int object is not a constant at all (it's more like "unchangeable variable").

Meanwhile, array size for file scope arrays in C is required to be a constant expression, which is why your const int object does not work in that role. (The above means, BTW, that your code will compile perfectly fine as C++, but won't compile as C.)

In C language to define named constants you have to use either #define or enums. In your specific case it could be done as follows

#define NAMESIZE 40
#define ADDRSIZE 80

P.S. If you replace your file-scope arrays with local arrays, your C code will compile as is, i.e. with const int objects as array sizes, because modern C (ANSI C99) supports variable-length arrays (VLA) in local scope. (And your arrays will be VLA in that case). In older versions of C (like ANSI C89/90) the code will not compile even with local arrays.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
7

Those const declarations, in C, just define some read only memory, they are not true constants. They can't be evaluated until runtime which is too late for the array declarations.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490