Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

c program doesn't work scanf char

#include <stdio.h>
int main() {

    struct cerchio c1, c2;
    float distanza;
    char k;

    //input del centro del primo cerchio
    printf("Enter the coordinate x of the first circle's center: ");
    scanf("%f", &c1.centro.x);
    printf("Enter the coordinate y of the first circle's center: ");
    scanf("%f", &c1.centro.y);

    //input del raggio del cerchio
    printf("Enter the circle's radius: ");
    scanf("%f", &c1.raggio);

    printf("The first circle's center is: (%.2f, %.2f)
", c1.centro.x,      c1.centro.y);


    printf("Do you want to move this circle? y/n 
");
    //Here is the problem <-------------
    scanf("%s", &k); 

    if(k=='y'){
        moveCircle(&c1);
        printf("Now the circle's center is: (%.2f, %.2f)
", c1.centro.x, c1.centro.y);
    }
}

In the scanf under the comment //here is the problem if i put %c the program end. The input doesn't work! If i put %s the program work perfectly. Why? I have declared the variable k char!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
scanf("%s", &k); 

should be

scanf(" %c", &k); 

%c is the right format specifier for a character(char) while %s is used for strings. The space character behind %c skips all whitespace characters including none, until the first non-whitespace character as specified in the C11 standard:

7.21.6.2 The fscanf function

[...]

  1. A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails

The reason why your program wouldn't wait for further input when you used %c is because there was a newline character( ) prevailing in the standard input stream(stdin). Remembering pressing enter after entering data for each scanf? The newline character is not captured by the scanf with %f. This character is instead captured by the scanf with %c. This is why this scanf doesn't wait for further input.

As for why your other scanfs(with a %f) did not consume is because the %f skips whitespace characters as seen in the C11 standard:

7.21.6.2 The fscanf function

[...]

  1. Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier. 284

As for why your program worked when you used is because you were lucky. Using %s instead of %c invokes Undefined Behavior. This is because %s matches a sequence of non-whitespace characters and adds a NUL-terminator at the end. Once a user enters anything, the first character is stored in k while the rest of the characters(if any) as well as the is written in an invalid memory location.

If you are currently thinking why the %s format specifier did not consume the is because it skips whitespace characters.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...