macro problem

This is a discussion on macro problem within the C Programming forums, part of the General Programming Boards category; Hi, I have a macro that tests for the largest of two numbers: Code: Largest(x,y) ( ((x) > (y))? (x): ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    macro problem

    Hi,
    I have a macro that tests for the largest of two numbers:

    Code:
    Largest(x,y) ( ((x) > (y))? (x): (y) )
    How would I code it to test for three?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    
    #define Largest(x,y) ( ((x) > (y))? (x): (y) )
    
    int main(void)
    {
       int x = 2, y = 1, z = 3;
       int largest = Largest(x,Largest(y,z));
       printf("largest = %d\n", largest);
       return 0;
    }
    
    /* my output
    largest = 3
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    could you walk me through whats happening there?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    largest is being assigned the (largest of x and (the largest of y and z)).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The result of the inner macro call are called against the third value. Since the first (inner) result is the larger of those two, it works fine when it's called again, giving you the result of that value compared to a third value.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    doesnt z have to be in the macro definition?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Does it look like it?

    A macro is a text substitution; what appears to be code is automagically replaced by the replacement text...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Code:
    int largest = Largest(x,Largest(y,z));
    so, the inner Largest finds the larger of y and z, which is z, then the outer Largest takes that and compares it to x to find the final result?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Sometimes it can be helpful to examine the preprocessor output.
    Code:
    int main(void)
    {
    int x = 2, y = 1, z = 3;
    int largest = ( ((x) > (( ((y) > (z))? (y): (z) )))? (x): (( ((y) > (z))? (y): (z) )) );
    printf("largest = %d\n", largest);
    return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Thats scary. Ive never looked at that stuff before. Thats whats being processed behind the scenes? Is that opened as a file in the project folder or can you see it through a command?

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Most compilers allow some option to output the work of the preprocessor. This was an editing of the uglier output with BC55.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Im using visual studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in macro
    By Bargi in forum C Programming
    Replies: 17
    Last Post: 02-04-2009, 09:17 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21