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.
View all questions in Keil forum