We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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); }
Yes.
1) You are missing your description of your problem.
2) You are missing how printf() is expected to work. If you add a string and an integer, you will create a new string pointer into some unknown address of memory. printf() takes a formatting string with magic characters to inform where the formatted data should be inserted. In this case.
printf("The answer is: %u",ans);
Take a closer look at the manual for a description of how to use printf() or any other standard function. Just getting past the compilation step does not mean that the code is valid.
ohh! my mistake...i just switched from C#...but it still halted at that place (in the printf) when stepping each instructions :S
Did you try to single-step into printf()? Without source, you can't. Instead step over.
i tried using Step over because i was using step into :s but it didnt work neither because i added this extra instruction at the very end:
#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); ans += 2; }
and it never reached it :S
The last instruction does not do anything - you are never using ans before leaving main() so what do you expect the compiler to do?
Another thing: You do not have a command line to return to, so do not write a main() that drops you out into the unknown.
Add an infinite loop at the end of main.
Since you're ignoring Per's comment regarding printf() usage, one can only assume that you are experimenting with implicit pointer manipulation. At the end of the 'for' loop, 'ans' will be 10, so printf() will output characters from the space character following the "answer" word in the string; that is, printf() will output " is: ". That is what you are expecting, right?
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 (;;); }