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
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
  • 255
  • 5
  • 12
-1
votes
1 answer

Not able to compile code with unsigned and signed overloads of a function

I was trying to write an overloaded function to accept both signed and unsigned integers. Following is my code: #include void fun(const long long a) { std::cout << "Signed: " << a << std::endl; } void fun(const unsigned long long…
-1
votes
1 answer

Difference between the num as char and as int on memory - Regarding the equivalent num on the ascii -

What's the Difference between the number as char and the number as int (or any type, which I can make any arithmetic operation using it like double on c++) on memory - Regarding the equivalent number on the ascii code -. Also, how ('5'-'0') can help…
-1
votes
2 answers

What is wrong with this code? The answer should be 24 for this question right?

What is wrong with this code? The answer should be 24 for this question right? int t; vectorarr={24,434}; for(int i=arr.size()-1;i>=(arr.size()-2);i--) { t=i; } cout<
-1
votes
3 answers

C language printf function and output problem

I need help solving this problem in my mind, so if anyone had a similar problem it would help me a lot. signed char c=0x10; printf("%x", c<<0x4|c>>0x4); Why output is 101?
-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
  • 49
  • 1
  • 1
  • 5
-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
  • 15
-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); …
agongji
  • 117
  • 1
  • 7
-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
  • 5
-3
votes
2 answers

Why is it not allowed to assign double literal to variable of type float?

In java, the compiler throws an error if you try to assign a double literal to an int variable. However, it allows the assignment of an int literal to variable of type long. double d = 4.0; float f = d; // this is not allowed. 4.0 should be…
kaka
  • 597
  • 3
  • 5
  • 16
-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…
-5
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
15
16