top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

What is automatic type promotion in c

+7 votes
281 views
What is automatic type promotion in c
posted Dec 3, 2013 by Prachi Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

C performs type conversions automatically when values of differing types participate in expressions. For most programming, you don't have to memorize these rules exactly, but it's good idea to have a general understanding of how they work, so that you won't be surprised by any of the default conversions, and so that you'll know to use explicit conversions in those few cases where C would not perform a needed conversion automatically.

The rules, then (which you can also find on page 44 of K&R2) are approximately as follows:

1) In most circumstances, values of type char and short int are converted to int right off the bat.
2) If an operation involves two operands, and one of them is of type long double, the other one is converted to long double.
3) If an operation involves two operands, and one of them is of type double, the other one is converted to double.
4) If an operation involves two operands, and one of them is of type float, the other one is converted to float.
5) If an operation involves two operands, and one of them is of type long int, the other one is converted to long int.
6) If an operation involves both signed and unsigned integers, the situation is a bit more complicated. If the unsigned operand is smaller (perhaps we're operating on unsigned int and long int), such that the larger, signed type could represent all values of the smaller, unsigned type, then the unsigned value is converted to the larger, signed type, and the result has the larger, signed type. Otherwise (that is, if the signed type can not represent all values of the unsigned type), both values are converted to a common unsigned type, and the result has that unsigned type.
7) Finally, when a value is assigned to a variable using the assignment operator, it is automatically converted to the type of the variable if (a) both the value and the variable have arithmetic type (that is, integer or floating point), or (b) both the value and the variable are pointers, and one or the other of them is of type void *.
answer Dec 5, 2013 by Alok Kumar
+1 vote

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.

For example:-

int a = 5, c; 
float b = 20.1; 
c = a + b; 

Here a int variable, while b is float variable. So before performing addition operation value of the variable a (Lower data type) will automatically convert into float constant (higher data type) then it will perform addition operation. I hope it should answer your doubt.

answer Dec 3, 2013 by Meenal Mishra
Similar Questions
+4 votes

What will be the output of this code?

 void main(){
       int i=320;
       char *ptr=(char *)&i;
       printf("%d",*ptr); 
    }
+2 votes

A char can not be a +ve or -ve. last bit is signed bit in accumulator.but not useful for CHAR Whats the purpose? of doing it.

+4 votes

What is the point of declaring Pointer of different types (eg. integer,float,char) as we all know pointer takes 4 bytes of space regardless which type of pointer it is and only contains address?

+2 votes

If I correct , by default main () function has integer return type.
I want to know what happens in the system internally when its return type gets changed from integer to void ?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...