Click here to Skip to main content
15,942,710 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please read this paragraph(from section 6.3.1.8 of the C99 standard):
operand comparision.
If both operands have the same type, then no further conversions are required.

"Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.

Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.

Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type. "


we see the word conversion in this in this paragraph.
my question is this conversion is accomplished at compile time or runtime?
Posted
Updated 4-Jun-12 10:29am
v3

1 solution

The analysis for the kind of conversion is clearly done at compile time. All static type analysis is done at compile time.

The actual conversion is done at runtime.

But the compiler usually tries to optimize, e.g. compile time constants are converted at compile time - no reason to have the conversion done at run time for these.

Cheers
Andi
 
Share this answer
 
v4
Comments
Reza Oruji 4-Jun-12 13:38pm    
thanks,but according to the third paragraph of my question, how compiler knows which operand should be converted(eg in the case of comparison of unsigned int and signed int ) at compile time?
Andreas Gieriet 4-Jun-12 13:57pm    
Read it carefully: integer does include all integral types, not only int.

1) both signed or both unsigned: convert to the "larger"
2) both same "size" but mixed signed/unsigned: convert signed to unsigned
3) if unsigned "smaller" than the signed and the signed can hold all unsigned values: convert to the signed one
4) if all fails, the unsigned type of the "larger" of the two is taken

Now we can create an example for all four cases:
1) op(int, int) --> op(int, int)
op(unsigned short, unsigned short) --> op(unsigned short, unsigned short)
op(short, int) --> op(int, int)
...
2) op(unsigned int, int) --> op(unsigned int, unsigned int)
...
3) op(int, unsigned short) --> op(int, int)
...
4) is there any' e.g. op(wchar, short) --> op(unsigned short, unsigned short)?

Cheers
Andi
Andreas Gieriet 4-Jun-12 14:03pm    
If it is a variable, the type is given. If it is a literal value, the type is also given, e.g. 123 is int, 123u is unsigned int etc. See the respective section in the specification. I.e. all literal values have an implicit type.
Cheers
Andi
nv3 4-Jun-12 13:53pm    
Well answered. +5.
Andreas Gieriet 4-Jun-12 13:59pm    
Thanks for your 5!
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900