Visualstudio c language getchar() related questions

Asked 2 years ago, Updated 2 years ago, 35 views

The code to print out the number you entered into multiplication tables I read a book and tried it When you hit only one getchar() at the end, type an integer in the console window and type enter The window closed right away I added another getchar() and it ran normally. There is only one getchar() in the code in the book, but why does the console window close right away?

c visual-studio

2022-09-21 14:34

1 Answers

When you enter an integer as a scanf() function, for example 7, the n value contains 7 but the buffer actually contains the "7\n" value. That's why you get the remaining '\n' from the getchar() function.

Usually, you have to wait for getchar() to enter, but since the opening character has already entered, the main statement ends. When you add one more, the main statement is maintained because it is in the state of receiving input again.

In fact, if you take the return value of the getchar() function, you can see that it contains the opening character.


2022-09-21 14:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.