putchar

This is a discussion on putchar within the C Programming forums, part of the General Programming Boards category; i got the following code: #include <stdio.h> main() { int c; c = getchar(); c = c+1; putchar(c); } and ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    99

    putchar

    i got the following code:

    #include <stdio.h>

    main()
    {
    int c;
    c = getchar();
    c = c+1;
    putchar(c);
    }

    and i woul dlike to ask how am I ABLE do the putchar to a new line \n?

    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Please phrase your question in the form of a complete sentence. Otherwise it's doubtful anyone will know what it is you're trying to say.

    [edit] AND DAMNIT, USE CODE TAGS! [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    i got the following code:

    Code:
    #include <stdio.h>
    
    main()
    {
    int c;
    c = getchar();
    c = c+1;
    putchar(c);
    }
    and i would like to ask how am I ABLE do the putchar to a new line \n?

    thanks

    got it now? blamene?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Guesses
    Code:
    putchar( '\n' );

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What I was trying to get you to do, is tell us exactly what it is you wanted to do. Like so:
    Hi, I have this code, and I want to make it so when I call putchar, it puts it on a seperate line.
    Or perhaps:
    Hi, I have this code, and I want to put a newline. How do I do that?
    See? Two different questions, both of which could have been what you were asking. I still don't know what it is you're trying to do. That's why it helps if you're actually clear. It is your job as a person seeking help to be clear as to what you want help with, so we can actually help you.

    Otherwise we have to simply guess. Or chew you out. One is more fun than the other. Guess which.

    Quzah
    Hope is the first step on the road to disappointment.

  6. #6
    </life>
    Join Date
    Oct 2004
    Posts
    83
    PHP Code:
    int i;
    char c='a';

    putchar('\n');

    for(
    i=0;i<26;i++)
    {
        
    putchar(c);
        
    c++;
    }

    putchar('\n'); 
    Microsoft is merely an illusion, albeit a very persistant one.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Or more simply:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char c;
    
      for(c = 'a';c <= 'z';++c)
        putchar(c);
      putchar('\n');
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Of course both solutions begs the question: What happens when you are on a system where 'a' to 'z' is not contigious?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They also beg the question, what does this have to do with the origional poster's question? Unless of course that was actually their question. But since they didn't word it so it was actually understandable, we may never know.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by Thantos
    Of course both solutions begs the question: What happens when you are on a system where 'a' to 'z' is not contigious?
    Nothing much worse than could happen with the OP's original code.
    If you understand what you're doing, you're not learning anything.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You know it'd be kinda funny to run that code on a system where 'z' < 'a'. Think of the fun that could be had!

  12. #12
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Thantos
    Of course both solutions begs the question: What happens when you are on a system where 'a' to 'z' is not contigious?
    That's more a compiler issue, and rare one because all compilers implement, or should implement, the ascii table.

    cogeek, if you want to make your putchar to write a '\n' which is 13 ascii, you must insert a char with ascii 12, which I think it's not possible because all chars from 0 to 31 are control chars used by the platform. chars greater than 31 are used for text printing.
    Last edited by xErath; 11-19-2004 at 11:50 AM.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by xErath
    That's more a compiler issue, and rare one because all compilers implement, or should implement, the ascii table.

    cogeek, if you want to make your putchar to write a '\n' which is 13 ascii, you must insert a char with ascii 12, which I think it's not possible because all chars from 0 to 31 are control chars used by the platform. chars greater than 31 are used for text printing.
    This is really off base. The C standard is not written around the ASCII table. Functions like isupper(), islower(), isdigit(), etc., are there to maintain compatibility between system that are and aren't using the ASCII table.

    Doing putchar('\n') is going to work on a system whether or not it uses ASCII. However, putchar(12) (or 13? I'm not sure why you jumped from 13 to 12) is not necessarily going to work on all systems. '\n' isn't even ASCII value 13. It's ASCII value 10.

    EDIT: Ahh, I just realized you were suggesting doing putchar(012) (or putchar('\12')), not putchar(12) since 12 octal is 10 decimal.
    Last edited by itsme86; 11-19-2004 at 12:12 PM.
    If you understand what you're doing, you're not learning anything.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    That's more a compiler issue, and rare one because all compilers implement, or should implement, the ascii table.
    I suggest you do a board search. I remember Salem posting the name of an IBM system that doesn't have contigious 'a' - 'z'.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    EBCDIC is one such coding system for characters which doesn't have contiguous letters

Page 1 of 2 12 LastLast
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, 07:26 PM
  2. Putchar() to print an integer value?
    By swgh in forum C Programming
    Replies: 2
    Last Post: 05-19-2007, 12:30 PM
  3. array help
    By 1rwhites in forum C Programming
    Replies: 17
    Last Post: 11-09-2005, 03:10 PM
  4. crazy triangles
    By markg in forum C Programming
    Replies: 3
    Last Post: 10-24-2005, 12:50 PM
  5. Text based frame in win32 console
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 04-16-2002, 07:01 AM

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