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

    Join Date
    Oct 2016
    Rep Power

    Arrow Biginner [c-code; strange behaviour]

    What follows is a simple program from "Absolute Beginner's Gude to c-programming".

    The program is suposed to ask for two numbers for multiplication and print result.
    After executing, it asks if the user wishes to continue with the program. If no, it terminates.

    The puzzling behaviour I am faced with is the behavior of the scanf() statement when the program asks if the user
    would like to continue:

    scanf("%c", &choice);

    if I use the statement as depicted here, the output of the program after first execution is:

    Would you like to do another multiplication? (Y/N) Give me a number:

    that is, the scanf("%c", &choice); statement is ignored (or, program assumes the answer to be not N) and the program moves on to ask for a new number.

    However, if I modify the scanf() statement to

    scanf(" %c", &choice);

    (note the space before %c), the program works smoothly, that is it waits for input to the question if the user wants to continue.

    No compiling warnings or errors are received in Code::Blocks (with std. gcc compiler) or the clang compiler used by Harvard's CS50 MOOC web interface.
    The behaviour of the program is, however, same in both environments.

    The question is: Is that space before %c required for scanf() to work properly with character input?
    The scanf() statements with %f work fine without a blank.

    Appreciate any pointer to solving this puzzling behaviour of scanf().

    ======================== the program ======================================
    #include <stdio.h>

    int main(void)
    // declare variables

    float num1, num2, result;
    char choice;

    // ask for two numbers and print result

    printf("Give me a number: ");
    scanf("%f", &num1);
    printf("Give me another number: ");
    scanf("%f", &num2);

    result = num1 * num2;

    printf("\nThe product of %.2f and %.2f is %.2f.\n\n", num1, num2, result);

    // ask if user wants to continue multiplying

    printf("\nWould you like to do another multiplication? (Y/N)");
    scanf("%c", &choice);
    if (choice == 'n')
    choice = 'N';
    while (choice != 'N');

    return 0;
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    The programs all work as expected. The secret hidden character is the line ending marker that you entered into the input stream upon pressing return, some combination of line feeds and carriage returns.
    Concerning the space character
    Originally Posted by scanf man page
    A [scanf] directive is one of the following:

    A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches
    any amount of white space, including none, in the input.

    An ordinary character (i.e., one other than white space or '%'). This character must exactly match the next
    character of input.

    A conversion specification, which commences with a '%' (percent) character. A sequence of characters from
    the input is converted according to this specification, and the result is placed in the corresponding
    pointer argument. If the next item of input does not match the conversion specification, the conversion
    fails—this is a matching failure.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo