unix and linux are reserved words?

4

Even though I do not declare the variables unix and linux and even not include anything in my teste.c compiling with GCC on Linux I have:

Compilation

gcc teste.c -o teste

Execution

./teste

Output

  

Unix and Linux are worth 1

Code

main()
{
        if(linux * unix)
                puts("Unix e Linux valem 1");
}

Are they reserved words of the language? Why are the two worth 1?

    
asked by anonymous 11.10.2015 / 03:18

2 answers

3

These are not reserved words but predefined macros when the compiler is running in a Linux / Unix environment.

At the command line, try the following command to list all of these macros:

gcc -dM < /dev/null

The two words linux and unix have the following definition:

gcc -dM < /dev/null | grep linux

#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1
#define linux 1

gcc -dM < /dev/null | grep unix 
#define __unix__ 1
#define __unix 1
#define unix 1

The ANSI C standard of 1989 introduced rules that require that the name of a macro be started by two underscores or by an underscore followed by a small letter.

You can say that gcc, by default, "does not respect" these same rules, meaning that you can not do something like

int main() {

   int unix = 10;

}

This will generate the following message:

error: expected identifier or '(' before numeric constant
  int unix = 10;

As @pmg indicated in your reply, you can change this behavior using the options:

gcc -std=c90 -pedantic
gcc -std=c99 -pedantic
gcc -std=c11 -pedantic
    
11.10.2015 / 10:06
2

gcc is a multi-language compiler.
link

  

By default, GCC provides some extensions to the C language that on rare occasions conflicts with the C standard. See Extensions to the C Language Family . ...

The way you called it is a compiler for "GNUC89".

In "GNUC89" the unix and linux symbols are set to 1 in your implementation.

To use gcc as a C11 compiler (the latest version of Standard), invoke it as well

gcc -std=c11 -pedantic ...
    
11.10.2015 / 10:06