2

Here's a C program one of my friends had written. From what I know, arrays had to be initialised at compile time before C99 introduced VLA's, or using malloc during runtime.

But here the program accepts value of a const from the user and initialises the array accordingly. It's working fine, even with gcc -std=c89, but looks very wrong to me. Is it all compiler dependent?

#include <stdio.h>

int
main()
{
 int const n;
 scanf("%d", &n);
 printf("n is %d\n", n);
 int arr[n];
 int i;
 for(i = 0; i < n; i++)
   arr[i] = i;
 for(i = 0; i < n; i++)
   printf("%d, ", arr[i]);
 return 0;
}
4
  • 1
    If I compile this as you've suggested, I see the warning test.c:7: warning: writing into constant object (argument 2), indicating that the compiler is aware that this is not fine. Commented Oct 6, 2013 at 12:30
  • Of course, this means that the real problem here is you're trying to modify a const object. So nothing to do with VLAs ;) Commented Oct 6, 2013 at 12:30
  • 2
    It's a GNU extension prior to C99, compile with -pedantic. Commented Oct 6, 2013 at 12:32
  • Yeah, I got the warning, but I was expecting that the compiler would just not compile the code for me and the program wouldn't work. Why is that some rules are implemented completely, and some mistakes are let off with a warning? Commented Oct 6, 2013 at 12:35

2 Answers 2

2

Add -pedantic to your compile options (e.g, -Wall -std=c89 -pedantic) and gcc will tell you:

warning: ISO C90 forbids variable length array ‘arr’

which means that your program is indeed not c89/c90 compliant.

Change with -pedantic with -pedantic-errors and gcc will stop translation.

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

5 Comments

So if suppose I compiler as gcc -std=89 still due to variable length array code compiled as c99 ? And if I use gcc -std=89 -pedantic-errors then compiler will behave as c89 and don't compile?
Yeah, it's like the compiler is making us choose how we want it to behave, and not follow the said standards completely. How should we ideally predict the behavior?
@GrijeshChauhan regarding -pedantic, gcc documentation says Valid ISO C and ISO C++ programs should compile properly with or without this option [...] However, without this option, certain GNU extensions and traditional C and C++ features are supported as well. With this option, they are rejected.
So, is it safe to say that the program has undefined behavior as a C90 program, depending on the compiler used?
@Flipper It is safe to say the program is not a strictly conforming c90 program (c90 6.5.4.2 constraint is violated).
1

This called Variable Length Arrays and allowed in C99 . Compiling in c89 mode with -pedantic flag, compiler will give you warnings

[Warning] writing into constant object (argument 2) [-Wformat]  
[Warning] ISO C90 forbids variable length array 'arr' [-Wvla]
[Warning] ISO C90 forbids mixed declarations and code [-pedantic]

1 Comment

Indeed, but the OP is asking about -std=c89.

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.