vendredi 20 novembre 2020

Operations with return values in C

I'm new to C and trying to write a super basic program that compares 3 numbers and returns the biggest.

But when I try to print some text with if functions, it skips 2 if and goes to else. I might told it more complicated than it is. :)

OK, here is my code:

#include <stdio.h>

int a;
int b;
int c;

int max(int a, int b, int c) {
    
    if (a>b && a>c) {
        return a;
    }
    
    else if (b>a && b>c) {
        return b;
    }
    
    else {
        return c;
    }
}

int main()
{
    int d;
    d = max(44,8,16);
    
    if (d==a) {
        printf("a");
    }
    
    else if (d==b) {
        printf("b");
    }
    
    else {
        printf("c");
    }
}

It just shows me "c". Nothing else. What should I write in if functions in order to see "a", "b" and "c" characters according to return values in "max" function?

I want to keep max function as it is. So is it possible to do such a thing?

Thanks in advance..

Aucun commentaire:

Enregistrer un commentaire