Type Casting
- When user needs the type conversion explicitly, then type casting is used in c programming language.
- Remember that type conversion is automatic while type casting is explicitly specified by the programmer in program.
- Syntax:
(type_name) expression;
- Where, type_name is the name of data type we want to convert the expression to.
- The converted value is used during evaluation of expression only.
- It does not change the basic data type of operand(s) of an expression.
- Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int sum = 47;
int number = 10;
float average;
clrscr();
average = sum/number;
printf("average = sum/number = %f",average);
average = (float)sum/number;
printf("\n average = (float)sum/number = %f",average);
getch();
}
No comments:
Post a Comment