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

1
  • Th discussion in my answer here about what is a constant expression with respect to variable length arrays is relevant. Commented Jul 9, 2014 at 13:03

4 Answers 4

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.

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

6 Comments

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 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.
VLA is supported in C++ too. I just checked it.
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.
So it is just limited to gcc.
|
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.[...]

Comments

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 :)

Comments

-1

Sizeof operator is compiler independent.

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

VLA-as-function-argument

Comments

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.