Problem with unsigned int

Discussion in 'Programmer's Corner' started by thomassabu, Jun 25, 2013.

  1. thomassabu

    Thread Starter New Member

    Jun 24, 2013
    8
    0
    Dear All,

    i wrote the following a code for pic16f88. But not getting the correct output.

    I am getting the correct output till b=0 to 32768. but if i give the value greater than 32768, am obtaining an undesired output with a negative symbol. Someone please help me. Thanks in advance.

    #include<htc.h>
    #include<stdio.h>
    #include "lcd.h"

    #define _XTAL_FREQ 4e6
    void main()
    {
    ANSELH=0x00;
    TRISB=0x01;
    PORTB=0x00;

    lcd_init();
    lcd_clear();
    unsigned int b=65535;
    char a[10]="0";

    while(1)
    {
    sprintf(a,"%d",b);
    lcd_goto(0);
    lcd_puts(a);
    }
     
    Last edited: Jun 25, 2013
  2. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,355
    Change

    sprintf(a, "%d",b);

    to

    sprintf(a, "%u",b);
     
    thomassabu likes this.
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    ...as MrChips has hinted, %d prints the number as a signed quantity, %u, prints it as an unsigned number...
     
    thomassabu likes this.
  4. thomassabu

    Thread Starter New Member

    Jun 24, 2013
    8
    0
    i got the corect output. Thank you very much all.
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    Be sure to take it as an opportunity to gain valuable insight into how the printf() family of functions work.

    When you passed the variable b to the printf() function, all it got was a pattern of 16 bits. The format string tells the function how to interpret that pattern. The information in the format string is.

    The bit takaway, for now, is that whenever you aren't getting results that make sense and there is a printf() family function involved, carefully examine the format string to make sure that what you are really telling the function to do actually does match what you wanted to tell it to do. It's a common mistake and one that you will make from time to time as long as you continue to write code.
     
    thomassabu likes this.
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    The last take-away from this process may be the most important: the sprintf() functions are HUGE... I had a memory increase of some 4K in a project when first including them.

    These functions do so many things you drag in code you do not need,like code to print signed ints when all you need are unsigned ints, which are much simpler and need far less code. There is often a function such as itoa() (Integer TO Ascii) which is small and dedicated to one task.

    The moral is in limited memory embedded code it is far better to use small atomic functions then large one size fits all functions.
     
    thomassabu likes this.
  7. MrChips

    Moderator

    Oct 2, 2009
    12,421
    3,355
    Good point. In small embedded systems, I recommend writing your own itoa() function. It is not difficult to do.
     
    thomassabu likes this.
  8. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,788
    Totally unrelated to the OP's problem, but a very, very worthwhile point to make anytime we see anyone using one of these functions for an embedded or otherwise resource-starved implementation.
     
    thomassabu likes this.
Loading...