Jakub
Jakub

Reputation: 699

Check max value from three variables by using preprocessor in C

I want to print the value of the variable, which has the highest value. I want to do this by using the preprocessor, but I don't know how to do this. It's easy to do this by using if-else, but how to do that by using preprocessor? This is my code

#include <stdio.h>
#define MAX(a,b,c)
#if a > (b && c)
#   define a
#endif
#if b > (a && c)
#   define b
#else
#   define c
#endif

int main (void)
{
    int wynik;
    int a = 6;
    int b = 13;
    int c = 9;
    wynik = MAX(a,b,c);
    printf("%i", wynik);

    return 0;
}

And this is the errors from my terminal

E:\skrypty_c>gcc -o Cwiczenie12_4.exe Cwiczenie12_4.c
Cwiczenie12_4.c: In function 'main':
Cwiczenie12_4.c:17:11: error: expected identifier or '(' before '=' token
     int c = 9;
           ^
Cwiczenie12_4.c:18:23: error: expected expression before ';' token
     wynik = MAX(a,b,c);

Upvotes: 0

Views: 2071

Answers (3)

Roberto Caboni
Roberto Caboni

Reputation: 7490

You cannot check the value of runtime variables through preprocessor conditionals. They can check only the values of preprocessor level symbols. For example

#define A 5

#if (A  > 3)
...
#endif

Looking at the way you used #define and #if, my guess is that you meant defining some sort of preprocessor level function having #define a similar role of def func (): in Python. That's not how it works.

  • #define A expr just replaces the symbol A with expression expr in the current source file before compilation. No occurrences of A, no substitutions
  • #define A(b,c) expr is like the previous one, but uses b,c like function parameters, and they are expanded in the replaced in expression expr using the values passed when the macro is called.
  • #if expr or #ifdef symbol or #ifndef symbol are ways to check the value or even the simple definition of symbols previously defined through #defines (or through compiler option -D) in order perform conditional compilation of whole sections of code. These sections are closed with #endif, and alternative sections in case of false conditions can be compiled using #else and #elif directives.

What you can do, and I suspect it was your actual purpose, is to define a macro finding the maximum value among its parameters:

#define MAX(a,b,c) \
    (((a>b) && (a>c))? a : ((b>a) && (b>c))? b : c ))

I used ternary operator to calculate the max value.

Note: the \ character allows to place a macro on multiple lines. It needs to be the last character of the line.

Upvotes: 1

0___________
0___________

Reputation: 67564

Preprocessor cannot be used for that. Preprocessor does not know anything about the c variables and sysntax.

You cant use any preprocesor conditional expressions in the #defines.

if you want to use macro to find max of 3 tokens you need to use C expressions for that as it will be evaluated runtime (or compile time), not during the preprocessing

#define MAX(a,b,c)  (a) > (b) ? ((a) > (c) ? (a) : ((c) > (b) ? c : (b))) : ((b) > (c) ? (b) : (c))

Upvotes: 3

bipll
bipll

Reputation: 11940

That's not how it works. Your max-of-three macro would be something like

#define MAX(a, b, c) ((a) <= (b)? (b) <= (c)? (c) : (b) : (a) <= (c)? (c) : (a))

but frankly, it would be much better as a function:

inline int max(int a, int b, int c) {
    if(a <= b) {
        if(b <= c) return c;
        return b;
    }
    return a <= c? c : a;
}

Upvotes: 4

Related Questions