Questions tagged [integer-promotion]

Anything related to C and C++ integer promotions, i.e. a class of data-type conversions that happens automatically when an object of integer type appears in certain contexts (e.g. when a value of type `short` is added to an `int` it is automatically promoted to `int` type before performing the operation).

In C and C++ integer promotion refers to automatic type conversions made between compatible integral types. When an operation is attempted between two compatible types (one can be safely converted into the other) any necessary adjustments are added silently by the compiler. This helps to avoid type casting where the programmer's intent is clear.

239 questions
0
votes
1 answer

Integer arithmetic overflow prevention in C

I thought the following code might cause an overflow since a * 65535 is larger than what unsigned short int can hold, but the result seems to be correct. Is there some built-in mechanism in C that stores intermediary arithmetic results in a larger…
0
votes
1 answer

int to char cast, strange behavior

Consider the following: unsigned int i1 = 0xFFFFFFFF; unsigned char c1 = i1; char c2 = i1; printf("%x\n", i1); // 0xFFFFFFFF as expected printf("%x\n", c1); // 0xFF as expected printf("%x\n", c2); // 0xFFFFFFFF…
0
votes
3 answers

Do unsigned integers get promoted to signed? What is the type of expression (uint16_t)-1 * (uint16_t)-1

For uint32_t and uint64_t results are expected, but promotions for uint8_t and uint16_t are strange. Tests on c++14/c++17, gcc and clang, 64-bit linux, sizeof(int) == 4. #include using namespace std; static_assert(…
user2622016
  • 6,060
  • 3
  • 32
  • 53
0
votes
3 answers

What is the type promotion rule for a parenthesized sub-expression?

Assume an integer expression comprised of multiple unsigned integral types uint16_t and uint32_t. The expression contains a parenthesized sub-expression, within which all elements are of type uint16_t. Should the elements inside the parenthesized…
ysap
  • 7,723
  • 7
  • 59
  • 122
0
votes
1 answer

How do you take the address of an inverted compound litteral in C?

I wasn't exactly exactly sure what title I should use for this question, but I'll try to clarify through an explanation: So the long and short of it is that I want a functions argument to accept both an array AND an integer for the same argument.…
Kalcifer
  • 1,211
  • 12
  • 18
0
votes
1 answer

Applying "not" operator on long double in C

I have below C code in which I have applied a not operator on a long double variable: #include int main() { long double a; signed char b; int arr[sizeof(!a+b)]; printf("\n%d",sizeof(arr)); return 0; } This code outputs…
Vipul Tyagi
  • 547
  • 3
  • 11
  • 29
0
votes
1 answer

SHRT_MIN in C as hex

I wanted to display the highest and lowest "short" values as hexadecimal numbers (using a Windows environment, mingw64 compiler): printf("largest %x and smallest %x",SHRT_MAX,SHRT_MIN) The output was as expecteded for the max number -> 7fff but for…
Bert
  • 57
  • 6
0
votes
2 answers

Using a long variable to store a char and printing it

I am storing a char in a long variable and trying to print it using printf. long a = 'A'; printf("%c \n",a); Considering default argument promotions the arguments get promoted to int but as long has higher rank than int it should not get promoted…
LocalHost
  • 910
  • 3
  • 8
  • 24
0
votes
3 answers

Negation operator on unsigned char

#include int main() { unsigned char a = 5; printf("%d\n",~a); printf("%d\n",a=~a); return 0; } According to the accepted answer here Negation inside printf , in the first printf, a should get promoted to an int and the…
Stupid Man
  • 885
  • 2
  • 14
  • 31
0
votes
1 answer

I am having problems compiling my Java Code.. I am presuming it is because of a casting/promotinng error in the second method "method1"

Terminal Java Code Hello everyone. I am having problems compiling my Java Code.. I am presuming it is because of a casting/promotinng error in the second method "method1". If anyone could spot the errors and let me know that would be great! Thank…
0
votes
2 answers

How to get output in integer instead of ASCII code value

I am executing this program Output is 104 in ASCII Code values. It is giving value in ASCII value but how can i get output in number void main() { char ch1 , ch2, sum; ch1 = '2'; ch2 = '6'; sum = ch1+ch2; printf("sum = %d ",…
0
votes
0 answers

Integer value promotion to unsigned int vs signed int in c

I have two c codes, the difference between them is that one uses char and one uses int, as follows, First: if case is true int x = -1; unsigned int y = 2; if(x>y) Second: the if case is false char x = -1; unsigned char y = 2; if(x>y) Why does…
Ahmad Anwar
  • 123
  • 1
  • 8
0
votes
1 answer

enum class - printf prints wrong value

I have this enum class: enum class myEnum : u8{ LEVEL_ERROR = 0, LEVEL_WARN = 50, LEVEL_DEBUG = 150, }; and at some point I making use of it (not exactly this way, but simply this is what happens): myEnum instance = 42; printf("My…
Michał
  • 691
  • 1
  • 5
  • 22
0
votes
0 answers

Are there any active proposals to change integral promotion rules?

As already discussed here integral promotion rules can cause headaches in some cases (especially for arithmetic operations between signed and unsigned types). At the end of this answer there is a link to a proposal. However that proposal is from…
Timo
  • 9,269
  • 2
  • 28
  • 58
0
votes
1 answer

Comparing results after integer promotion

Following situation: I have macros for storing the complement of a variable together with its original value within a structure. With another macro I want to check if the original value is equal to the complement of the stored complement value. But…
mbed_dev
  • 1,450
  • 16
  • 33