Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data type promotion or demotion in C

Tags:

c

types

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 :)

like image 725
Matt_p Avatar asked Jan 23 '14 11:01

Matt_p


People also ask

Can int be promoted to float?

Floating-point operands are not promoted like integer ones are: the expression 1. f + 1.

What is automatic type promotion in C?

In c if two operands are of different data type in a binary operation then before performing any operation compiler will automatically convert the operand of lower data type to higher data type. This phenomenon is known as automatic type conversion.

What is integer promotion in C?

Some data types like char , short int take less number of bytes than int, these data types are automatically promoted to int or unsigned int when an operation is performed on them. This is called integer promotion.

What is data type conversion in C?

In C programming, we can convert the value of one data type ( int, float , double , etc.) to another. This process is known as type conversion.


2 Answers

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.

like image 177
Lundin Avatar answered Oct 14 '22 12:10

Lundin


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.

like image 21
Chinna Avatar answered Oct 14 '22 11:10

Chinna