Output/Input Problem

Discussion in 'C Programming' started by NO_Code, Oct 10, 2005.

  1. NO_Code

    NO_Code Guest

    Hi,
    I am trying a very simple program on a linux machine. Code given below

    int main{
    char a,b;

    printf("Enter a ");
    scanf("%c",&a);
    printf("Enter b ");
    scanf("%c",&b);
    printf("b= %c\n",b);
    return 0;
    }

    when i run this here is what i get
    Enter a: c
    Enter b: b=
    It only allows me to enter just one character and as soon as I press
    enter the second prompt is printed along with the b= statement and the
    program exits
    What am i doing wrong and how can i correct this?
     
    NO_Code, Oct 10, 2005
    #1
    1. Advertising

  2. NO_Code

    Guest

    scanf("%s", &a);

    %s - this switch allows you to read more characters from console if
    char a[..]; is declared like that or as a pointer.
    You better try to read more info: try "man scanf".
     
    , Oct 10, 2005
    #2
    1. Advertising

  3. NO_Code

    NO_Code Guest

    scanf("%s", &a);
    If i use %s would this not mean that I am trying to read a null
    terminated string? which i am not as i am reading only a character from
    stdin
     
    NO_Code, Oct 10, 2005
    #3
  4. In article <>,
    NO_Code <> wrote:
    >I am trying a very simple program on a linux machine. Code given below


    >int main{
    > char a,b;
    > printf("Enter a ");
    > scanf("%c",&a);
    > printf("Enter b ");
    > scanf("%c",&b);
    > printf("b= %c\n",b);
    > return 0;
    >}


    >when i run this here is what i get
    >Enter a: c
    >Enter b: b=
    >It only allows me to enter just one character and as soon as I press
    >enter the second prompt is printed along with the b= statement and the
    >program exits
    >What am i doing wrong and how can i correct this?


    scanf() with a %c format element reads *exactly* one character. You
    are entering two characters, the input you want and the newline to
    terminate the line. Thus after the first scanf(), you still have
    a character in the input buffer waiting to be read by the second
    scanf().


    The other part of what you need to know:
    - scanf() always leaves the buffer positioned right after what was
    read;
    - *except* for %c and %[ format elements, scanf() skips leading
    whitespace... including possibly any newline that happened to be
    in the buffer;
    - the %c and %[ format elements do NOT skip leading whitespace;
    - the %s format element does NOT read until the end of line: it
    skips leading whitespace and then reads until the first whitespace...
    which might happen to be the next newline, but also might happen
    to be merely a space or tab.


    Notice the implication here, that unless you are using %c or %[
    then scanf() will ignore line boundaries between input elements.
    For example, scanf("%d%d", &i, &j) is perfectly happy to read any of these:

    10 11
    10 11
    [tab]10[tab]11
    [newline][newline][newline]10 11
    10[space][space][space][newline][space][space][space][space]11
    --
    The ordering of results sorted by date is approximate.
     
    Walter Roberson, Oct 10, 2005
    #4
  5. In article <>,
    NO_Code <> wrote:
    >scanf("%s", &a);
    >If i use %s would this not mean that I am trying to read a null
    >terminated string? which i am not as i am reading only a character from
    >stdin


    More No then Yes.

    The scanf() %s format element skips leading whitespace
    and then reads a *whitespace* delimited string, which it then
    stores in the provided input buffer as a NUL terminated string.

    If a is declared as a single character, then scanf("%s", &a)
    would certainly be wrong: at best the position after a
    would get overwritten with the NUL character, and if the user
    happened to enter a longer input, many other memory positions
    might get overwritten as well.
    --
    I am spammed, therefore I am.
     
    Walter Roberson, Oct 10, 2005
    #5
  6. NO_Code

    Suman Guest

    NO_Code wrote:
    > Hi,
    > I am trying a very simple program on a linux machine. Code given below
    >
    > int main{


    How did this even compile? No `unexpected token' problems?

    [to say nothing of the rest ...]
     
    Suman, Oct 10, 2005
    #6
  7. NO_Code <> wrote:

    > scanf("%s", &a);
    > If i use %s would this not mean that I am trying to read a null
    > terminated string? which i am not as i am reading only a character from
    > stdin


    It is proper Usenet etiquette to include the relevant portions of the text
    you are replying to. To do this using Google groups, please follow the
    instructions below, penned by Keith Thompson:

    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 10, 2005
    #7
  8. NO_Code

    Aloo Guest

    Try using "fflush(stdin)" just after the first printf. I think u
    will solve your problem, because by the looks of it the secomd 'scanf'
    seems to read the null character leftover at 'stdin' i.e. the keyboard
    buffer , by the first scanf.
     
    Aloo, Oct 10, 2005
    #8
  9. In article <>,
    Aloo <> wrote:
    >Try using "fflush(stdin)" just after the first printf.


    Try posting some context so we know what you are replying to.

    >I think u
    >will solve your problem, because by the looks of it the secomd 'scanf'
    >seems to read the null character leftover at 'stdin' i.e. the keyboard
    >buffer , by the first scanf.


    I think that it won't solve anything. According to the C standard,
    fflush() is only defined for *output* streams.

    fflush() of an input stream could result in any of a number of Bad Things.

    Possibly the worst of these is that it might happen to discard the
    newline in the buffer, thereby giving the illusion that fflush(stdin)
    is well-defined, leading to a chain of events in which someone ends up
    posting bad advice about fflush(stdin) and thereby encourages a
    proliferation of broken code.

    On the other hand, it might lead to someone's beer being spilled,
    which for them might be a much worse tragedy.
    --
    These .signatures are sold by volume, and not by weight.
     
    Walter Roberson, Oct 10, 2005
    #9
  10. Aloo wrote:
    > Try using "fflush(stdin)"


    Pay no attention to the above. fflush is not defined on input streams.

    > just after the first printf. I think u
    > will solve your problem,


    I doubt it. If it should by accident "work" in one context, it will
    blow up tomorrow.

    > because by the looks of it the secomd 'scanf'
    > seems to read the null character leftover at 'stdin' i.e. the keyboard
    > buffer , by the first scanf.
    >
     
    Martin Ambuhl, Oct 10, 2005
    #10
  11. NO_Code

    Guest

    just use fflush(stdin) to flush the input buffer so that it can show
    the prompt for the next character.
    eg.
    printf("Enter a ");
    scanf("%c",&a);
    fflush(stdin);
    printf("Enter b ");
    scanf("%c",&b);
    fflush(stdin);
    printf("b= %c\n",b);
    return 0;
     
    , Oct 11, 2005
    #11
  12. wrote:
    > just use fflush(stdin) to flush the input buffer so that it can show
    > the prompt for the next character.


    I can't believe we have had two suggestions in 24 hours that people use
    undefined behavior. Please learn: "fflush() is not defined on input
    streams."

    We are told in the Lunyu (13.3) that Zilu put a silly claim to Kongzi.
    Master Kong replied "How can you be so dense? Where an exemplary man is
    ignorant, one should expect him to offer no opinion." This is a maxim
    you should inscribe on your forehead. Or you could look up one of Dan
    Pop's more colorful expressions of the same idea.
     
    Martin Ambuhl, Oct 11, 2005
    #12
  13. a écrit :
    > scanf("%s", &a);
    >
    > %s - this switch allows you to read more characters from console if
    > char a[..]; is declared like that or as a pointer.
    > You better try to read more info: try "man scanf".
    >

    Do it yourself...
     
    Emmanuel Delahaye, Oct 11, 2005
    #13
  14. Walter Roberson a écrit :
    > scanf() with a %c format element reads *exactly* one character. You
    > are entering two characters, the input you want and the newline to
    > terminate the line. Thus after the first scanf(), you still have
    > a character in the input buffer waiting to be read by the second
    > scanf().


    <all good things-for-level-4-gurus snipped>

    Now you understand better why scanf() is for gurus only. Mean
    programmers use fgets().
     
    Emmanuel Delahaye, Oct 11, 2005
    #14
  15. Aloo a écrit :
    > Try using "fflush(stdin)"


    Please stop saying insanities.
     
    Emmanuel Delahaye, Oct 11, 2005
    #15
  16. a écrit :
    > just use fflush(stdin) to flush the input buffer so that it can show

    Please stop saying insanities.
     
    Emmanuel Delahaye, Oct 11, 2005
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    12
    Views:
    1,640
  2. ashutosh
    Replies:
    3
    Views:
    9,005
    Andrew Thompson
    Jun 16, 2005
  3. Guy
    Replies:
    5
    Views:
    2,832
    brucie
    Dec 13, 2003
  4. cjl
    Replies:
    1
    Views:
    10,503
    Michael Winter
    Oct 31, 2005
  5. Paul
    Replies:
    1
    Views:
    430
Loading...

Share This Page