-
type conversions
Hi guys, I've been studying type conversions and have some problems when running a simple display program. It prints out what I think might be some garbage and I do not get why. Can you take a look and tell me where I should look for an error? Thanks.
Code:
#include <stdio.h>
main()
{
char c = '\1';
short int s = 2;
int i = -3;
long int m = 5;
float f = 6.5;
double d = 7.5;
printf("%c %hd %d %ld %f %f\n", c, s, i, m, f, d);
printf("%d %ld %f %ld %ld %d\n", c * i, s + m, f / c, d / s, f - d, (int) f);
/*
(int, long int, float, double, double, int)
*/
return 0;
}
The results I am getting are:
:) 2 -3 5 6.500000 7.500000
-3 7 6.500000 0 1074659328 0
It looks to me as if something does not get printed out...
-
I like Lint's helpful information.
Code:
#include <stdio.h>
int main(void)
{
char c = '\1';
short int s = 2;
int i = -3;
long int m = 5;
float f = 6.5;
double d = 7.5;
printf("%c %hd %d %ld %f %f\n", c, s, i, m, f, d);
printf("%d %ld %f %ld %ld %d\n", c * i, s + m, f / c, d / s, f - d, (int) f);
/*
test.c 15: [559 Warning] Size of argument no. 5 inconsistent with format
test.c 15: [559 Warning] Size of argument no. 6 inconsistent with format
*/
return 0;
}
Use %f for float or double values with printf.
-
Oh, right, there is a difference between scanf and printf with specifications! Thanks! I really appreciate.