How to define a macro to find max between two numbers in C

2 Answers

0 votes
#include <stdio.h>

#define max(A, B) ((A) > (B) ? (A) : (B))

int main(void)
{
    int a = 213, b = 971;    
    
    printf("%d\n", max(3, 7));
    printf("%d\n", max(7, 3));
    printf("%d\n", max(a, b));

    return 0;
}


 
 
/*
run:
    
7
7
971

*/

 



answered Nov 17, 2015 by avibootz
0 votes
#include <stdio.h>
 
#define max(A, B) ((A) > (B) ? (A) : (B))
 
int main(void)
{
    int a = 1, b = 2;    
     
    printf("%d\n", max(a++, b++));
 
    return 0;
}
 
 
  
  
/*
run:
     
3 wrong - the max is between 1 and 2
 
*/

 



answered Nov 19, 2015 by avibootz

Related questions

2 answers 153 views
153 views asked Jun 25, 2024 by avibootz
1 answer 126 views
2 answers 141 views
141 views asked Jun 25, 2024 by avibootz
1 answer 153 views
1 answer 121 views
121 views asked Apr 13, 2022 by avibootz
1 answer 162 views
162 views asked Dec 10, 2020 by avibootz
1 answer 173 views
173 views asked Feb 12, 2019 by avibootz
...