Thread: How Come?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212

    How Come?

    Code:
    #ifdef __WIN32__
    #define stuff printf("This is Windows")
    #define clear system("cls")
    #endif
    
    #ifdef __UNIX__
    #define stuff printf("This is UNIX")
    #define clear system("clear")  
    #endif
    
    #ifdef __LINUX__
    #define stuff printf("This is Linux")
    #define clear system("clear")  
    #endif
    
    #ifdef __MACH__
    #define stuff printf("This is a Mach")
    #endif
    
    #ifdef __APPLE__
    #define stuff printf("This is a Apple")
    #endif
    
    main(){
      printf("%s\n",stuff);
    }
    This works in Borland but not on gcc. How come?

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I'm not sure if the __UNIX__ and __LINUX__
    defines are right. It's compiler dependant anyways but most
    compilers have a way you can #define stuft by the command line
    so like you can do

    gcc file.c -DUNIX which defines UNIX.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    by dooing info gcc, you can read the documentation on target
    macros.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    im surprised that it runs on anything.....

    look at your macros...

    #ifdef __WIN32__
    #define stuff printf("This is Windows")
    #define clear system("cls")
    #endif

    main(){
    printf("%s\n",stuff);
    }

    now the printf statement will expand to :-

    printf("%s\n",printf("This is windows"));

    does that look right to you!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212

    Typo

    There was a typo.
    it should of been
    Code:
    #ifdef __WIN32__
    #define stuff "This is Windows"
    #define clear system("cls")
    #endif
    and the same for the rest. In linux in totally ignores this stuff.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Given your other typo's, did
    printf("%s\n",stuff);
    really have a \n on the end?

    If output doesn't end with a \n, you may not see the output, though it ought to flush stdout at program exit.

    Use
    gcc -E prog.c > prog.i
    to just run the pre-processor, to find out what all those #defines etc expand to.
    Is the result what you expect?
    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.

Popular pages Recent additions subscribe to a feed