type of this expression- (n(int)>0)?f(float):n(int)

I read this in Ritchie that the type of following expression is float regardless of whether n is positive.
I don't understand why. If n > 0 shouldn't just the second expression be evaluated.

f is a float and n is an int
 
  (n>0) ? f : n
Two reasons:
1. Types are determined at compile time. n only has a value at run time.
2. When performing operation between operands of different sizes, all operands are converted to the largest or more inclusive (so to speak) type.

If you want to convince yourself:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

void what_is_it(float){
    std::cout <<"It's a float.\n";
}

void what_is_it(int){
    std::cout <<"It's an int.\n";
}

int main(){
    int n;
    std::cin >>n;
    float f = 4;
    what_is_it(n > 0 ? f : n);
}
Topic archived. No new replies allowed.