3 ms·
My head hurts. Explanation, please?
by troels 16y ago
My head hurts. Explanation, please?
- malkia 16y agoDunno about explanation, but experiment shows it to work at least with two preprocessors - Microsoft's and GCC: First make a file a.c that has in it: #define true false #define false true int A = true; int B = false; then from the command line, do (with the Microsoft compiler): D:\>cl -E a.c int A = true; int B = false; Now with gcc (I'm using cygwin's gcc): D:\>sh -c "gcc -E a.c" int A = true; int B = false; I've stripped the other stuff.
- shasta 16y agoThere's a rule in the preprocessor to prevent infinite recursion: only replace each symbol once. The first time it encounters 'true', it's replaced with 'false'. The first time it encounters 'false, it's replace with 'true'. It's already seen 'true', so it throws up its hands. The consequence of this rule is that cycles of #defines don't do anything. Edit: Well, that's only true if there aren't other symbols introduced during expansion. Here's a counterexample that prints 3: #include <stdio.h> int x = 1; #define x 1 + y #define y 1 + x int main(int argc, char *argv[]) { printf("%d\n",x); return 0; }
- troels 16y agoAh .. Makes sense. Thanks.
- deleted 16y ago[deleted]