Thread: #ifndef explanation?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    #ifndef explanation?

    I was wondering how #ifndef worked. to me, the following should compile. (i get syntax errors) any hints are greatly appreciated!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #ifndef a
    #define a int
    #endif
    
    #ifndef b
    #define b int
    #endif
    
    #ifndef LT
    #define LT (a,b)(a<b)
    #endif
    
    main() {
    	int i;
    
    	while(i = 0; LT( i, 9); i++; )
    		printf("Compiled/n");
    //or
                    LT( 5, 9){
    		printf("Compiled/n");
    	}
    }
    my reference manual claims "if true, the code that is between one of the preceding and an #endif will be compiled"

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    gcc -E prog.c

    Then open prog.i in your editor, and see what the pre-processor has turned your code into.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, while doesn't take three things in a loop guard, but for does.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Linked Lists
    By Bleu_Cheese in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 09:17 PM
  3. Warning on an ifndef directive
    By DavidP in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2007, 01:31 AM
  4. #ifndef
    By paperbox005 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 07:25 PM
  5. ifndef
    By mayfda in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2003, 03:29 PM