Integer and Float Conversions in C

Rate this post

In order to effectively develop C programs, it will be necessary to understand the rules that are used for the implicit conversion of floating point and integer values in C. These are mentioned below. Note them carefully.

  • An arithmetic operation between an integer and integer always yields an integer result.
  • An operation between a real and real always yields a real result.
  • An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real.

Few practical examples:

Integer and Float Conversions in C
Integer and Float Conversions in C

Type Conversion in Assignments

It may so happen that the type of the expression and the type of the variable on the left-hand side of the assignment operator may not be same. In such a case the value of the expression is promoted or demoted depending on the type of the variable on left-hand side of =.

For example:

int i ;
float b ;
i = 3.5 ;
b = 30 ;

Here in the first assignment statement though the expression’s value is a float (3.5) it cannot be stored in i since it is an int. In such a case the float is demoted to an int and then its value is stored.

Hence what gets stored in i is 3. Exactly opposite happens in the next statement. Here, 30 is promoted to 30.000000 and then stored in b, since b being a float variable cannot hold anything except a float value.

Instead of a simple expression used in the above examples if a complex expression occurs, still the same rules apply. For example, consider the following program fragment.

float a, b, c ;
int s ;
s = a * b * c / 100 + 32 / 4 – 3 * 1.1 ;

Here, in the assignment statement some operands are ints whereas others are floats. As we know, during evaluation of the expression the ints would be promoted to floats and the result of the expression would be a float. But when this float value is assigned to s it is again demoted to an int and then stored in s.

Type Conversion in Assignments
Type Conversion in Assignments

Note that though the following statements give the same result, 0, the results are obtained differently.
k = 2 / 9 ;
k = 2.0 / 9 ;

In the first statement, since both 2 and 9 are integers, the result is an integer, i.e. 0. This 0 is then assigned to k. In the second statement 9 is promoted to 9.0 and then the division is performed.
Division yields 0.222222.

However, this cannot be stored in k, k being an int. Hence it gets demoted to 0 and then stored in k.

Related Posts

  • 88
    Welcome to the C Knowledge Base and Tutorial   Introduction to C Programming Start Learning C Programming What is C? Why We should learn C C Character Set : Constants, Variables and Keywords Constant and Variable in C: C Keywords: Types of C Constant Rules for Constructing Integer Constants Rules…
    Tags: rules, variable, type, tutorial, integer, programming, float, conversions, conversion, assignments
  • 57
    We have seen the first c program, Now let us look at the instructions that we used in these programs. There are basically three types of instructions in C: Type Declaration Instruction - To declare the type of variables used in a C program. Arithmetic Instruction - To perform arithmetic operations between…
    Tags: statement, int, variable, float, type, side, example, real, integer, learn
  • 49
    Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this: if ( this condition is true )       execute this statement ; The keyword if tells the compiler that what follows is a decision control…
    Tags: statement, example, float, note, type, learn, programming, tutorial
  • 40
    The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements…
    Tags: statement, expression, learn, example, programming, tutorial
  • 39
    In this article we will learn about the NOT operator, written as !. This operator reverses the result of the expression it operates on. For example, if the expression evaluates to a non-zero value, then applying ! operator to it results into a 0. Vice versa, if the expression evaluates…
    Tags: expression, result, example, learn, programming, tutorial

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: