#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    21
    Rep Power
    0

    Help displaying unsigned


    How can i display this number "4200000000" without using printf?
    Thanks in advance.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    with "cout" for example.

    Or split it into characters and use "putc".

    Anyway your question is too vague.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    21
    Rep Power
    0
    i did it thanks anyway.....
    Code:
    void    my_putnbru(unsigned int nb)
    {
      int   neg;
    
      neg = 0;
      if (nb >= 10)
        my_putnbr(nb / 10);
      if (neg == 1)
        {
          neg = 0;
          my_putchar(nb % 10 + '1');
        }
      else
        my_putchar(nb % 10 + '0');
    }
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    Originally Posted by sparky90ful
    i did it thanks anyway.....
    Code:
    void    my_putnbru(unsigned int nb)
    {
      int   neg;
    
      neg = 0;
      if (nb >= 10)
        my_putnbr(nb / 10);
      if (neg == 1)
        {
          neg = 0;
          my_putchar(nb % 10 + '1');
        }
      else
        my_putchar(nb % 10 + '0');
    }
    Whatever you did, you did not do it with this code!

    What does neg do? You set it to zero, and test it for 1, it will never have the value 1. What is my_putnbr()? Should that have been a recursive call to my_putnbru(), or are you reusing a signed version to implement the unsigned version? In which case posting your code is of no great use to anyone without my_putnbr().
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    21
    Rep Power
    0
    i forgot to delete the neg.....my_putnbr is my own function who replace "printf %d" and take a int in param i have changed the my_putnbr int param in unsigned int......neg was to handle the neg of an int
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    Originally Posted by sparky90ful
    i forgot to delete the neg.....my_putnbr is my own function who replace "printf %d" and take a int in param i have changed the my_putnbr int param in unsigned int......neg was to handle the neg of an int
    I think it would make more sense to have this function stand alone:

    C Code:
     
    void my_putnbru( unsigned int nb )
    {
      if (nb >= 10)
      {
         my_putnbru( nb / 10 ) ;
      }
      my_putchar (nb % 10 + '0' ) ;
    }


    If you were to define one in terms of another, it makes more sense to define my_putnbr() in terms of my_putnbru() by printing the - sign if negative, changing the sign then calling my_putnbru.

    C Code:
     
    void my_putnbr( int nb )
    {
      if (nb < 0)
      {
         my_putchar( '-' ) ;
         nb = -nb ;
      }
      my_putnbru( nb ) ;
    }



    It may be useful to also return the number of characters output (and certainly does no harm - printf() does that for example):

    C Code:
     
    int my_putnbru( unsigned int nb )
    {
        static int count = 0 ;
        if (nb >= 10)
        {
            putnbru( nb / 10 ) ;
        }
        putchar(nb % 10 + '0');
        count++ ;
     
        return count ;
    }
     
    void my_putnbr( int nb )
    {
      int count = 0 ;
      if (nb < 0)
      {
         my_putchar( '-' ) ;
         nb = -nb ;
         count++ ;
      }
      count += my_putnbru( nb ) ;
      return count ;
    }
    Last edited by clifford; October 24th, 2013 at 08:29 AM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    21
    Rep Power
    0
    Originally Posted by clifford
    I think it would make more sense to have this function stand alone:

    C Code:
     
    void my_putnbru( unsigned int nb )
    {
      if (nb >= 10)
      {
         my_putnbru( nb / 10 ) ;
      }
      my_putchar (nb % 10 + '0' ) ;
    }


    If you were to define one in terms of another, it makes more sense to define my_putnbr() in terms of my_putnbru() by printing the - sign if negative, changing the sign then calling my_putnbru.

    C Code:
     
    void my_putnbr( int nb )
    {
      if (nb < 0)
      {
         my_putchar( '-' ) ;
         nb = -nb ;
      }
      my_putnbru( nb ) ;
    }



    It may be useful to also return the number of characters output (and certainly does no harm - printf() does that for example):

    C Code:
     
    int my_putnbru( unsigned int nb )
    {
        static int count = 0 ;
        if (nb >= 10)
        {
            putnbru( nb / 10 ) ;
        }
        putchar(nb % 10 + '0');
        count++ ;
     
        return count ;
    }
     
    void my_putnbr( int nb )
    {
      int count = 0 ;
      if (nb < 0)
      {
         my_putchar( '-' ) ;
         nb = -nb ;
         count++ ;
      }
      count += my_putnbru( nb ) ;
      return count ;
    }
    Second example is interesting.....fhirst is better then mine and logic but.....i am a noob i tested it works i didn't make attention to details :D anyway thanks for help
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    Originally Posted by sparky90ful
    anyway thanks for help
    No problem. Though repeating my post in your reply was unnecessary - my scrolling finger hurts :rolleyes: !

    BTW depending on why you need to avoid stdio, you might perhaps just as well implement a minimalist printf(). This for example. That code will work without modification with your my_putchar() simply by initialising thus:

    Code:
    init_printf( NULL, my_putchar ) ;
    Then you will have a working printf() function (and sprintf() too). The implementation is not a complete ISO implementation, it is simplified to minimize size.
    Last edited by clifford; October 24th, 2013 at 12:06 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    21
    Rep Power
    0
    i just starting school to learn programation and those are the exercices....they give us to code functions that are already exist and they give us the prototype of the function......your example is exactly what i did....

IMN logo majestic logo threadwatch logo seochat tools logo