views:

276

answers:

6
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??

+4  A: 

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
'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
@esh C is kept simple. It doesn't require the compiler to do any more complex analysis like this.
Johannes Schaub - litb
+1  A: 

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
Sorry, i had to give -1. It's not related to linkage in any way. `extern const int t = 5; char buf[t + 5];` would work equally well in C++.
Johannes Schaub - litb
Hmm, you're right..
Jordan Lewis
i think it has to do with the fact you are not allowed to have dynamic arrays in C, but C++ has some functionality for it
Bob Fincheimer
+8  A: 

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

See this stack overflow question

David Sykes
+3  A: 

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
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
+3  A: 

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
C99 allows variable sized arrays as local variables
Spudd86
+1  A: 
Jens Gustedt
In C99, `sizeof(buf)` would be evaluated at runtime and yield `10` too. :)
Johannes Schaub - litb
yes, you are right, I was thinking that sizeof should always be determined at compile time. But it seems, just with that dynamically sized arrays, this is not true any more. Argh.
Jens Gustedt