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

Reading a signed char as unsigned - Type Promotion

Consider this little program: #include int main() { char c = 0xFF; printf("%d\n", c); return 0; } Its output is -1, as expected (considering char is signed in my system). What I'm trying to do is to make it print 255. This is…
sidyll
  • 57,726
  • 14
  • 108
  • 151
1
vote
2 answers

Fixed point arithmetic long long int representation issue in C

I am struggling with the realization of a signed long long int variable having the value 1 set in its integer part. Looking at the 16.16 implementation with a signed long int variable it does work like this: static signed long int varInit = 1 << 16;…
kvirk
  • 97
  • 1
  • 10
1
vote
2 answers

Why the sequence from the bitwise operator(~) would be this? Is that broken?

#include #include int main() { unsigned char a=100,b=50; printf("%d & %d = %d\n",a,b,a&b); printf("%d | %d = %d\n",a,b,a|b); printf("%d ^ %d = %d\n",a,b,a^b); printf(" ~%d = %d\n",a, ~a); /*the out come of this line…
Lucas
  • 25
  • 6
1
vote
5 answers

What does *= do?

Hey I am kinda new to C and I wanted to ask why this prints out 4 instead of 260? #include int main() { unsigned char x = 130; x *= 2; printf("%d\n", x); }
Alpha
  • 319
  • 2
  • 12
1
vote
3 answers

Why does function return bool when it is defined as an int?

Why does the int hum2() function returning a boolean type? Shouldn't the function return the type I defined it as. Like Float function returns float value of Double function returns a double value. #include int hum2() { return…
1
vote
5 answers

Is there a way to prove integral promotion to int?

In pure ansi C, is there any way to show that, given char c1 = 1, c2 = 2; the type of the following: c1 + c2 is int? Thanks. NOTE: I know that according to standards it is, but in C++ you can use the typeid operator to show it. I'd like to be able…
Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60
1
vote
1 answer

C++ primer enums and implicit conversion

Hello I have this from C++ primer 5th edition: // unscoped enumeration; the underlying type is machine dependent enum Tokens {INLINE = 128, VIRTUAL = 129}; void newf(unsigned char); void newf(int); unsigned char uc = VIRTUAL; newf(VIRTUAL); //…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
1
vote
2 answers

Using smallest necessary C type vs just using int

Given a C function (random example): void* getMyStuff( size_t size, unsigned char alignment ); Does using unsigned char help the compiler by telling it the limit of the values being passed, or does it hinder the compiler by preventing it from using…
Nairou
  • 3,585
  • 5
  • 29
  • 47
1
vote
2 answers

Unsigned integer overflow in comparison expressions

In the C language, with this code snippet: uint16_t a = 243; uint16_t b = 65535; uint16_t max = 65535; if ((a + b) > max) { printf("Out of range!"); } else { printf("Within range."); } The result will be "Out of range!". What type conversion…
1
vote
1 answer

format %lu expects argument of type 'long unsigned int' but argument 3 has type long long unsigned int, cast from printer to integer of different size

I am receiving warnings when compiling my code. These warnings include: format %lu expects argument of type 'long unsigned int' but argument 3 has type long long unsigned int & cast from printer to integer of different size. I don't seem to know…
azwahd180
  • 21
  • 1
  • 2
1
vote
1 answer

Integer promotion between unsigned and int

Here std::string s{ "aa" }; s.length() - 3; // == very large number From memory, C's integer promotion rules (idk about C++) produce a type wide enough to fit the result of the calculation and favour unsigned types (for the extra bit of width).…
Vorac
  • 8,726
  • 11
  • 58
  • 101
1
vote
2 answers

IA32 Assembly code for type casting from signed/unsigned char to unsigned/signed int

This is from Computer Systems, a Programmer's perspective (2nd edition) In question 3.4, the student is asked to determine the assembly instruction needed to cast from a source data type to a destination that is referred to by a pointer This answer…
mooglin
  • 500
  • 5
  • 17
1
vote
1 answer

Unsigned and Signed in same expression: What rules apply?

I'm confused by the issue stated in the title. I've been told that in expressions involving both types of variables, signed are converted to/interpreted as unsigned. However, as the following code snippet shows, that isn't always the case. Code:…
Edward Garemo
  • 434
  • 3
  • 13
1
vote
1 answer

Why does the ternary operator turn (... ? int : unsigned long long) into int when an implicit conversion is involved?

When I run #include #include #include struct S { operator unsigned long long() const { return 3ULL << 30; } }; int main() { printf("%s, 0x%llX, %d\n", typeid(true ? S() : 0).name(), …
user541686
  • 205,094
  • 128
  • 528
  • 886
1
vote
2 answers

C++ template function to compare any unsigned and signed integers

I would like to implement a template function that compares two variables of two types (T1 and T2). These types are two random unsigned or signed integer types. To be able to compare them correctly I need to cast both of them to a 'bigger' integer…
Silicomancer
  • 8,604
  • 10
  • 63
  • 130