C Prog Basics and Type Conversion
C Prog Basics and Type Conversion
• Modulus operator is not valid for real and mixed mode arithmetic.
Integer Division
• #include<stdio.h> • Out put:
• int main() • 2
• {
• int res;
• res=5/2;
• printf(“%d”,res);
• }
Implicit Conversion
• #include<stdio.h> • Output:
• int main() • 2.000000
• {
• float res;
• res=5/2;
• printf(“%f”,res);
• }
Explicit conversion
• #include<stdio.h> • Output:
• int main() • 2.500000
• {
• float res;
• res=(float)5/2;
• printf(“%f”,res);
• }
Explicit Type casting
In this type of conversion, the programmer can convert one data type to other data type explicitly.
• Syntax: (datatype) (expression)
• Expression can be a constant or a variable
• Ex: y = (int) (a+b)
• double a = 6.5;double b = 6.5
• int result = (int) (a) + (int) (b)
• result = 12 instead of 13.
• int a=10
• float(a)->10.00000