This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

printf problem for integers > 32767

I am using printf function to send data to serial port. The mcu has to send a number which may be upto 65535.

But for numbers larger than 32767, I was getting negative numbers. To test it I wrote following code

        unsigned int i;
        for(i=32760;i<=32770;i++)
                printf("%d\n", i);    //printf ("char %bd int %d long %ld\n",a,b,c);

What I was getting in VB application is


32760
32761
32762
32763
32764
32765
32766
32767
-32768
-32767
-32766

So I tried the same program in Pelles C for Windows . The result was perfect.


32760
32761
32762
32763
32764
32765
32766
32767
32768
32769
32770
Press any key to continue...

Can somebody tell me what is wrong here?

  • And when you found you got incorrect [sic!] printouts, you then immediately started to wonder about signed and unsigned numbers, and picked up the documentation for printf() and too a closer look at the available formatting strings?

    Does the printf() documentation really suggest that "%d\n" is a good formatting string for unsigned integers? Remember that this is standard C - not anything Keil-specific.

  • Dear Per Westermark, you are literally reading my mind, as if you were here. I just downloaded printf documentation from byuh.doncolton.com/.../printf.pdf and trying to study the formatting strings.

    This is not the first time I have searched for the printf documentation :(

    Sorry, I am new to C, just started to code in keil, previously I was using assembly, and finishing my projects somehow. AND I MUST ACCEPT THAT IT WAS ONLY POSSIBLE BECAUSE OF YOU PEOPLE.

    Instead of "%d\n", I had also tried

    const unsigned char code crlf[3] = {0x0d, 0x0a, 0x00};
    printf("%d%s", i,crlf);

    I thought it may not be a Keil-specific issue, but as I did not have any problem in Pelles C, I asked here. Please help!!!

  • Examine the size of an unsigned int. Examine the range of an unsigned int. Examine the differences in size and range of an unsigned int with different compilers and environments. Also, you might want to examine how a value is encoded into an unsigned int anc compare it with how it is done with a signed int.

    Then you should easily understand what is happening.

    I do not charge for my services and, when I can, I do so for free.

  • Thanks J Macmillan,

    I solved my problem with your free service ;)
    I used %u instead of %d or %i & I am getting proper results now.

    THANKS EVERYBODY!!!

  • Then you might like some free reference/learing resources for C programming:

    blog.antronics.co.uk/.../08

    As already noted, the above is all standard 'C' - nothing specifically to do with Keil or C51.

    However, there are certain areas which do have issues specific to Keil C51;

    eg, see: http://www.keil.com/forum/22199/

    You have not been charged for this service.