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 the output of this question is 266 -1 10 in turbo c compiler?

int main(){ int a, b; char *cp; a = 511; cp = &a; b = *cp; *cp = 10; printf("%d %d %d", a,b,*cp); return 0; } The output for the above code is 266 -1 10 I understood how a = 266, this is because the first byte of a…
0
votes
2 answers

Is it expected for sizeof(char) to return 1 instead of 4 when integer promotion takes place in C?

#include int main() { unsigned char c = 'a'; c = c + 3; printf("%c ", c); printf("%zu",sizeof(c)); return 0; } Output:d 1 when integer promotion takes place implicitly in c. then why the size of char is printing 1(in…
0
votes
2 answers

Misra Rule violation "composite expression shall not be assigned to an object with wider essential type" and Integral promotion

I have below function to check some count value and update the final count. uint16 final_count = 0U; uint8 count1 = 0U; uint8 count2 = 0U; uint8 count3 = 0U; uint8 count4 = 0U; void test(void) { uint8 input = 0U; input = get_input();…
user2986042
  • 1,098
  • 2
  • 16
  • 37
0
votes
2 answers

Same-line vs. multi-lined bitwise operation discrepancy

While writing a program for uni, I noticed that unsigned char byte_to_write_1 = (0xFF << 2) >> 2; ==> 0xFF (wrong) unsigned char byte_to_write_2 = (0xFF << 2); byte_to_write_2 = byte_to_write_2 >> 2; ==> 0x3F (correct) I don't understand what's…
0
votes
0 answers

Conversion warning depends on order of operands

When compiling with -Wconversion I am noticing that I get different results depending on the order of operands to my bitwise and. For example // This is fine uint16_t thing1 = thing1 | (0x00FFU & buffer[5]); // This is not fine uint16_t thing2 =…
0
votes
1 answer

Usage of int instead of char

I've got a copy of the book "The C Programming Language", and in one of the first chapters it shows a short example of a code that copies characters from stdin to stdout. Here is the code: main() { int c; c = getchar(); while (c != EOF)…
0
votes
1 answer

What is the result type of operation between integer literals?

int main(){ char a = 5 + (16711935 * 1200); return 0; } Based on the type of integer literals and conversion rules in C, 16711935 is of type int and 1200 is promoted to an int. My question is what is the type of the intermediate result of this…
Dan
  • 2,694
  • 1
  • 6
  • 19
0
votes
2 answers

How does type conversion and integer promotion work for stdint.h?

In C, I understand type conversions, integer promotion, casting, etc. for standard types, but how do the stdint.h types factor into this? For type rankings, the rules state: No two signed integer types shall have the same rank, even if they have…
user1801359
  • 422
  • 1
  • 4
  • 14
0
votes
4 answers

what is the output of conditional operator with unary operator

I have the following code where behavior is not clear to me. Can some one please help how conditional operator evaluate the following code and output ans as 1 #include int main() { bool delayMessages=0; bool Delay = false; delayMessages += Delay ?…
0
votes
1 answer

avoiding array index promotion in hopes of better performance

I have a huge array of integers and those integers are not greater than 0xFFFF. Therefore I would like save some space and store them as unsigned short. unsigned short addresses[50000 /* big number over here */]; Later I would use this array as…
user3600124
  • 829
  • 1
  • 7
  • 19
0
votes
3 answers

Printf in unsigned short int and unsigned int using %d

Why in second printf when using %d I am not getting -1? #include int main(){ unsigned int u=-1; unsigned short int y=-1; printf("%d %u\n",u,u); printf("%d %u",y,y); }
0
votes
2 answers

Conversion Warning with Bitwise Or and Casted Operands

Code Snippet 1 (shown below) produces the following -Wconversion warning: debug_Wconversion.c:10:57: warning: conversion to ‘uint16_t’ from ‘int’ may alter its value [-Wconversion] result = ((uint16_t) (((uint16_t) byte1) & 0x0050)) |…
0
votes
2 answers

The type returned by 'operator|' does not always match the type you passed it

Some code called a templated function, but it did not call the specialisation I expected. Turned out the culprit was this: I or-ed some uint16_t constants and passed them as an argument like this: foo(uint16_bar | uint16_baz);. My expectation was it…
0
votes
1 answer

C++ Tilde Operator on bool

I've done the LinkedIn C++ Assessment and got the following question: What is the result from executing this code snippet? bool x=true, x=false; if(~x || y) { /*part A*/ } else { /*part B*/ } I don't know anymore what the answers were,…
DomiB
  • 15
  • 5
0
votes
4 answers

Why does printf's hh and h length modifiers exist?

In variadic functions, default argument promotions occur. 6.5.2.2.6 If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type…
ikegami
  • 367,544
  • 15
  • 269
  • 518