PPS Lab · Experiment 2
Type conversion and type casting
CPractice sessionsJNTUH R22 · CS108ES
Aim
To write a C program that reads two integers, a real number and a character, and shows how C converts one data type to another, both automatically (implicit conversion) and with a cast (explicit conversion).
Algorithm
- Read two integers a and b, a real number x and a character ch.
- If b is 0, print a message and stop, because the program divides by b.
- Implicit conversion: add a and x, so a is converted to
doublebefore the addition. Print the result. - Implicit conversion: store x in an
intvariable, which drops the fractional part. Print it. - Implicit conversion: store ch in an
intvariable, which gives its character code. Print it. - Print
a / b, which is integer division because both areint. - Explicit conversion: print
(double) a / b,(int) xand(char) (ch + 1).
Program
#include <stdio.h>
int main(void) {
int a, b;
double x;
char ch;
printf("Enter two integers: ");
if (scanf("%d %d", &a, &b) != 2) {
printf("Invalid input\n");
return 1;
}
if (b == 0) {
printf("The second integer cannot be 0\n");
return 1;
}
printf("Enter a real number: ");
if (scanf("%lf", &x) != 1) {
printf("Invalid input\n");
return 1;
}
printf("Enter a character: ");
if (scanf(" %c", &ch) != 1) {
printf("Invalid input\n");
return 1;
}
/* Implicit (automatic) conversion: the compiler converts the type itself */
double total = a + x; /* a is converted to double before adding */
int whole = x; /* double to int: the fraction is dropped */
int code = ch; /* char to int: gives the character code */
printf("\nImplicit conversion\n");
printf("a + x = %d + %.2f = %.2f (int converted to double)\n", a, x, total);
printf("whole = x gives %d (fraction dropped)\n", whole);
printf("code = ch gives %d (character code of '%c')\n", code, ch);
printf("a / b = %d (both int, so integer division)\n", a / b);
/* Explicit conversion (casting): the programmer asks for the type */
printf("\nExplicit conversion (casting)\n");
printf("(double) a / b = %.2f\n", (double) a / b);
printf("(int) x = %d\n", (int) x);
printf("(char) (ch + 1) = %c\n", (char) (ch + 1));
return 0;
}Output
Enter two integers: 7 2
Enter a real number: 3.75
Enter a character: A
Implicit conversion
a + x = 7 + 3.75 = 10.75 (int converted to double)
whole = x gives 3 (fraction dropped)
code = ch gives 65 (character code of 'A')
a / b = 3 (both int, so integer division)
Explicit conversion (casting)
(double) a / b = 3.50
(int) x = 3
(char) (ch + 1) = BRecorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
In an expression with mixed types, C converts the smaller type to the larger one before doing the operation: char and short become int, and int becomes double when the other operand is a double. This is called implicit conversion or type promotion.
A cast such as (double) a converts only the value used in that expression; the variable a stays an int. In (double) a / b the cast applies to a first, so the division is done in double. Writing (double) (a / b) would divide as integers first and give 3.00.
The space in scanf(" %c", &ch) skips the newline left behind by the earlier scanf.
Common mistakes
Expecting
7 / 2to give 3.5. Both operands areint, so the result is 3. Cast one operand:(double) 7 / 2.Writing
(double) (a / b). The integer division happens first and the cast only turns 3 into 3.0. Cast an operand, not the result.Thinking that converting 3.75 to
introunds it to 4. Conversion tointdrops the fraction, so the result is 3.Using
scanf("%c", &ch)after reading a number. It reads the leftover newline instead of the character; put a space before%c.
Viva questions
Try to answer each one aloud before you open it.
What is the difference between implicit and explicit type conversion?
Implicit conversion is done automatically by the compiler, for example when an
intis added to adouble. Explicit conversion, or casting, is written by the programmer as(type) value.What is the result of 7 / 2 and of 7.0 / 2 in C?
7 / 2is 3 because both are integers.7.0 / 2is 3.5 because one operand is adouble, so the other is converted todoubletoo.What happens when a double value is assigned to an int variable?
The fractional part is dropped (truncated towards zero), so 3.75 becomes 3 and −3.75 becomes −3.
Why does storing 'A' in an int give 65?
A character is stored as its character code. In ASCII the code of
'A'is 65.What is type promotion?
It is the automatic conversion of a smaller type to a larger one in an expression, such as
chartointorinttodouble, so that no information is lost.