Thread: Help to understand this code !

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    6

    Help to understand this code !

    Hi ,
    i can't understand this code :

    Code:
    #include <stdio.h>
    main (){
            int ch;
            do{
                    ch = getchar();
                    putchar(ch);
            } while(ch != EOF);
    
    }
    1. Why ch type is int and is not char ?
    2. Can use other type for ch ?
    3. How many chars can store in ch ?

    Excuse me for my bad english writing , Thanks in advance .

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Why ch type is int and is not char ?
    Read the manual page for getchar()

    2. Can use other type for ch ?
    You can try - see what happens.

    3. How many chars can store in ch ?
    Does it look like an array to you?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    3. How many chars can store in ch ?
    Does it look like an array to you?
    I tested many input for this code with random length , but work with any length !!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ah, but how many does it store at the same time?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    What system are you running on? If Linux, use gdb to step through the code line by line, all the while 'display'ing the contents of the variable "ch". Pay special attention when you get to the end.

    If Windows, use whatever IDE you're using.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    It seems all that code does it prints what the user inputs infinitely until the user terminates the program ( Hence the EOF - End of file ). Not sure
    what the point would be, but I guess it could be useful if you just want to print what the user puts in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me understand this code...
    By shaselai in forum C Programming
    Replies: 1
    Last Post: 02-04-2009, 10:50 AM
  2. I don't understand this code
    By Nazgulled in forum C Programming
    Replies: 2
    Last Post: 12-18-2007, 01:52 PM
  3. Would you help me understand this code?
    By eXeCuTeR in forum C Programming
    Replies: 23
    Last Post: 10-27-2007, 04:20 AM
  4. Need help to understand this STL code.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 01:59 PM
  5. I want to understand this code
    By BadProgrammer in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2003, 02:39 PM