integer-promotion

Integer promotion (MISRA C:2012 Rule 7.2)

不想你离开。 提交于 2020-01-04 05:32:28
问题 MISRA enforces the use of the U suffix for unsigned integer constants uint32_t the_answer = 0x42U; I feel the U is a bit boilerplate because the line is very understandable without it. So I am wondering how much this rule is important and if unsigned int x = 1 is truely a bad example of implicit integer promotion. 回答1: You are correct, the U in this specific example is superfluous as per an exception to Rule 10.3: "A non-negative integer constant expression of essentially signed type may be

Type of char multiply by another char

孤人 提交于 2019-12-31 04:07:47
问题 What is the type of the result of a multiplication of two chars in C/C++? unsigned char a = 70; unsigned char b = 58; cout << a*b << endl; // prints 4060, means no overflow cout << (unsigned int)(unsigned char)(a*b) << endl; // prints 220, means overflow I expect the result of multiplying two number of type T (e.g., char, short, int) becomes T. It seems it is int for char because sizeof(a*b) is 4. I wrote a simple function to check the size of the result of the multiplication: template<class

Is char default-promoted?

▼魔方 西西 提交于 2019-12-28 05:52:52
问题 This may be a silly question, but could someone please provide a standard reference for C++11 and C11: Is char default-promoted to int ? Here's a little background: Both C and C++ have notions of default argument promotion (C++11: 5.2.2/7; C11: 6.5.2.2/6). This entails that in the following call, the arguments are promoted: void f(int, ...); float a = 1; short int b = 2; char c = 'x'; f(0, a, b, c); For the function call, a is converted to double and b is converted to int . But what happens

In a C expression where unsigned int and signed int are present, which type will be promoted to what type?

断了今生、忘了曾经 提交于 2019-12-27 10:27:10
问题 I have a query about data type promotion rules in C language standard. The C99 says that: C integer promotions also require that "if an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int." My questions is in case of a C language expression where unsigned int and signed int are present, which type will be promoted to what type? E.g. int cannot represent all the values of the unsigned int (values larger than MAX_INT

Why must a short be converted to an int before arithmetic operations in C and C++?

余生长醉 提交于 2019-12-25 05:11:14
问题 From the answers I got from this question, it appears that C++ inherited this requirement for conversion of short into int when performing arithmetic operations from C. May I pick your brains as to why this was introduced in C in the first place? Why not just do these operations as short ? For example ( taken from dyp's suggestion in the comments ): short s = 1, t = 2 ; auto x = s + t ; x will have type of int . 回答1: If we look at the Rationale for International Standard—Programming Languages

Integer Overflow - Why not [duplicate]

こ雲淡風輕ζ 提交于 2019-12-23 07:27:37
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Addition of two chars produces int Given the following C++ code: unsigned char a = 200; unsigned char b = 100; unsigned char c = (a + b) / 2; The output is 150 as logically expected, however shouldn't there be an integer overflow in the expression (a + b) ? Obviously there must be an integer promotion to deal with the overflow here, or something else is happening that I cannot see. I was wondering if someone

What's the difference between integer promotions and integer conversions in C++

梦想的初衷 提交于 2019-12-21 16:55:23
问题 Section 4.5 of the C++ standard (integer promotion) talks about specific cases of converting integral types to types with a higher rank. Section 4.7 of the C++ standard (integral conversions) begins with (bullet 4.7.1): An rvalue of an integer type can be converted to an rvalue of another integer type. An rvalue of an enumeration type can be converted to an rvalue of an integer type. As far as I understand conversions described in 4.5 (maybe except for the bullet 4.5.3 (enums)) can be

Does multiplying unsigned short cause undefined behaviour?

限于喜欢 提交于 2019-12-21 10:46:36
问题 As a follow-up to "https://stackoverflow.com/questions/33732041/why-static-castunsigned-intushrt-maxushrt-max-yields-correct-value" I was asking myself if promoting all types (except some exceptions) with a lower rank than int to int to perform arithmetic operations might cause UB in some cases. e.g.: unsigned short a = 0xFFFF; unsigned short b = a*a; As unsigned short is promoted to int for arithmetic operations this would result in: unsigned short a = 0xFFFF; unsigned short b = (int)a*(int

Would making plain int 64-bit break a lot of reasonable code?

狂风中的少年 提交于 2019-12-21 07:07:12
问题 Until recently, I'd considered the decision by most systems implementors/vendors to keep plain int 32-bit even on 64-bit machines a sort of expedient wart. With modern C99 fixed-size types ( int32_t and uint32_t , etc.) the need for there to be a standard integer type of each size 8, 16, 32, and 64 mostly disappears, and it seems like int could just as well be made 64-bit. However, the biggest real consequence of the size of plain int in C comes from the fact that C essentially does not have

Usual arithmetic conversions in C : Whats the rationale behind this particular rule

我与影子孤独终老i 提交于 2019-12-20 05:13:51
问题 From k&R C First, if either operand is long double, the other is converted to long double. Otherwise, if either operand is double, the other is converted to double. Otherwise, if either operand is float, the other is converted to float. Otherwise, the integral promotions are performed on both operands ; ... This would mean below expression char a,b,c; c=a+b; is actually caculated as c = char((int)a+(int)b); What is the rationale behind this rule? Do these conversions happen if a, b and c were