lundi 27 juillet 2020

How is an int function able to return long long variable?

int reverse(int x) {
        if(x==0)
            return 0;
        int y = abs(x);
        long long result = 0;
        while(y > 0) {
            result *= 10;
            result += y % 10;
            y /= 10;
        }
        if(result > INT_MAX || result < INT_MIN)
            return 0;
        if(x<0) {
            return -result;
        }
        return result;
        
    }

How is this code valid? It's clear that result is a long long variable but the return type of the code is int. The code still works.

Aucun commentaire:

Enregistrer un commentaire