8
const int t=5;
char buf[t+5];

When I compile this gives error in C but not in C++!!
Can anybody please explain me the reason?

Note: I know the const defaults to internal linkage in 'C++', where as in 'C' it defaults to external linkage. Does it has any relation to the above case??

Péter Török
  • 114,404
  • 31
  • 268
  • 329
esh
  • 273
  • 2
  • 6

6 Answers6

9

This isn't valid in C89 C, though it may be valid in C99

See this stack overflow question

Community
  • 1
  • 1
David Sykes
  • 48,469
  • 17
  • 71
  • 80
4

I think it's because the compiler can't evaluate t+5 to a constant expression. It looks like it should be OK, but:

One important point about array declarations is that they don't permit the use of varying subscripts. The numbers given must be constant expressions which can be evaluated at compile time, not run time.

Source

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • 't' is const variable, so compiler knows that its value will be same in the program lifetime. so why cann't it substitutes the variable 't' value and allocate the memory for buf. Btw when the memroy will be allocated for buf?? – esh Jun 17 '10 at 12:03
  • 1
    @esh C is kept simple. It doesn't require the compiler to do any more complex analysis like this. – Johannes Schaub - litb Jun 17 '10 at 12:05
4

As others explained, C is kept more simple than C++ and doesn't allow const variables to appear in integer constant expressions. But in both C89 and C++ declared arrays must have compile-time constant sizes.

You can use enumerations for this

enum {
  BufSize = 5
};

char buf[BufSize + 5];

It doesn't have to do with internal linkage - external linkage variables are equally viable in integer constant expressions in C++. The internal linkage in C++ rather is a consequence, but not a neccessity, of allowing them to appear in constant expressions. The C++ Standard explains why they have internal linkage by default

Because const objects can be used as compile-time values in C++, this feature urges programmers to provide explicit initializer values for each const. This feature allows the user to put const objects in header files that are included in many compilation units

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
3

In C the Size of an Array has to be an Constant Expression. Const Int is in C not an Constant Expression. It's meaning is more like "readonly". Use #define t 5 instead.

nuriaion
  • 2,621
  • 2
  • 23
  • 18
  • Why it is not giving error in c++? if I remove const from from the 1st line, c++ also gives error?. where the compiler will keep the value of t (i.e 5) during compile time, why cann't it use this value for buf? – esh Jun 17 '10 at 12:30
1

Yes, this is related to C's external linking of t.

You've declared an externally linked integer t. If you link this file with another that defines t, then your buffer's size would have to be determined after the file's compile-time, which is of course impossible in C.

Jordan Lewis
  • 16,900
  • 4
  • 29
  • 46
1

You have two issues here, dynamically sized arrays and consts. The concept of const is different in C and C++. For C it is just a variable that you don't have the right to change and thus is not a valid dimension for C-dialects that only allow for arrays with compile time fixed dimensions. The only way to define a `compile time constant' in C is to use an enumeration type

enum dummy { t=5 };

So such a thing would work with C89.

In contrast to that you code should also work in C99. There the array would be syntactically identified to be of dynamic size. Then any decent optimizer should be able to optimize this away. But beware also that sizeof(buf) would be the total size of the array (10) in C++ whereas with c99 it would be sizeof(char*)

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177