Thread: Newbie C Programmer - Confusion over output of a simple program

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    5

    Newbie C Programmer - Confusion over output of a simple program

    Sir, I wrote a simple program:

    Code:
    #include <stdio.h>
    
    
    main()
    {
        char nam[100];
        char place[50];
        int a;
        printf("\n\tHi, what's your name? ");
        gets(nam);
        printf("\n\tWelcome to our show, %s\n",nam);
        printf("\n\tHow old are you? ");
        scanf("%d",&a);
        printf("\n\tHmm, you don't look a day over 22\n");
        printf("\n\tTell me, %s, where do you live? ",nam);
        gets(place);
        printf("\n\tOh, I've heard %s is a lovely place\n",place);
    }
    which on execution should gives the following output:

    Hi, what's is your name? David
    Welcome to our show, David
    How old are you? 27
    Hmm, you don't look a day over 22
    Tell me, David, where do you live? California
    Oh, I've heard California is a lovely place

    where underlined & italics are typed by the user.

    I have not been able to find out anything wrong with my firts code, but on execution it runs fine as expected till 4th line of the output, but in the 5th line where it should ask for the input, prints the last two line as it is without the input (California here).
    But when i repeat gets(place) one more time in my code as

    Code:
    printf("\n\tTell me, %s, where do you live? ",nam);
        gets(place);
        gets(place);
        printf("\n\tOh, I've heard %s is a lovely place\n",place);
    the program runs exactly as expected.

    So sir, please anybody explain me the reason for why my first code did not run as expected and why the second code demands another gets(place) to run as expected.
    Last edited by omani; 02-26-2017 at 01:11 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well first of all, you shouldn't be using gets() at all.
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    You also need to understand how scanf functions work.
    Given a statement of
    scanf("%d",&a);
    and an input of
    27\n

    the scanf call will extract 27 from the stream and leave the \n for something else to deal with.

    Unfortunately for you, your next input call sees the trailing \n and is immediately happy - the job is done.

    You could try flushing the input buffer -> FAQ > Flush the input buffer - Cprogramming.com

    Or adopt a consistent way of reading all input, using only fgets()
    Code:
        char buff[50];  // deliberately chosen to be no larger than your strings
        printf("\n\tHi, what's your name? ");
        fgets(buff,sizeof(buff),stdin);
        sscanf(buff,"%s",nam);
        printf("\n\tWelcome to our show, %s\n",nam);
        printf("\n\tHow old are you? ");
        fgets(buff,sizeof(buff),stdin);
        sscanf(buff,"%d",&a);
        printf("\n\tHmm, you don't look a day over 22\n");
        printf("\n\tTell me, %s, where do you live? ",nam);
        fgets(buff,sizeof(buff),stdin);
        sscanf(buff,"%s",place);
        printf("\n\tOh, I've heard %s is a lovely place\n",place);
    Both fgets and sscanf return a success/fail status, which should be checked in any production program.

    Whilst it's easy to write a short program when your user is competent and cooperative, most of the time, you need to consider the cooperative but incompetent user.

    The real problems come when the (ab)user is both competent and hostile. These users will deliberately try to break your program by feeding it garbage.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program: no output.
    By hewlettuser in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2017, 02:21 PM
  2. Replies: 5
    Last Post: 02-24-2017, 08:25 AM
  3. Simple c program-please explain output.
    By hitesh.incept in forum C Programming
    Replies: 6
    Last Post: 09-13-2012, 12:04 PM
  4. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 05:07 PM
  5. newbie needs help with simple program
    By ericr6 in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2010, 12:05 PM

Tags for this Thread