integer-promotion

C++ Unexpected Integer Promotion

筅森魡賤 提交于 2019-12-19 17:46:58
问题 I was writing some code recently that was actually supposed to test other code, and I stumbled upon a surprising case of integer promotion. Here's the minimal testcase: #include <cstdint> #include <limits> int main() { std::uint8_t a, b; a = std::numeric_limits<std::uint8_t>::max(); b = a; a = a + 1; if (a != b + 1) return 1; else return 0; } Surprisingly this program returns 1. Some debugging and a hunch revealed that b + 1 in the conditional was actually returning 256, while a + 1 in

If char c = 0x80, why does printf(“%d\n”, c << 1) output -256?

我怕爱的太早我们不能终老 提交于 2019-12-19 03:26:16
问题 #include<stdio.h> int main(void) { char c = 0x80; printf("%d\n", c << 1); return 0; } The output is -256 in this case. If I write c << 0 then the output is -128 . I don't understand the logic behind this code. 回答1: Already your starting point is problematic: char c = 0x80; If (as seemingly in your case) char is a signed type, you are assigning the integer constant 128 to a type that is only guaranteed to hold values up to 127 . Your compiler then may choose to give you some implementation

type promotion in C

倾然丶 夕夏残阳落幕 提交于 2019-12-19 02:46:13
问题 I am quite confused by the following code: #include <stdio.h> #include <stdint.h> int main(int argc, char ** argv) { uint16_t a = 413; uint16_t b = 64948; fprintf(stdout, "%u\n", (a - b)); fprintf(stdout, "%u\n", ((uint16_t) (a - b))); return 0; } That returns: $ gcc -Wall test.c -o test $ ./test 4294902761 1001 $ It seems that expression (a - b) has type uint32_t. I don't uderstand why since both operators are uint16_t. Can anyone explain this to me? 回答1: The C standard explains this quite

Why are integer types promoted during addition in C?

孤者浪人 提交于 2019-12-17 16:44:13
问题 So we had a field issue, and after days of debugging, narrowed down the problem to this particular bit of code, where the processing in a while loop wasn't happening : // heavily redacted code // numberA and numberB are both of uint16_t // Important stuff happens in that while loop while ( numberA + 1 == numberB ) { // some processing } This had run fine, until we hit the uint16 limit of 65535. Another bunch of print statements later, we discovered that numberA + 1 had a value of 65536 ,

How do promotion rules work when the signedness on either side of a binary operator differ? [duplicate]

£可爱£侵袭症+ 提交于 2019-12-17 00:05:53
问题 This question already has answers here : Implicit type conversion rules in C++ operators (9 answers) Closed last year . Consider the following programs: // http://ideone.com/4I0dT #include <limits> #include <iostream> int main() { int max = std::numeric_limits<int>::max(); unsigned int one = 1; unsigned int result = max + one; std::cout << result; } and // http://ideone.com/UBuFZ #include <limits> #include <iostream> int main() { unsigned int us = 42; int neg = -43; int result = us + neg; std

How do promotion rules work when the signedness on either side of a binary operator differ? [duplicate]

假装没事ソ 提交于 2019-12-17 00:05:31
问题 This question already has answers here : Implicit type conversion rules in C++ operators (9 answers) Closed last year . Consider the following programs: // http://ideone.com/4I0dT #include <limits> #include <iostream> int main() { int max = std::numeric_limits<int>::max(); unsigned int one = 1; unsigned int result = max + one; std::cout << result; } and // http://ideone.com/UBuFZ #include <limits> #include <iostream> int main() { unsigned int us = 42; int neg = -43; int result = us + neg; std

Arithmetic conversion VS integral promotion

余生颓废 提交于 2019-12-10 23:10:11
问题 char cval; short sval; long lval; sval + cval; // sval and cval promoted to int cval + lval; // cval converted to long This is a piece of code on C++ Primer. I know sval+cval generates an int type according to convert the small integral types to a larger integral type. The types bool, char, signed char, unsigned char, short, and unsigned short are promoted to int if all possible values of that type fit in an int. But for the last one I couldn't understand why it uses "converted". Why is cval

What happens when a integer overflow occurs in a C expression?

牧云@^-^@ 提交于 2019-12-10 16:52:33
问题 I have the following C code: uint8_t firstValue = 111; uint8_t secondValue = 145; uint16_t temp = firstValue + secondValue; if (temp > 0xFF) { return true; } return false; This is the alternative implementation: uint8_t firstValue = 111; uint8_t secondValue = 145; if (firstValue + secondValue > 0xFF) { return true; } return false; The first example is obvious, the uint16_t type is big enough to contain the result. When I tried the second example with the clang compiler on OS/X, it correctly

Why does C/C++ automatically convert char/wchar_t/short/bool/enum types to int?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 12:49:23
问题 So, if I understood it well, integral promotion provides that: char, wchar_t, bool, enum, short types ALWAYS are converted to int (or unsigned int ). Then, if there are different types in an expression, further conversion will be applied. Am I understanding this well? And if yes, then my question: Why is it good? Why? Don't become char/wchar_t/bool/enum/short unnecessary? I mean for example: char c1; char c2; c1 = c2; As I described before, char ALWAYS is converted to int , so in this case

Operator precedence and automatic promotion (to avoid overflow)

匆匆过客 提交于 2019-12-10 06:49:55
问题 Finding the size of some data in bytes is a common operation. Contrived example: char *buffer_size(int x, int y, int chan_count, int chan_size) { size_t buf_size = x * y * chan_count * chan_size; /* <-- this may overflow! */ char *buf = malloc(buf_size); return buf; } The obvious error here is the ints will overflow (an 23171x23171 RGBA byte buffer for eg). What are the rules for promotion when multiplying 3 or more values? (Multiplying a pair of values is simple) We could play it safe and