Putchar

This is a discussion on Putchar within the C Programming forums, part of the General Programming Boards category; Ive been reading my book through out the course of my day and ive found my self reading this example ...

  1. #1
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24

    Putchar

    Ive been reading my book through out the course of my day and ive found my self reading this example and its output over & over again.

    Code:
    #include <stdio.h>
    
    main()
    {
             putchar(65);
                  putchar(10);
                       putchar(66);
                  putchar(10);
             putchar(67);
        putchar(10);
    return 0;
    }
    now I get the gist of putchar but wat i dont get is why does it only prints
    A
    B
    and not the other letters.

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    10 is the ASCII code for the character \n, so putchar(10) is the same as putchar('\n');

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Inferno
    Ive been reading my book through out the course of my day and ive found my self reading this example and its output over & over again.

    Code:
    #include <stdio.h>
    
    main()
    {
             putchar(65);
                  putchar(10);
                       putchar(66);
                  putchar(10);
             putchar(67);
        putchar(10);
    return 0;
    }
    now I get the gist of putchar but wat i dont get is why does it only prints
    A
    B
    and not the other letters.
    well you only have 4 different numbers - the numbers are ascii for A (65), B (66), and C(67) - the number 10 is '\n' or the newline character which makes your program display like this:

    Code:
    board $ ./putc
    A
    B
    C
    board $
    as opposed to this:
    Code:
    ABC
    by using this modified code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        putchar(65);
    /*    putchar(10); */
        putchar(66);
    /*    putchar(10); */
        putchar(67);
    /*    putchar(10); */
        return 0;
    }
    If you changed your original code.

    edit:: beaten again!

    ~/
    Last edited by kermit; 08-13-2004 at 07:24 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try not to use direct references to ASCII numbers in your code, instead use the character constants. For example, this is a lot easier to understand/read:
    Code:
     putchar ('A');
     putchar ('\n');
     putchar ('B');
     putchar ('\n');
     putchar ('C');
     putchar ('\n');
    Of course, we're only playing with putchar() here, we wouldn't normally call it multiple times in succession to print out a few characters
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Inferno's Avatar
    Join Date
    Nov 2003
    Posts
    24
    hey thnxs yall

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 08:26 PM
  2. Putchar() to print an integer value?
    By swgh in forum C Programming
    Replies: 2
    Last Post: 05-19-2007, 01:30 PM
  3. array help
    By 1rwhites in forum C Programming
    Replies: 17
    Last Post: 11-09-2005, 04:10 PM
  4. crazy triangles
    By markg in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 01:50 PM
  5. Text based frame in win32 console
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 04-16-2002, 08:01 AM

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