English C error while in compilation process

3 replies
Goto Page
To the start Previous 1 Next To the start
Up
Fraizeraust
Moderator
Offline Off
Currently I've been interested into getting in touch with the C programming language so I decided to learn some basics stuff. The progress is quite slow but not left in the abyss.

For now I wrote a simplistic program written in C for practical purposes so I can cope with the first basics. Here's the source chunk of code:
Code:
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <stdlib.h>

int main() {
     
     system("COLOR 3"); /* Colour value as Acqua */

     char *Message = "Programming in C language. This is for test purposes only.";
     printf("%s\n", Message);
     return 0;
}

Here comes the problems though. During the compilation process, the compiler keeps refusing to do the compilation and prints out the following issue as shown below.
Code:
"E:\Quincy\mingw\bin\gcc.exe" -std=iso9899:199409 -pedantic-errors-Wno-long-long
c:\users\XXXXX\desktop\text1.c: In function 'main':
c:\users\XXXXX\desktop\text1.c:8: error: ISO C90 forbids mixed declarations and code
Unsuccessful build

Thought at first that there's something wrong with the syntax or in run-time but I went wrong (excuse me for my ignorance ). Took an eye at this problem and it seems that it's not really caused by the code syntax but the C version standard itself. As the current searches say, the older versions of C had a VERY strict rule of not mixing declarations like the compiler says thus the present version of C within the compiler is old . Well, why I haven't thought about that...

So, does that mean I'm constrained to move to another compiler or just update the version library? There's a way somehow?

P.S.: By the way, I am using Quincy 2005 as the first compiler. Pretty old but still functional. usable to compile simple or normal C programs and kinda useful for me as a beginner. This software also contains an editor in its own. Will might change to another compiler (like GCC or others) in the future but I will stick with this one at the moment.
25.05.16 10:04:07 pm
Up
ohaz
User
Offline Off
Try this:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>

int main() {

     char *Message;
     
     system("COLOR 3"); /* Colour value as Acqua */

     Message = "Programming in C language. This is for test purposes only.";
     printf("%s\n", Message);
     return 0;
}


Basically, ISO C90 wants you to first declare all variables before writing any line of code that sets / uses them. It'll always look like this:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
type function(params){

// declare variables
int foo;
int bar;
char* buffer;

// set and use variables
foo = 5;
bar = foo;
buffer = "abc";

}
https://ohaz.engineer - Software Engineering
26.05.16 12:09:39 am
Up
Flacko
User
Offline Off
If you want to relax that restriction you should compile your program under a newer standard.

I see you're using mingw, which aims to be a port of GCC to Windows. To change the standard being used during compilation in GCC/MingW you have to change the -std flag passed to it somewhere in your IDE options (currently it is set to the C90 standard)
You could also try Clang which supports the same options as GCC and has prebuilt binaries for Windows.

Here's a list of supported values:
https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html
C99 & up allow mixing declarations and initialization, while the newest version is C11.
26.05.16 09:15:32 am
Up
Fraizeraust
Moderator
Offline Off
@user ohaz \ @user Flacko: First of, thank you both for an answer. I changed the version standard of C and everything seems quite normal without any problem. Yep, undoubtedly the first step is to declare the variables and then use them in the chunk but it isn't always the case for the latest versions. Good to know that. Will try also Clang compiler in the next time if possible.
To the start Previous 1 Next To the start