Unix and linux are reserved words?

Asked

Viewed 109 times

4

Even if the variables are not declared unix and linux and even without including anything in my teste.c compiling with the GCC on Linux have:

Compilation

gcc teste.c -o teste

Execution

./teste

Exit

Unix and Linux are worth 1

Code

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

Are words reserved from language? Because both are worth 1?

2 answers

3


Not reserved words but predefined macros when the compiler is running on a Linux/Unix environment.

Try, on the command line, to run the following command to list all 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

1989 ANSI C standard introduced rules that require a macro name to be started by two underscores or an underscore followed by a letter maisculas.

You can say that gcc by default "doesn’t respect" those same rules, meaning you can’t 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 his reply, you can change this behavior using the options:

gcc -std=c90 -pedantic
gcc -std=c99 -pedantic
gcc -std=c11 -pedantic

2

The gcc is a compiler for several languages.
https://gcc.gnu.org/onlinedocs/gcc-5.2.0/gcc/Standards.html#Standards

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

The way you called it he’s a compiler for "GNUC89".

In "GNUC89" the symbols unix and linux are defined as 1 in your implementation.

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

gcc -std=c11 -pedantic ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.