Like Tree4Likes
  • 1 Post By Salem
  • 1 Post By Salem
  • 1 Post By jwroblewski44
  • 1 Post By BatchProgrammer

Help to understand this code !

This is a discussion on Help to understand this code ! within the C Programming forums, part of the General Programming Boards category; Hi , i can't understand this code : Code: #include <stdio.h> main (){ int ch; do{ ch = getchar(); putchar(ch); ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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?
    redred likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Ah, but how many does it store at the same time?
    redred likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    289
    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.
    redred likes this.

  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.
    redred likes this.

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, 09:50 AM
  2. I don't understand this code
    By Nazgulled in forum C Programming
    Replies: 2
    Last Post: 12-18-2007, 12: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, 01:39 PM

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