Hi,
I am trying to run my first C program on Keil...it works perfectly until I insert a printf statement...am i missing something?
#include <reg52.h> #include <stdio.h> void main(void) { unsigned int i; unsigned int ans = 0; for (i =0; i< 5; i++) { ans += 2; } printf("The answer is: " + ans); }
oo im sry...was too tired to notice i pasted the wrong code!
this should have been it:
#include <reg52.h> #include <stdio.h> void main(void) { unsigned int i; unsigned int ans = 0; for (i =0; i< 5; i++) { ans += 2; } printf("The answer is: %u",ans); ans += 2; }
i cant really think of what is going wrong...:S my actual point of doing this program is in using the printf...but it wasnt working...so i thought of adding some maths...and it actually was stepping them well...but as soon as it reaches printf, it blocks :s
Probably because some thing(s) need to be set up before you can use printf; eg, where do you expect the output from printf to appear...?
This will depend upon which particular compiler you are using - another detail that you have omitted to mention!
I suggest that you start with the "Hello, world" example provided with your compiler...
http://www.keil.com/support/man/docs/gs/
http://www.keil.com/support/man/docs/uv3/uv3_examples.htm
"my actual point of doing this program is in using the printf...but it wasnt working"
When I do 'printf() tests, I usually use a simulator and sprintf() so that I don't have to worry about an device's output driver blocking:
#include <reg52.h> #include <stdio.h> char buf[32]; void main(void) { unsigned int i; unsigned int ans = 0; for (i =0; i< 5; i++) { ans += 2; } sprintf(buf, "The answer is: %u",ans); ans += 2; for (;;); }