Newline character(\n) in scanf [duplicate] - c

This question already has answers here:
Why does scanf ask twice for input when there's a newline at the end of the format string?
(7 answers)
Closed 5 years ago.
Suppose i write the code below:
#include<stdio.h>
int main()
{
int a,b;
scanf("%d\n",&a);
printf("%d",a);
return 0;
}
The input is taken and the cursor is blinking in the next line without printing the value of a.
But if I remove the \n character, it is printing the value of a automatically in the next line.
Even if I place a \n, before the %d in scanf (scanf("%d\n",&a);), it is not moving the cursor to the next line, and taking the input, instead of being taking input in the next line. So, does scanf automatically takes input in next lines? and does \n cannot be used with the scanf function??
Actually, my problem wants me to input three integers in three line. It is written Input:
Three integers on three lines.
But on trying to use \n in scanf, it is only showing a cursor blinking in the next line after taking the input.

Any whitespace-character (as determined by isspace()) in the format-string for scanf() will cause it to read and discard characters until the next character read would be non-whitespace, or an error occurs.
You didn't enter any other non-whitespace than the number? Well, have fun waiting.

Related

Why is the C loop executing one more time than required? [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Closed 4 years ago.
I mistakenly used scanf("%d\n",&val); in one of my programmes, I could not understand the behavior, the function showed.
int main(){
int val;
scanf("%d\n", &val);
printf("%d\n", val);
return 0;
}
Now the program required 2 integer inputs, and prints the first input that was entered.
What difference should that extra \n brings?
I tried to search but couldn't find the answer, even through manual of scanf.
An '\n' - or any whitespace character - in the format string consumes an entire (possibly empty) sequence of whitespace characters in the input. So the scanf only returns when it encounters the next non-whitespace character, or the end of the input stream (e.g. when the input is redirected from a file and its end is reached, or after you closed stdin with Ctrl-D).
From man scanf in my Linux box:
A 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.
BTW, %d is also a directive.
So your "%d\n" has two directives, the first one reads the number and the second one... well reads any amount of white space, including your end-of-lines. You have to type any non-white space character to make it stop.

why do compiler ask to print two numbers in new line while given newline in arrays? [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Closed 4 years ago.
I mistakenly used scanf("%d\n",&val); in one of my programmes, I could not understand the behavior, the function showed.
int main(){
int val;
scanf("%d\n", &val);
printf("%d\n", val);
return 0;
}
Now the program required 2 integer inputs, and prints the first input that was entered.
What difference should that extra \n brings?
I tried to search but couldn't find the answer, even through manual of scanf.
An '\n' - or any whitespace character - in the format string consumes an entire (possibly empty) sequence of whitespace characters in the input. So the scanf only returns when it encounters the next non-whitespace character, or the end of the input stream (e.g. when the input is redirected from a file and its end is reached, or after you closed stdin with Ctrl-D).
From man scanf in my Linux box:
A 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.
BTW, %d is also a directive.
So your "%d\n" has two directives, the first one reads the number and the second one... well reads any amount of white space, including your end-of-lines. You have to type any non-white space character to make it stop.

why loop iterating 6 times but condition is given 5 times [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Closed 3 years ago.
in my code according to for loop Condtion it is clear that it must iterate only 5 times from 0 to 4.
But it is taking 6 input from the user.
Why it is happening.
Please clear my doubt.
#include <stdio.h>
#include <conio.h>
void main() {
int i, num;
clrscr();
printf("Enter 5 elements in the array\n");
for (i = 0; i < 5; i++) {
scanf("%d\n", &num);
}
getch();
}
According to your code i think you are mixing up the inputs you request with the system pause created by the getch() function you call at the end. The getch() initiates a pause which you can easily mix up with your input prompts since during this pause, user input is possible though not considered.
I propose you introduce an instruction in the loop which indicates the line or some information so you can make the distinction.
You have two problems regarding input.
The first is the getch call at the end of the program. But the other problem is the newline in the scanf format string
scanf("%d\n",&num);
When the scanf function sees a space in the format string (newline is considered a space in this context) then it will read and discard all spaces in the input. The problem with a trailing space in a format string is that to know when the spaces in the input ends, there must be some non space input as well.
So (almost) never have a trailing space (space, newline, carriage-return, tab) in a scanf format string.
The reason this code wait 5 times user input is the getch() at the end.
It waits for the input of one character and probably used to prevent the shell window from directly closing after the program finshed.

while loop does not execute scanf() [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Closed 6 years ago.
#include <stdio.h>
int main() {
float change = 0.0;
printf("O hai! ");
while (change <= 0) {
printf("How much change is owed?\n");
scanf("%f\n", &change);
}
return 0;
}
and the result if input is a negative is endless "How much change is owed?"
scanf is actually entered, but due to the \n in format string "%f\n", after having entered a number, scanf waits for the next non-whitespace character to return. Note that a white space in format specifier lets scanf consume a sequence of any white space characters, not only one, and so it "hangs" as long as only white space characters are provided by the stream.
Change scanf("%f\n",&change) into scanf("%f",&change).
When you ask a computer to discard whitespace, how does it know what it's done? Answer: As soon as it reads something that's not whitespace.
You asked it to discard whitespace after reading a number. Thus it's not done until it reads the number and then reads some non-whitespace.
That really doesn't make any sense since there's no reason anyone would enter non-whitespace after entering the number.
Here's a tip though that will save you pain in the future: If what you really want to do is read a line of input then parse it, use a function that reads a line and then some code to parse that input.

Difference between scanf("%c", &c) and scanf(" %c", &c) [duplicate]

This question already has answers here:
Why does a space in my scanf statement make a difference? [duplicate]
(3 answers)
Closed 9 years ago.
Consider the following C code snippet:
#include <stdio.h>
int main()
{
int a;
char c;
scanf("%d",&a);
scanf("%c",&c);
printf("int=%d\n",a);
printf("char=%c\n",c);
}
I'm able to input only the integer and not the character.The output is simply the integer value and no value is output for the second printf statement.
However if I use a space before the format specifier:
scanf(" %c",&c);
It works as expected. Why is this the case?
Someone told me it has something to do with clearing the input buffer. Could someone shed some light on the same?
The difference between scanf("%c", &c1) and scanf(" %c", &c2) is that the format without the blank reads the next character, even if it is white space, whereas the one with the blank skips white space (including newlines) and reads the next character that is not white space.
In a scanf() format, a blank, tab or newline means 'skip white space if there is any to skip'. It does not directly 'clear the input buffer', but it does eat any white space which looks similar to clearing the input buffer (but is quite distinct from that). If you're on Windows, using fflush(stdin) clears the input buffer (of white space and non-white space characters); on Unix and according to the C standard, fflush(stdin) is undefined behaviour.
Incidentally, if you typed the integer followed immediately by a carriage return, the output of your program ends with two newlines: the first was in c and the second in the format string. Thus, you might have seen:
$ ./your_program
123
int=123
char=
$
That is, the scanf() reads the newline as its input. Consider an alternative input:
$ ./your_program
123xyz
int=123
char=x
$
The integer input stopped when it read the 'x'; the character input therefore reads the 'x'.
Because after you input the number and press ENTER, the new line stays in the buffer and will be processed by the second scanf.
In short, you saved new line in the variable c.
However ,if you use
scanf(" %c",&c);
// ^
the space will consume the new line, which makes c the value you expected.
You have to pass a pointer to the data object specified by the format string, so
scanf("%c", c);
will actually pass the value of c, which in turn could cause a program fault,
scanf("%c", &c);
will pass the address of c, allowing scanf to change the value of your copy.
The space after the %c will force it to look for a character, AND THEN a space. If there is not a space, it will not read the character

Resources