I’m using Dev-C++ v4.9.8.0 and I wrote the lines
const int i = 512;
i = 200;
and when I went to compile, I expected to see a compile-time error, but only got a warning. I told Dev-C++ that I want to compile this into a C program. My compiler under the hood is MinGW gcc v2.95, all of this running on a Windows 7 machine. I thought it was a language standard to generate a compile-time error when a program attempts to change a value declared with the const qualifier. Does the standard say this only warrants a warning?
Here’s my program:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
const int i = 512;
i = 250;
system("PAUSE");
return 0;
}
And here’s the Compile Log:
7 C:\C_Programs\Practice\main.c
[Warning] assignment of read-only variable `i’
“Does the standard say this only warrants a warning?”
The standard make no distinction of warnings and errors. The standard only has diagnostic messages.
In this program, there is a constraint violation of the assignment operator. The implementation has to issue a diagnostic message. The compiler is then free to continue to translate this program. But a program with a constraint violation is not a C program.