7

Is this valid in C language?

#include <stdio.h>
int main()
{
  int i = 5;
  int a[i];     // Compiler doesn't give error here. Why?
  printf("%d",sizeof(a));  //prints 5 * 4 =20. 4 is the size of integer datatype.
  return 0;
}

Compiler doesn't give error at the statement int a[i];. i isn't a constant then how can it compile successfully? Is it because I am using gcc compiler? Is it allowed in C++?

user221458
  • 716
  • 2
  • 7
  • 17
  • Th discussion in [my answer here](http://stackoverflow.com/a/21273849/1708801) about what is a constant expression with respect to variable length arrays is relevant. – Shafik Yaghmour Jul 09 '14 at 13:03

4 Answers4

17

Yes, this is valid as of C99, and is called a variable-length array (VLA). In other words, it has been in an official language standard for around 14 years.

No, it's not valid in C++, see this question for details.

Also note that sizeof is not a function, so that can be written as printf("%zu\n", sizeof a); which also uses the proper format specifier for a size_t value.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Ok thanks. Also does gcc compiles a c program using rules for c++ ? I can declare variables after using printf function and it doesn't give error. – user221458 Oct 02 '13 at 11:03
  • 3
    @user221458 Please *read* the text about C99, since you seem to be very much unaware of the language you're trying to use. You can declare variables more freely in C99. – unwind Oct 02 '13 at 11:04
  • VLA is supported in C++ too. I just checked it. – user221458 Oct 02 '13 at 11:11
  • VLA is not part of C++, it's supported by that compiler as an extension to C++. In other words code using VLAs will probably not compile if you use a different compiler. – john Oct 02 '13 at 11:15
  • So it is just limited to gcc. – user221458 Oct 02 '13 at 11:21
  • Note that gcc/g++ with -std=c++14 won't support this anymore - the sizeof() will generate a warning and incorrect code. – sstn Nov 12 '15 at 13:18
2

This is valid C99 it is called Variable Length Array(VLA) gcc supports VLA as an extension outside of C99 mode with respect to C++ both gcc and clang support variable length arrays as an extension even though this is really a C99 feature.

You can build using the -pedantic argument in gcc and clang both will give a warning similar to the following:

warning: variable length arrays are a C99 feature [-Wvla-extension]

sizeof is expected to work correctly with VLA although it will be evaluated instead of an integer constant. Although you do have undefined behavior in your code since you specified the wrong format specifier for size_t which is zu and not d. The C99 draft standard in section 7.19.6.1 The fprintf function which printf's section refers back to for the format string paragraph 9 says:

If a conversion specification is invalid, the behavior is undefined.[...]

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

I'd just add to unwind's answer that in C++14, there will be runtime-sized arrays, which work pretty much the same as VLA.

See chapter 8.3.4 in N3690 (array of runtime bound of T)

They seem to be supported in clang-3.3 (in C++1y mode), but NOT in GCC 4.8 (the support should come in GCC 4.9). When you write the code in pre-C++14 mode (c++03, c++11), your code will probably compile, but it should issue a warning about using a C99 feature not supported in C++.

And you always should compile with most pedantic warnings enabled :)

v154c1
  • 1,698
  • 11
  • 19
-1

Sizeof operator is compiler independent.

You could read more about this in the following links ::

VLA-as-function-argument

vkulkarni
  • 849
  • 1
  • 8
  • 12