Why does scanf not work properly sometimes
[c] scanf () is skipped (c-beginner)
This is because fgets leaves the rest of the line in the input stream if the given number of characters has already been read. If you use 2 here, this is what happens:
If I go after that I would have 1 character and the '\ 0' in other words a length of 2. I tried that too and I get the same error as with scanf () from earlier ^^.
Click to enlarge ...
- For example, the user enters a and presses return.
- In the input stream there are now the characters "a \ n" (the \ n is the line break).
- fgets (old, 2, stdin) reads the a, notices that it cannot read any more characters because of the zero termination and writes "a \ 0" in old. "\ N" remains in the input stream.
- fgets (new, 2, stdin) reads the \ n and recognizes it as the end of the line. It rewrites "\ n \ 0" in.
But even with 3 you get the same problem if the user enters more than one character. You are on the safe side if you remove the remaining line from the input stream after each entry. You can use the following function, for example:
That is not completely right. fgetsstops when it sees an LF ('\ n') in the input stream. And even under Windows you only get an LF and no CRLF when the user presses Return.
Uncle Schuppig said:
At Microsoft, the end of the line is even 2 characters long: carriage return + line feed. But fgets () stops reading in the CR and appends a 0 to the end. That is why you need 3 characters for one letter.
Click to enlarge ...
- What was your smartest investment
- Will the quantum computer change the financial world?
- How is Quandoo different from OpenTable
- How is India seen in Pakistan
- How does google work
- Is ballet haram
- Why aren't Indian teachers great
- When will music CDs no longer be produced?
- Brett Kavanaugh had a drinking problem
- How do you sell your images
- Who is the most argumentative Indian
- PHP dies in 2018
- What is the job of customer service
- Did Esau make it to heaven?
- What is ceramic coating
- Knowledge of French will be helpful in learning the Portuguese language
- How can artificial selection change a species
- The San Fernando Valley is gentrified
- How many arteries does the heart have
- What is shadow encrypted
- Playing Minecraft makes you gay
- What was the greatest Ringo Starr moment
- All psychosociopaths are control freaks
- What is your rating of Alexis Sanchez