Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rules of implicit numeric type conversions

Tags:

c++

I have couple of questions about type conversions with signed and unsigned variables. Which type will be chosen in case like this signed int + unsigned int = ??? And what about cases when instead of variables i have constants


x1(unsigned int) = x1(unsigned int) - 0xFFFFFFFA

I have read something like constants will be unsigned ints unless it is explicitly written like 0xFFFFFFFA(UL).Then

1) unsigned int - unsigned int = unsigned int
2) unsigned int = unsigned int

And what if number with floating point

 x1(unsigned int) = x2(signed short int) + x3(unsigned int) + x4(unsigned short int) 
 * 0.1(float);  
 1)float * unsigned short int = float
 2)float + unsigned int = float 
 3)float + signed short int = float
 4)unsigned int = (unsigned int)float

Here i guess 'a' will be char

signed int = 'a' + signed short int - signed int 
1) 'a' + signed short int = int ???
2) int - signed int = int ???
3) signed int = (signed int) int ???

And one more

long double = signed int + wchar_t - unsigned int * 10 
1)unsigned int * 10(int) = int
2)wchar_t - int = int
3)signed int + int = int 
4)long double = (long double) int
like image 481
PAPAmidNIGHT Avatar asked Feb 12 '26 21:02

PAPAmidNIGHT


1 Answers

You can always use the trick I commented:

struct {} _ = some_expression;

The compiler error will tell you the exact type of the expression.

For backgrounders, see http://cppreference.com, specifically Implicit Conversion

  • Numeric Promotions

  • Numeric Conversions

You should also read conv.prom and conv.double in the standard.

like image 120
sehe Avatar answered Feb 16 '26 12:02

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!