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
1
vote
1 answer

Understanding getopt() example. Comparison of int to char

Hello all I hope you can help me understand why getopt used an int and the handling of the optopt variable in getopt. Pretty new to C++. Looking at getopt, optopt is defined as an…
Tommy
  • 591
  • 2
  • 11
  • 21
1
vote
0 answers

How to create a zero-overhead integer-like type that does not implicitly convert to other types?

We have a fairly sized C++ code base which uses signed 32-bit int as the default integer data type. Due to changing requirements, it is necessary that we switch to 64-bit long integers for a particular data structure. Changing the default integer…
1
vote
1 answer

Type of char multiply by another char

What is the type of the result of a multiplication of two chars in C/C++? unsigned char a = 70; unsigned char b = 58; cout << a*b << endl; // prints 4060, means no overflow cout << (unsigned int)(unsigned char)(a*b) << endl; // prints 220, means…
ebi
  • 501
  • 3
  • 12
1
vote
1 answer

C++ Arithmetic Type Conversion with unsigned & signed integers

Code: #include using std::cout; using std::endl; int main() { unsigned int i = 5; int x = -3; cout << "(i + x) = " << (i + x) << endl; cout << "Set x to -6" << endl; x = -6; cout << "(i + x) = " << (i + x) <<…
Feez
  • 43
  • 4
1
vote
1 answer

How to force integer promotion in C for expression long = short + short

Lets assume the int is the 16-bit type to prevent implicit conversion from short. long test_1(short x, short y) { return x + y; } What are C rules about promoting numbers in this example? How to protect from modulo arithmetic?
Michas
  • 8,534
  • 6
  • 38
  • 62
1
vote
1 answer

Promotion of integer in variadic functions

Why does the first f() call in the code below end up printing the -2 that I have passed to it as 4294967294 instead? #include #include #include #include void f(int64_t i, ...) { va_list ap; int64_t…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
1
vote
2 answers

integer promotion and unsigned interpretation

int_8 int8 = ~0; uint_16 uInt16 = (uint_16) int8; Regarding the typecast above; where in C standard can I find reference to an indication for the following behaviour? - sign extension to the larger type before the unsigned interpretation…
golark
  • 27
  • 7
1
vote
2 answers

uint16_t subtraction GCC compilation error

I have the following program #include #include #include int main(void) { uint16_t o = 100; uint32_t i1 = 30; uint32_t i2 = 20; o = (uint16_t) (o - (i1 - i2)); /*Case A*/ o -= (uint16_t) (i1 -…
Mohamed
  • 265
  • 3
  • 6
1
vote
3 answers

Char and int16 array element both shown as 32bit hex?

In the example below: int main(int argc, char *argv[]) { int16_t array1[] = {0xffff,0xffff,0xffff,0xffff}; char array2[] = {0xff,0xff,0xff,0xff}; printf("Char size: %d \nint16_t size: %d \n", sizeof(char), sizeof(int16_t)); if…
TheMeaningfulEngineer
  • 15,679
  • 27
  • 85
  • 143
1
vote
1 answer

If I want to promote a char to an int, should I use static_cast(char variable) or +(char variable) and why?

This question is a little subjective, but I believe it may lead to some constructive answers. Assume I have char x; and I want to promote it to an integral so I can access it's numeric value. Which should I prefer to use and why? static_cast or…
Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
1
vote
1 answer

How does Objective-C handle bitmasking when arguments are not the same size?

When coding in Objective-C, what should the compiler do when it bitwise ANDs (or performs other bitwise operations) on two variables of different size? For example, lets say I have some variables as follows: unsigned long longVar =…
1
vote
4 answers

ones complement operator with short int

#include #include main() { short int x=0x55AA; printf("%x ",~x); } Above program gives output : ffffaa55. I was expecting o/p only aa55 as short int is 2 bytes. Can anyone please explain it?
debonair
  • 2,505
  • 4
  • 33
  • 73
1
vote
0 answers

Why does OpenWatcom issue: Warning! W124: Comparison result always 0

With OpenWatcom we (a colleague and myself) receive the following warning: Warning! W124: Comparison result always 0 The code we have is of course part of a larger project, but the piece of code that results in a warning is equivalent to…
0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
1
vote
1 answer

Compiler independent expression arithmetic conversions and integer promotions

Assume we have an assignment using variables of the following types: uint64 = uint16 + uint16 + uint32 + uint64 Assume we know that the resulting r-value fits inside a uint64 as long as all the work is done using uint64's. Will the compiler…
mcorley
  • 767
  • 12
  • 21
0
votes
2 answers

The width of int data type on a machine

Is it right to say that the width of int data type depends on the data width of the ALU ? For example is it right to say that a 32-bit processor will have int data type as 32-bit wide? Similarly for 16 bit and 8 bit(please note that C guarantees…
bubble
  • 3,408
  • 5
  • 29
  • 51