#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Plz help me on standard Input Output concept


    hi,
    in c programming i want to take input as 24 chars to the array buffer,
    while im giving input 24 char done,it has to print the output this is my requirement.

    but scanf/getchar() is waiting for enter,after hit the enter only its printing the 24 chars,thats ok.!! but i need the scanf to be terminated once it has completed 24 chars input without press enter key.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    5
    use
    Code:
    scanf("%24s",a);
    it will take 24 character from console.
    Last edited by eramit2010; August 7th, 2013 at 06:48 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    20
    But make sure your array has a size of at least 25, remember scanf() appends the end of string character after retrieving the 24 characters.

    Oh and scanf() doesn't discard the remaining characters, they are left in the input buffer. If you want to discard the remaining characters you'll need to do that after the fact.

    Also don't forget that scanf() will also stop processing the string when it encounters a whitespace character. You may want to investigate fgets() instead.

    Jim

    Comments on this post

    • eramit2010 agrees
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,439
    Rep Power
    1872
    > but scanf/getchar() is waiting for enter,after hit the enter only its printing the 24 chars,thats ok.!!
    > but i need the scanf to be terminated once it has completed 24 chars input without press enter key.
    You need to say which OS/Compiler you're using, because non-buffered input can be somewhat awkward.

    You could try setvbuf before calling any other stdio routine.
    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

IMN logo majestic logo threadwatch logo seochat tools logo