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
3
votes
2 answers

Why does combining two shifts of a uint8_t produce a different result?

Could someone explain me why: x = x << 1; x = x >> 1; and: x = (x << 1) >> 1; produce different answers in C? x is a *uint8_t* type (unsigned 1-byte long integer). For example when I pass it 128 (10000000) in the first case it returns 0 (as…
ivanibash
  • 1,461
  • 2
  • 14
  • 37
3
votes
1 answer

Bitwise negation of unsigned char

This is a question relating the c99 standard and concerns integer promotions and bitwise negations of unsigned char. In section 6.5.3.3 it states that: The integer promotions are performed on the operand, and the result has the promoted type. If…
Kenneth
  • 1,167
  • 1
  • 13
  • 26
3
votes
5 answers

Widening of integral types?

Imagine you have this function: void foo(long l) { /* do something with l */} Now you call it like so at the call site: foo(65); // here 65 is of type int Why, (technically) when you specify in the declaration of your function that you are…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
3
votes
3 answers

Integer promotion for `long` and `size_t` when sent through ellipsis?

View this question from the perspective of someone implementing printf. Since the arguments of printf are passed through an ellipsis (...), they get integer promoted. I know that char, short and int get promoted to int while long long does not get…
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
3
votes
1 answer

GCC, weird integer promotion scheme

I'm working with GCC v4.4.5 and I've notived a default integer promotion scheme I didn't expected. To activate enough warnings to prevent implicit bugs I activated the option -Wconversion and since then I've notice that when I perform the code…
A.G.
  • 1,279
  • 1
  • 11
  • 28
2
votes
3 answers

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

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 …
Hari
  • 153
  • 8
2
votes
3 answers

a |= (1 << 31) results in unexepected value

I made a reproducible code as below/ #include int main(void) { long int a = 0x0; a |= (1 << 31); printf("a: 0x%lx\n", a); } I expect 'a' should be 0x0000000080000000. but the value is given as a: 0xffffffff80000000. Why…
2
votes
2 answers

Question about Bitwise Shift in Microsoft C++

I am doing the following bitwise shift in Microsoft C++: uint8_t arr[3] = {255, 255, 255}; uint8_t value = (arr[1] << 4) >> 4; The result of these operations confused me quite a bit: value = 255 However, if I do the bitwise shift separately: value…
2
votes
2 answers

comparison between signed and unsigned char

I'm pretty much assuming this is a stupid question... but I can't really find the answer for it. So I'm asking this here. For the purpose of learning about implicit type casting, I'm running the following code on C. #include int main() { …
2
votes
2 answers

Promotions and conversions of variables in printf()

In this case, #include int main() { unsigned char a = 1; printf("%hhu", -a); return 0; } The argument -a in printf is promoted to int by the integer promotion by the unary minus operator and subsequently promoted by the…
op ol
  • 705
  • 4
  • 11
2
votes
3 answers

Is it UB to give a char argument to printf where printf expects a int?

Do I understand the standard correctly that this program cause UB: #include int main(void) { char a = 'A'; printf("%c\n", a); return 0; } When it is executed on a system where sizeof(int)==1 && CHAR_MIN==0? Because if a is…
2
votes
3 answers

Why is it more safe to place sizeof in malloc first?

What is the difference between int *p = malloc( h * w * sizeof(*p) ); and int *p = malloc( sizeof (*p) * h * w ); when h and w are of type int? Why is it more safe to set sizeof(*p) at the first than at the last place in malloc? I understood…
2
votes
1 answer

Default argument promotions according to C standards

I was reading C standard for default argument promotions and got confused over many points. This question shows all the paragraphs that i have doubt on in a proper way. First of all in Paragraph 6 point 3, it says if the prototype ends with…
LocalHost
  • 910
  • 3
  • 8
  • 24
2
votes
3 answers

Question on Usual Arithmetic Conversions - GCC Compiler

I'm trying to understand implicit datatype conversions in C. I thought that I had understood this topic, but yet the following code example is still confusing me. Specifically, I have read about Usual Arithmetic Conversions and Integer Promotion…
2
votes
2 answers

Size of unsigned char is 8 bits but why can I shift it (left or right) by or upto 31 bits?

So here is my code #include int main(void) { unsigned char ch = 244; ch = ch << 31; return 0; } I can shift ch upto 31 bits that's means ch is of 32 bits but how ? sizeof(ch) is also…
Golu
  • 350
  • 2
  • 14