0

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);
1
  • #define requires a single "line", and cannot include other #-style pre-processor directives like this. Commented Apr 8, 2020 at 18:18

3 Answers 3

4

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

The marginal advantage of the macro is it works on non-int values.
3

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))

Comments

1

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.

9 Comments

What's this a > (b && c)? Did you mean a > b && a > c?
@WeatherVane as I explained I have no idea. it is just the expression used by OP in those #ifs
@it checks if a is greater than 1 or 0 depending of the values of the b and c (sometimes their parts)
Considering it is supposed to find the maximum of three values...
@WeatherVane it is not the answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.