Like Tree2Likes
  • 1 Post By TheBigH
  • 1 Post By syamprasadkr

Inserting string with getchar()

This is a discussion on Inserting string with getchar() within the C Programming forums, part of the General Programming Boards category; 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 ...

  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.
    Salem likes this.
    Code:
    while(!asleep) {
       sheep++;
    }

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

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, 10: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

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