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

Are chars automatically promoted in C expressions?

I made a statement to a colleague of mine, which was: "chars are automatically promoted to integers in C expressions, and that's fine for performance since CPUs work fastest with their natural word size. I believe char promotion behavior is stated…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
5
votes
1 answer

Operator precedence and automatic promotion (to avoid overflow)

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 =…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
5
votes
1 answer

C: 8x8 -> 16 bit multiply precision guaranteed by integer promotions?

I'm trying to figure out if the C Standard (C90, though I'm working off Derek Jones' annotated C99 book) guarantees that I will not lose precision multiplying two unsigned 8-bit values and storing to a 16-bit result. An example statement is as…
4
votes
3 answers

MISRA C:2004, error with bit shifting

I'm using IAR Workbench compiler with MISRA C:2004 checking on. The fragment is: #define UNS_32 unsigned int UNS_32 arg = 3U; UNS_32 converted_arg = (UNS_32) arg; /* Error line --> */ UNS_32 irq_source = (UNS_32)(1U << converted_arg); The MISRA…
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
4
votes
4 answers

How can I identify an integer promotion and a demotion in C?

I am learning C, and integer promotion and the term demotion is new to me. I have read in the C Standard (C17) about the C type conversions and the integer promotion, but I don't know how to identify an integer promotion, and I don't know anything…
Cblue X
  • 143
  • 6
4
votes
3 answers

Why do `(char)~0` and `(unsigned char)~0` return values of different widths?

I bumped into this while writing a program trying to print the constituent byte values of UTF-8 characters. This is the program that I wrote to test the various ~0 operations: #include int main() { printf("%x\n", (char)~0); //…
4
votes
1 answer

(Where) does the C standard define the result of adding/subtracting two booleans?

The C11 standard defines the _Bool type (6.2.5.2) as a standard unsigned integer type (6.2.5.6) and as I read the standard, _Bool is then also an arithmetic type (6.2.5.18 via 6.2.5.7 and 6.2.5.17). Furthermore, it is specified that for + and -…
nielsen
  • 5,641
  • 10
  • 27
4
votes
3 answers

Clarifications about unsigned type in C

Hi I'm currently learning C and there's something that I quite don't understand. First of all I was told that if I did this: unsigned int c2 = -1; printf("c2 = %u\n", c2); It would output 255, according to this table: But I get a weird result: c2…
MM1
  • 912
  • 1
  • 10
  • 28
4
votes
1 answer

Left shift operation on an unsigned 8 bit integer

I am trying to understand shift operators in C/C++, but they are giving me a tough time. I have an unsigned 8-bit integer initialized to a value, for the example, say 1. uint8_t x = 1; From my understanding, it is represented in the memory like…
Pankaj Mishra
  • 550
  • 6
  • 18
4
votes
1 answer

Is shifting the signed bit of a signed short undefined behaviour in C?

I have heard that shifting into signed bit of an integer, i.e. int test = INT_MAX; test = (test<<1) + 1; is undefined behaviour due to test being greater than INT_MAX. But will this behaviour be encountered in signed short variables, i.e. short…
4
votes
1 answer

char type bitwise operation fails in to int

I am try to bitwise operate in C. Here is my code: int main() { char c = 127; c <<= 1; if (c == 0xfe) { printf("yes"); } else { printf("No"); } return 0; } System console print No. What I expect to…
4
votes
2 answers

Does enumerator used in expression have the same type as underlying type of its enum?

What is the type of enumeration constant, when it is used outside unscoped enum definition? Consider following code: #include enum modes { begin = 0, end = 1 }; int main() { std::cout << std::boolalpha <<…
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
4
votes
2 answers

Does the order of multiplication variables of different data type cause different results?

Lets say I have 3 variables: a long, an int and a short. long l; int i; short s; long lsum; If this is a pure math, since multiplication has a commutative property, the order of these variables doesn't matter. l * i * s = i * s * l = s * i *…
Nguai al
  • 958
  • 5
  • 15
4
votes
2 answers

Why implicit conversion occurs for addition between unsigned char?

GCC warns this code: unsigned char i = 1; unsigned char j = 2; i += j; says: warning: conversion to 'unsigned char' from 'int' may alter its value [-Wconversion] i += j; ^ It seems that j is implicitly converted to int. Why the implicit…
equal-l2
  • 899
  • 7
  • 17
4
votes
2 answers

Pass-by-reference and integral promotion

Why isn't the compiler able to promote char to int& but has no problem when passing it by reference-to-const (char to int const&)? Example code: #include using namespace std; void func1(int & i) { cout << "int & " << i <<…