printf

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);
}

Parents
  • 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.

Reply
  • 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.

Children
More questions in this forum