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
9
votes
4 answers

Pointer arithmetic and integral promotion

In the expression p + a where p is a pointer type and a is an integer, will integer promotion rules apply? For example, if a is a char, on a 64-bit machine it will surely be extended to 64 bit before being added to the pointer value (in the compiled…
Siyuan Ren
  • 7,573
  • 6
  • 47
  • 61
9
votes
3 answers

Do integer promotions always happen when evaluating an expression?

Possible Duplicate: C integer overflow behaviour when assigning to larger-width integers I haven't found a clear answer on this in my googling. Say you have two expressions: int16_t a16 = 100; int16_t b16 = 2000; int16_t result16 = (a16 * b16) /…
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
8
votes
6 answers

Why do a bitwise-and of a character with 0xff?

I am reading some code that implements a simple parser. A function named scan breaks up a line into tokens. scan has a static variable bp that is assigned the line to be tokenized. Following the assignment, the whitespace is skipped over. See below.…
Roger Costello
  • 3,007
  • 1
  • 22
  • 43
8
votes
1 answer

How to avoid integral promotion for bitwise operations

I'm floored that VisualStudio 2015 insists on promoting a WORD (unsigned short) to an unsigned int when only WORD values are involved in only bit manipulations. (i.e. promotes 16 bit to 32 bit when doing 16bit | 16bit). e.g. // where WORD is a…
Mordachai
  • 9,412
  • 6
  • 60
  • 112
8
votes
4 answers

What is the difference between literals and variables in C (signed vs unsigned short ints)?

I have seen the following code in the book Computer Systems: A Programmer's Perspective, 2/E. This works well and creates the desired output. The output can be explained by the difference of signed and unsigned representations. #include int…
8
votes
6 answers

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

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…
user2148758
  • 335
  • 1
  • 2
  • 10
8
votes
2 answers

Is unsigned char always promoted to int?

Suppose the following: unsigned char foo = 3; unsigned char bar = 5; unsigned int shmoo = foo + bar; Are foo and bar values guaranteed to be promoted to int values for the evaluation of the expression foo + bar -- or are implementations allowed to…
Vilhelm Gray
  • 11,516
  • 10
  • 61
  • 114
8
votes
3 answers

Integer promotion - what are the steps

This code prints B2 short a=-5; unsigned short b=-5u; if(a==b) printf("A1"); else printf("B2"); I read about integer promotion but it's still unclear to me, how does it work in the example here? Can someone thoroughly post the steps the…
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108
7
votes
3 answers

type promotion in C

I am quite confused by the following code: #include #include 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 -…
Julien REINAULD
  • 599
  • 2
  • 5
  • 18
7
votes
3 answers

Does uint16_t get promoted to int? And is it safe to typecast uint16_t to uint32_t?

uint32_t a = 10; uint16_t b = 0; if (a > b) { printf("a > b\n"); } I was expecting that b would get promoted to int and the compiler would complain about comparing signed and unsigned. But the compiler did not raise any warning. When I…
Karthick
  • 1,010
  • 1
  • 8
  • 24
7
votes
1 answer

Is relying on integer promotion a bad programming practice?

I'm currently writing some code for embedded systems (both in c and c++) and in trying to minimize memory use I've noticed that I used a lot of code that relies on integer promotions. For example (to my knowledge this code is identical in c and…
TomK
  • 47
  • 6
7
votes
2 answers

Behavior of integer promotion during bitwise operations

When one performs a bitwise operation on an arithmetic type smaller than int, it is automatically promoted to int. std::uint8_t a = 42; auto b = a | 0x0f; // b will be of type int What I haven't been able to determine is what exactly goes on during…
7
votes
2 answers

C++ Unexpected Integer Promotion

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 #include int main() { std::uint8_t a, b; a…
codesmith
  • 561
  • 1
  • 3
  • 17
7
votes
2 answers

Integral promotion and operator+=

I need to eliminate gcc -Wconversion warnings. For example typedef unsigned short uint16_t; uint16_t a = 1; uint16_t b = 2; b += a; gives warning: conversion to 'uint16_t {aka short unsigned int}' from 'int' may alter its value [-Wconversion] …
bencemeszaros
  • 596
  • 4
  • 13
7
votes
2 answers

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

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…
user283145