Search code examples
cc11variable-declaration

Does C11 allow variable declarations at any place in a function?


Does the C11 standard (note I don't mean C++11) allow you to declare variables at any place in a function?

The code below is not valid in ANSI C (C89, C90):

int main()
{
  printf("Hello world!");
  int a = 5; /* Error: all variables should be declared at the beginning of the function. */
  return 0;
}

Is it valid source code in C11?


Solution

  • Yes. This was already valid in C99 (see the second bullet here).