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

6 Answers 6

9

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

See this stack overflow question

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

C99 allows variable sized arrays as local variables
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

2 Comments

'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 C is kept simple. It doesn't require the compiler to do any more complex analysis like this.
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.

1 Comment

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

2 Comments

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++.
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
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*)

2 Comments

In C99, sizeof(buf) would be evaluated at runtime and yield 10 too. :)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.