Thread: Inserting string with getchar()

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Inserting string with getchar()

    I have the following piece of code
    Code:
    for(i=0;(ch=getchar())!='\n';i++)
            s[i]=ch;
    Why do we use ch here?
    Why can't we use array directly?
    Is it some kind of programming practice?
    Last edited by syamprasadkr; 03-04-2013 at 07:33 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    If you used the array directly, the final newline would be put into the array. This code looks like it is intended to avoid that.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    Yeah..it looks so,Thank U.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble inserting characters into a string
    By C0__0D in forum C Programming
    Replies: 13
    Last Post: 11-28-2012, 04:24 PM
  2. Inserting a string
    By juice in forum C Programming
    Replies: 12
    Last Post: 12-12-2011, 11:53 PM
  3. inserting charcters in a string
    By Calavera in forum C Programming
    Replies: 17
    Last Post: 10-10-2004, 09:40 AM
  4. Replies: 0
    Last Post: 04-05-2003, 09:33 AM
  5. Inserting string
    By mellisa in forum C++ Programming
    Replies: 5
    Last Post: 01-19-2003, 02:20 AM