Skip to content

Commit 23545f0

Browse files
committed
type conversions
1 parent d1fd565 commit 23545f0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

prog.4.5.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Basic conversions in C
2+
3+
#include <stdio.h>
4+
5+
int main (void)
6+
{
7+
float f1 = 123.125, f2;
8+
int i1, i2 = -150;
9+
char c = 'a';
10+
11+
i1 = f1; // floating to integer conversion
12+
printf ("%f assigned to an int produces %i\n", f1, i1);
13+
14+
f1 = i2; // integer to floating conversion
15+
printf ("%i assigned to an float produces %f\n", i2, f1);
16+
17+
f1 = i2 / 100; // integer divided by integer
18+
printf ("%i divided by 100 produces %f\n", i2, f1);
19+
20+
f2 = i2 / 100.0; // integer divided by a float
21+
printf ("%i divided by 100.0 produces %f\n", i2, f2);
22+
23+
f2 = (float) i2 / 100; // type cast operator
24+
printf ("(float) %i divided by 100 produces %f\n", i2, f2);
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)