Matt_p
Matt_p

Reputation: 135

Data type promotion or demotion in C

Which of these are data type promotion and demotion :

char ch = 'C';
int i = 65;
float fl = 2.2;

ch = ch + 1;
i = fl + 2 * ch;
fl = 2.0 * ch + i;
ch = 5212205.17;

Am I correct if i say :

Promotion :

  • i = fl + 2 * ch;

  • fl = 2 * ch + i;

demotion :

  ch = 5211205.17;

not sure if

  ch = ch + 1;

Is also demotion.. please help :)

Upvotes: 7

Views: 8441

Answers (3)

Lundin
Lundin

Reputation: 213892

char ch = 'C'; 

The literal 'C' is of type int and demoted to type char (conversion to the left operator during assignment).

float fl = 2.2;

The literal 2.2 is of type double and demoted to type float (conversion to the left operator during assignment).

ch = ch + 1;

The variable ch is of type char and promoted to type int (integer promotions).

The result of the addition ch + 1 is of type int and demoted to type char (conversion to the left operator during assignment).

i = fl + 2 * ch;

The variable ch is of type char and promoted to type int (integer promotions).

The result of 2 * ch is of type int and promoted to type float (balancing).

The result of fl + 2 * ch is of type float. The float is demoted to an int (conversion to the left operator during assignment). This is a dangerous conversion because of loss of precision and a good compiler should give a warning for attempting to store a float inside an int without an explicit cast.

fl = 2.0 * ch + i;

The variable ch is of type char and first promoted to type int (integer promotions) and then promoted to type double (balancing).

The result of 2.0 * ch is of type double.

The result of 2.0 * ch + i is of type double and demoted to type float (conversion to the left operator during assignment).

ch = 5212205.17;

The literal 5212205.17 is of type double and demoted to type char (conversion to the left operator during assignment). This is a dangerous conversion and possibly also undefined behavior, since the signedness of char is implementation-defined, and also the number cannot fit inside a char.

Attempting to store a signed floating point number inside a type that cannot represent it (such as an unsigned int) is undefined behavior, i.e. a severe bug.

Upvotes: 12

MikeySec
MikeySec

Reputation: 81

I've never heard of such a concept as "data type demotion" in C programming.

"Promotion" is usually used to denote the fact of converting a "small" type to a bigger type which is native on the platform for which the program is compiled.

For instance, on an x86, gcc will promote an 8-bit char to a 32-bit integer, because the ALU of an x86 processor works on 32-bit words for integers.

To come back to your question, either it's about implicit type conversion, or we miss some context to be able to say which variable will be promoted.

Disclaimer : it looks like you may be preparing for an exam, in which case you should probably stick to your teacher's conventions. Maybe he's calling implicit conversion of an integer or a floating point type to a larger integer respectively floating point type a promotion, the truncation of the fractional part as a demotion, or an integer overflow as a demotion as well.

Upvotes: 0

Chinna
Chinna

Reputation: 4002

In ch = ch+1; Here 1 is an integer so ch is promoted to integer and add to 1 then result is demoted to character and stored in ch.

Upvotes: 1

Related Questions