Putchar function

This is a discussion on Putchar function within the C Programming forums, part of the General Programming Boards category; hey all, i am a newbie and will appreciate your help with this very beginners thread... i am trying to ...

  1. #1
    nyc
    nyc is offline
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Putchar function

    hey all,
    i am a newbie and will appreciate your help with this very beginners thread...
    i am trying to write a function that will accept an integer and will print it is ASCII value (after conversion) using putchar.
    here is what i have so far:
    Code:
    int my_itoa(int i)
    {
    putchar(i+'0');
    }
    however this function prints the value of i so if my argument was 3 it prints out 3.
    any idea how to get the ASCII value of 51?
    thanks a lot.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Posts
    5,439
    no conversion is needed:

    putchar(51);

    that would print 3.
    Code:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    putchar(i+'0');
    Since you were adding to the ascii value of 0 and since numbers are in the correct order in ascii that is why a value of 3 outputted the number 3 as 0+3=3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21