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.

193 questions
0
votes
3 answers

Compiler warnings conversion

We are compiling using gcc with -Wconversion enabled. I get following warnings when I left shift result returned by isBitSet function below. warning: conversion to 'u_int16_t {aka short unsigned int}' from 'int' may alter its value…
Sirish
  • 7,948
  • 19
  • 61
  • 97
0
votes
1 answer

Is the promotion to unsigned done on the result or on each operand?

In the following: 2147483647U > -2147483647 - 1 it will evaluate to false because of the conversion/promotion to unsigned. My question is though how it will be promoted? Will the subtraction operation be done first and the result will be promoted to…
Cratylus
  • 49,824
  • 60
  • 195
  • 327
0
votes
1 answer

Can you please explain this—"the rules involving mixed types of operands do not apply to the shift operators"

I can't understand the line "The result of the shift has the same type as the thing that got shifted (after the integral promotions)" in the following extract from the C book by Mike Banahan (Section 2.8.2.3). Importantly, the rules involving…
Thokchom
  • 1,544
  • 3
  • 14
  • 30
0
votes
4 answers

How possibly can a unary +/- operator cause integral promotion in "-a" or "+a", 'a' being an arithmetic data type constant/variable?

This seemingly trivial line is taken from the C book my Mike Banahan & Brady (Section 2.8.8.2). I can understand how implicit promotion comes into play in expressions like c=a+b depending on the types of the operands, but I am unable to grasp how…
Thokchom
  • 1,544
  • 3
  • 14
  • 30
0
votes
1 answer

Why is "int sum=ch1+ch2+ch2" not giving overflow when right-side operands are character variables & result >255?

In this program I am attempting to assign the result of the addition of character variables to an integer variable.I have made sure that the size of the addition is greater than 255.So I expect an expression overflow on the right and even though the…
Jugni
  • 245
  • 5
  • 12
-1
votes
1 answer

Usual arithmetic conversions and Integer promotion

I'm trying to understand what's going on under the hood of c conversions, and different types promotions and comparing stuff and all of this. union myUnion{ int intVal; float floatVal;}; if (m.floatVal == m.intVal) { cout << "BINGO!"; } if…
M. Alex
  • 39
  • 1
  • 1
  • 4
-1
votes
2 answers

Is this Integer Promotion? How does it work?

I was just experimenting and I tried out two printf()s. unsigned char a = 1; a = ~a; printf("----> %x %x %x %x", ~a, a, ~a, ++a); This one gave the output ----> ffffff00 ff ffffff00 ff Next one was unsigned char a = 1; printf("----> %x %x %x %x",…
Tattu
  • 327
  • 1
  • 3
  • 14
-2
votes
4 answers

I learned that in C language char type ranges from -128 to 127, but it doesn't seem like that

This might be a very basic problem, but I couldn't manage to. Here is what I am working with. #include int main(void) { char c1, c2; int s; c1 = 128; c2 = -128; s = sizeof(char); printf("size of char: %d\n", s); …
-2
votes
1 answer

Output of Program

#include int main() { short int i=20; char c=97; printf("%d,%d,%d",sizeof(i),sizeof(c),sizeof(c+i)); return 0; } suppose given size of short int =2, char is 1 and of int is 4B Well If I'm running on machine it is giving 2,1,4 but ans is…
-2
votes
2 answers

8051 16 bit addition/multiplication result 16 bit instead of 32 bit

I have a problem. In this program the variable x should be set to 0x10000 but in both operations the result is 0. This is not the main program but a test to find the reason for the error. I am currently making a 64 bit multiplier with hex input. I…
-2
votes
1 answer

C++ template operator gives wrong result when subtracting a scalar from a vector

I am trying to make a template operator function for subtracting a scalar from a vector and return the result in a vector. The function below is what I have been able to come up with: // vector - scalar template auto…
bobster
  • 53
  • 4
-3
votes
2 answers

how integer is promoted to unsigned integer

#include void main() { int a = -1; unsigned int b =15; if(b==a) printf ("b is equal to a"); } The output is empty. negative integers are stored as 2's complement of same postive number .When a…
-6
votes
1 answer

What is the actual difference between widening conversion and numeric promotion in java?

I just figured out somewhere that there is a difference between widening conversion (implicit conversion) and numeric promotion (integer promotion). I checked out on the reliable websites, but couldn't find any specific difference for them. Is there…
1 2 3
12
13