prev


Assigned Values For Variables

1.Simple multiplication (16.6.2009)

/*
Program# 1
date : 10-12-2008
Product of two real numbers
*/

#include <stdio.h>
main(){
float a,b,c;
a=5.5;
b=7.5;
c=a*b;
printf("\na=5.5 , b=7.5 a*b=%f\n",c);
}

output:
a=5.5 , b=7.5 a*b=41.250000

Things to note:
1. In this program, since both the variables 'a' and 'b'are float, result will be a float .For example, if c is declared as int, then the output will be a integer value, that is 41.250000 will get truncated and the output will be just 41.Try Changing variable c data type as 'int' and use "%d" as printf format specifier.

Operand a Operand b Resultant:a*b
int int int
int float float
float int float
float float float

From the above table, we can observe that if there are two different datatypes, then the result will be in the type of highest value datatype.

2. In this output after the decimal points 6 digits are there, if we wish we can customize the output by specifying the format string, that is if we use %.2f in the printf statement, then the result will be 41.25, only 2 digits after decimal point will be displayed. Replace above printf() statement with printf( \na=5.5 , b=7.5 a*b=%.2f\n ,c); and check the output.

Comments
blog comments powered by Disqus
next