Why does scanf need
But whats the case with strings? Asked By: pranay. Answered By: Scott M. Answered By: Jerry Coffin. Answered By: Aditya Kumar Maurya. In addition, it looks more like the format that we are requesting of the user, that is, two numbers separated by a blank. If you type this program, compile it and run it, you will discover another interesting fact. If you type one number, then press the Enter key, the program will wait for you to type the second number and press the Enter key again.
This happens because scanf skips over white space when it reads numeric data such as the integers we are requesting here. White space characters are those characters that affect the spacing and format of characters on the screen, without printing anything visible. The white space characters you can type as a user are the blank space spacebar , the tab character Tab key , and the newline Enter Key.
If you try running the program again, but place a comma between the two numbers, you will see that something different happens. Instead of reading the two numbers correctly, the second number will be garbage. This is because scanf will only skip white space and the comma is not white space.
What actually happens when you ask scanf to read numeric data is that it first skips any white space it finds and then it reads characters until the character it reads cannot form part of a number that is, it is not a digit or a plus or minus sign.
In this case, when it encounters the comma, it stops reading. Since it has not read any digits, there is no number for it to store, so it simply leaves the original value. You can prove this to yourself by initializing operand2 to some value for instance, 10 at declaration time. Now, if you type a number, then a comma, and then the second number, you will see that the printf statement will print the initialization value, rather than the second number you typed.
When you ask scanf to read character data, it behaves differently. To understand why, you first have to realize that anything you type at the keyboard is a character, even the digits, punctuation marks, blanks, tabs, and the newline you get from pressing the Enter key. When you ask scanf to read a character, it will read whatever is next as the value of the character.
To see how this works, try typing and compiling the following program. When you run it and the program requests three characters, type the letter 'a', a comma, a blank space, and then the digit '1' before pressing the Enter key. In the following "snapshot" of the screen, the characters the user types appear in red:.
You will notice several things when you run the program with this input. First, the output you see may not be exactly what you expected. When printf prints the first three characters, the value of ch1 is 'a', which is perfectly normal.
The value of ch2 is ',' and that is not too surprising either. It is likely that you might expect the value of ch3 to be '1', since that is the next visible character on the input line, but notice that scanf does not skip white space when it is reading character data, as it does when it reads numeric values. Corbin Corbin Hope this helps. Ethan Ethan 1, 3 3 gold badges 18 18 silver badges 37 37 bronze badges.
Jainendra Jainendra Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete?
Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. On a character input in the first scanf , the second one doesn't run. It skips to take input for Would you like to I'm a complete beginner in C programming and I have a question.
Does anyone know why this happens? I am in an introductory C class and am having a problem with data input. I am in the middle of an exercise on subroutines and my code appears correct,
0コメント