3.8 – Conversions with printf()

by subbu on September 8, 2013

Type conversions with printf():

char vs int:

printf() performs automatic conversion from char to int and int to char. Most of the times, it is proved as a reliable conversion. If we use %d as the format specifier then its equalant decimal is printed. If we use %c as the format specifier then equalant character is printed.

#include<stdio.h>
int main()
{
char ch='b';
printf("%d",ch);/*prints ASCII value of 'b'*/
printf("\n%c",ch);/*prints the character*/
return 0;
}

Output:
98
b

#include<stdio.h>
int main()
{
int num=98;
printf("%d",num);/*printf the number*/
printf("\n%c",num);/*prints equalant character to ASCII 65*/
return 0;
}

Output:
98
b
Character to int conversion in C language
In the first example equalant binary to ‘b’ is 01100010, assigned to the variable ch. In the second example equalant binary to 98 is 1100010, assigned to the variable num. As both have the same binary, equal decimal is printed when %d used, equal character is printed when %c used as format specifiers.

Now we will see what happens if an integer is printed using %c which is beyond the range of char type.

#include<stdio.h>
int main()
{
int x=300;
printf("%d",x);
printf("\n%c",x);
return 0;
}

Output:
300
,

Binary equal to 300 (1 0010 1100) is stored in int type memory as (0000 0001 0010 1100)
While printing with %d, it is treated as positive number because the first bit in 16bits is 0 and prints its decimal equal 300
While printing with %c equal character to the lower 8bits is printed (44 is the ASCII value of ,)
int to char in C language

floating point conversions:

Because the purpose of printf() is not to convert, it is proved that converting from float to other types and other types to float using format specifiers is not reliable and needs explicit casting.

#include<stdio.h>
int main()
{
int a=10,b=20;
float c=14.78,d=67.89;
printf("%d\t%d\t%f\t%d",a,b,c,d);
return 0;
}

Output:
10      20      14.780000       -1073741824

We expect printing the value of float type variable ‘d’ (67.89) with %d will result rounding to 67, but produces some unexpected result.

#include<stdio.h>
int main()
{
int a=10,b=20;
float c=14.78,d=67.89;
printf("%f\t%d\t%f\t%f",a,b,c,d);
return 0;
}

Output:
0.000000        536870912       -2.000000       0.000000

We expect printing the value of int type variable ‘a’ (10) with %f will result printing with decimal places like 10.000000, but produced some unexpected result.
Even values of b,c and d are not printed properly though proper format specifiers are used.
With this example we understand that once printf() malfunctions, it can’t perform well after words though proper format specifiers are used.

Why floating point conversions with printf() is not reliable:

We will see why it is not reliable to perform floating point conversions with printf() through a couple of examples.

#include<stdio.h>
int main()
{
float x=123.999999;
printf("%c",x);
printf("\n%d",x);
return 0;
}

Output:
0
0

Here, in this example 123.999999 is stored in 4bytes of x as 1’s and 0’s.
When we print it with %c then character to ASCII equalant of lower 1byte is printed
When we print it with %d then decimal equal to lower 2bytes is printed.
float to char, int conversions in C language

#include<stdio.h>
int main()
{
int x=247;
printf("%f",x);
return 0;
}

Output in Turbo C 3.0:
printf: floating point format not linked
Abnormal program termination

Output in MinGW gcc:
0.000000
int to float conversions in C language
Here, in this example 247 is stored in 2bytes of x as 1’s and 0’s
When we print it with %f then floating point equalant to 4bytes that is additional 2bytes (garbage) is printed.

Explicit conversion in printf()

It is suggested to explicitly convert using typecast operator according to the format specifier we use in printf(). All the above programs are rectified using type cast operator
Note: follow this link to know about type casting

#include<stdio.h>
int main()
{
int a=10,b=20;
float c=14.78,d=67.89;
printf("%d\t%d\t%f\t%d",a,b,c,(int)d);
return 0;
}

Output:
10      20      14.780000       67

#include<stdio.h>
int main()
{
int a=10,b=20;
float c=14.78,d=67.89;
printf("%f\t%d\t%f\t%f",(float)a,b,c,d);
return 0;
}

Output:
10.000000       20      14.780000       67.889999Pr

#include<stdio.h>
int main()
{
int x=247;
printf("%f",(float)x);
return 0;
}

Output:
247.000000

Quiz:
1) What would the output of following program?

#include<stdio.h>
int main()
{
int i=97;
printf("%c",i);
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
float f=65.34;
printf("%c",(char)f);
return 0;
}

3) What would be the output of following program in Turbo C 3.0 and MinGW gcc?


#include<stdio.h>
int main()
{
printf("%d",sizeof('a'));
printf("\n%d",sizeof(7));
printf("\n%d",sizeof(0.7));
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
double d=(double)'a';
printf("%lf",d);
return 0;
}

5) What would be the output of following program?

#include<stdio.h>
int main()
{
float f=45.78;
int i=(int)f;
printf("%d",i);
printf("\n%f",f);
return 0;
}

6) What would be the output of following program?

#include<stdio.h>
int main()
{
float f=(char)87.56;
printf("%f",f);
return 0;
}

7) What would be the output of following program?

#include<stdio.h>
int main()
{
int i=3;
float j=4;
printf("%d",i/j);
return 0;
}

8) What would be the output of following program?

#include<stdio.h>
int main()
{
char ch='c';
printf("%f",(float)((int)ch));
return 0;
}

9) What would be the output of following program?

#include<stdio.h>
int main()
{
int p=2.999998f;
printf("%f",p);
return 0;
}

10) What would be the output of following program?

#include<stdio.h>
int main()
{
int a=4.5678;
int b=5.98756;
printf("%d\n%d",a,b);
return 0;
}

11) What would be the output of following program?

#include<stdio.h>
int main()
{
float a;
a=4/2;
printf("%f\n%f",a,4/2);
return 0;
}
Show Answers

1)
Output:
a
97 is the ASCII value of ‘a’

2)
Output:
A
“(char)f” – (char)65.34 – (char)65 – ‘A’

3)
Output in Turbo C 3.0:
2
2
8
sizeof(‘a’) – sizeof(97) – sizeof(int) – 2
sizeof(7) – sizeof(int) – 2
sizeof(0.7) – sizeof(double) – 8

Output in MinGW gcc:
4
4
8
sizeof(‘a’) – sizeof(97) – sizeof(int) – 4
sizeof(7) – sizeof(int) – 4
sizeof(0.7) – sizeof(double) – 8

4)
Output:
97.000000
(double)’a’ is 97.000000

5)
Output:
45
45.779999
“(int)f” rounds 45.78 and returns 45

6)
Output:
87.000000
(char)87.56 gives W. The ASCII value of W 87 is assigned to f as 87.000000

7)
Output:
0
i/j return float, which can’t be printed with %d results unexpected result

8)
Output:
99.000000
(int)ch – int(c) – 99

9)
Output in Turbo C 3.0:
printf: floating point format not linked
Abnormal program termination
Output in MinGW gcc:
0.000000
Can’t print int type with %f

10)
Output:
4
5
Floating point constant rounds when assigns to int type variables

11)
Output:
2.000000
0.000000
a has 2.000000 which is properly printed with %f
4/2 returns an int type 2 can’t be printed with %f results unexpected result

Previous post:

Next post: