Thread: very simple preprocessor

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    22

    very simple preprocessor

    Can anyone tell me why i cant print out the max number? What am i missing? And yes, it has to be defined outside of main. thanks.

    Code:
    #include <stdio.h>
    
    
    #define MAX(x,y) ((x)>(y)?(x):(y))
    
    int main(void)
    {
        int x=5,y=3;
        printf("max=", MAX(x,y));
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    main.c||In function 'main':|
    main.c|9|warning: too many arguments for format|
    ||=== Build finished: 0 errors, 1 warnings ===|
    Something is missing in here:

    Code:
    printf("max=", MAX(x,y));

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:9:5: warning: too many arguments for format [-Wformat-extra-args]
    You're missing a %d format.
    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.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Just for the future be very cautious when you use something like this
    Code:
    #include <stdio.h>
    
    
    #define MAX(x,y) ((x)>(y)?(x):(y))
    
    int main(void)
    {
        int x=5,y=3;
        printf("max=%d\n", MAX(x++,y));
    
    
        return 0;
    }
    It will give me output 6
    Code:
    #include <stdio.h>
    
    
    #define MAX(x,y) ((x)>(y)?(x):(y))
    
    int main(void)
    {
        int x=5,y=3;
        printf("max=%d\n", MAX(++x,y));
    
    
        return 0;
    }
    It will give me output 7

    This happens because what is in the place of x ( in line where you have the #define ) will be executed too. Same for y.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POI preprocessor ?
    By securelinprog in forum C Programming
    Replies: 2
    Last Post: 02-23-2010, 12:12 AM
  2. Preprocessor help
    By royen in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2009, 10:56 AM
  3. preprocessor help
    By brunogon in forum C Programming
    Replies: 13
    Last Post: 06-16-2008, 11:14 AM
  4. preprocessor!!
    By nishu1988 in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2007, 11:31 PM
  5. What is Preprocessor...?
    By gardenair in forum C Programming
    Replies: 2
    Last Post: 04-09-2003, 07:52 PM