Why does the C preprocessor interpret the word "linux" as the constant "1"? -
why c preprocessor in gcc interpret word linux
(small letters) constant 1
?
test.c:
#include <stdio.h> int main(void) { int linux = 5; return 0; }
result of $ gcc -e test.c
(stop after preprocessing stage):
.... int main(void) { int 1 = 5; return 0; }
which -of course- yields error.
(btw: there no #define linux
in stdio.h file.)
in old days (pre-ansi), predefining symbols such unix
, vax
way allow code detect @ compile time system being compiled for. there no official language standard (beyond reference material @ of first edition of k&r), , c code of complexity typically complex maze of #ifdef
s allow differences between systems. these macro definitions set compiler itself, not defined in library header file. since there no real rules identifiers used implementation , reserved programmers, compiler writers felt free use simple names unix
, assumed programmers avoid using names own purposes.
the 1989 ansi c standard introduced rules restricting symbols implementation legally predefine. macro predefined compiler have name starting 2 underscores, or underscore followed uppercase letter, leaving programmers free use identifiers not matching pattern , not used in standard library.
as result, compiler predefines unix
or linux
non-conforming, since fail compile legal code uses int linux = 5;
.
as happens, gcc non-conforming default -- can made conform (reasonably well) right command-line options:
gcc -std=c90 -pedantic ... # or -std=c89 or -ansi gcc -std=c99 -pedantic gcc -std=c11 -pedantic
see the gcc manual more details.
gcc phasing out these definitions in future releases, shouldn't write code depends on them. if program needs know whether it's being compiled linux target or not can check whether __linux__
defined (assuming you're using gcc or compiler that's compatible it). see the gnu c preprocessor manual more information.
a largely irrelevant aside: "best 1 liner" winner of 1987 international obfuscated c code contest, david korn (yes, author of korn shell) took advantage of predefined unix
macro:
main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);}
it prints "unix"
, reasons have absolutely nothing spelling of macro name.
Comments
Post a Comment