How scanf works if I add a new line 'n' at the endhow scanf handles standard input vs pipelined inputHow do I get a platform-dependent new line character?How do function pointers in C work?Improve INSERT-per-second performance of SQLite?How line ending conversions work with git core.autocrlf between different operating systemsprintf() on the same line as a scanf()writing in c with printf and scanf not working as expectedHow scanf(“%d”, i) is working?scanf and wrong input makes output wierdScanf skipping a linec - how gets() work after scanf?
Python implementation of atoi
How to run NPCs with complicated mechanics?
How should Thaumaturgy's "three times as loud as normal" be interpreted?
What is the purpose of the rotating plate in front of the lock?
How do you say "to hell with everything" in French?
Are fast interviews red flags?
Is future tense in English really a myth?
What's the biggest difference between these two photos?
Bit floating sequence
How is the phase of 120V AC established in a North American home?
Why do the Brexit opposition parties not want a new election?
How invisible hand adjusts stock prices if company is listed on multiple exchanges, under multiple currencies, and one of the currencies plunges?
How strong is aircraft-grade spruce?
Did "Dirty Harry" feel lucky?
How do we create our own symbolisms?
Do you need to burn fuel between gravity assists?
Is mountain bike good for long distances?
Why are some hotels asking you to book through Booking.com instead of matching the price at the front desk?
How to restrain your dragon?
More than three domains hosted on the same IP address
How do draw effects during the discard phase work?
Two men on a road
Friend is very nit picky about side comments I don't intend to be taken too seriously
Bacteria vats to generate edible biomass, require intermediary species?
How scanf works if I add a new line 'n' at the end
how scanf handles standard input vs pipelined inputHow do I get a platform-dependent new line character?How do function pointers in C work?Improve INSERT-per-second performance of SQLite?How line ending conversions work with git core.autocrlf between different operating systemsprintf() on the same line as a scanf()writing in c with printf and scanf not working as expectedHow scanf(“%d”, i) is working?scanf and wrong input makes output wierdScanf skipping a linec - how gets() work after scanf?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
#include <stdio.h>
int main()
int a,b,c;
scanf("%d%d%dn",&a,&b,&c);
printf("%d %d %d",a,b,c);
return 0;
Look I am taking 3 inputs and printing 3 outputs... But as I added a new line at the end of the scanf function I have to give 4 inputs to get 3 outputs(I get the first 3 I give as input)... How scanf works here?
And in this case:
#include <stdio.h>
int main()
double n[2][5]; int i,j;
for (i=0;i<=1;i++)
for(j=0;j<=4;j++)
scanf("%lfn",&n[i][j]);
printf("Class=%d Roll=%d Marks=%lfn",i+6,j+1,n[i][j]);
return 0;
Look I have to give 11 inputs to get the 10 outputs... And each time I give a input I get the previous input as an output... How scanf is working here?
c newline scanf
add a comment |
#include <stdio.h>
int main()
int a,b,c;
scanf("%d%d%dn",&a,&b,&c);
printf("%d %d %d",a,b,c);
return 0;
Look I am taking 3 inputs and printing 3 outputs... But as I added a new line at the end of the scanf function I have to give 4 inputs to get 3 outputs(I get the first 3 I give as input)... How scanf works here?
And in this case:
#include <stdio.h>
int main()
double n[2][5]; int i,j;
for (i=0;i<=1;i++)
for(j=0;j<=4;j++)
scanf("%lfn",&n[i][j]);
printf("Class=%d Roll=%d Marks=%lfn",i+6,j+1,n[i][j]);
return 0;
Look I have to give 11 inputs to get the 10 outputs... And each time I give a input I get the previous input as an output... How scanf is working here?
c newline scanf
If you want to print 3 output, you need to give 4 input, what is the forth input? Say the first 3 are a,b and c.
– Zhenxiao Hao
Apr 12 '14 at 16:50
just any kind of arbitrary input... in this case any integer... But the program only prints the first 3 inputs...
– ezio3.1415
Apr 12 '14 at 17:03
"%d%d%dn"-->"%d%d%d","%lfn"-->"%lf"
– BLUEPIXY
Apr 12 '14 at 17:30
Avoid usingscanf(). Considerfgets()and then usesscanf()and/orstrto*(). If you must usescanf(), check its return value.
– chux
Apr 12 '14 at 20:20
add a comment |
#include <stdio.h>
int main()
int a,b,c;
scanf("%d%d%dn",&a,&b,&c);
printf("%d %d %d",a,b,c);
return 0;
Look I am taking 3 inputs and printing 3 outputs... But as I added a new line at the end of the scanf function I have to give 4 inputs to get 3 outputs(I get the first 3 I give as input)... How scanf works here?
And in this case:
#include <stdio.h>
int main()
double n[2][5]; int i,j;
for (i=0;i<=1;i++)
for(j=0;j<=4;j++)
scanf("%lfn",&n[i][j]);
printf("Class=%d Roll=%d Marks=%lfn",i+6,j+1,n[i][j]);
return 0;
Look I have to give 11 inputs to get the 10 outputs... And each time I give a input I get the previous input as an output... How scanf is working here?
c newline scanf
#include <stdio.h>
int main()
int a,b,c;
scanf("%d%d%dn",&a,&b,&c);
printf("%d %d %d",a,b,c);
return 0;
Look I am taking 3 inputs and printing 3 outputs... But as I added a new line at the end of the scanf function I have to give 4 inputs to get 3 outputs(I get the first 3 I give as input)... How scanf works here?
And in this case:
#include <stdio.h>
int main()
double n[2][5]; int i,j;
for (i=0;i<=1;i++)
for(j=0;j<=4;j++)
scanf("%lfn",&n[i][j]);
printf("Class=%d Roll=%d Marks=%lfn",i+6,j+1,n[i][j]);
return 0;
Look I have to give 11 inputs to get the 10 outputs... And each time I give a input I get the previous input as an output... How scanf is working here?
c newline scanf
c newline scanf
edited Mar 28 at 6:15
Yunnosch
12.5k6 gold badges25 silver badges36 bronze badges
12.5k6 gold badges25 silver badges36 bronze badges
asked Apr 12 '14 at 16:42
ezio3.1415ezio3.1415
11 bronze badge
11 bronze badge
If you want to print 3 output, you need to give 4 input, what is the forth input? Say the first 3 are a,b and c.
– Zhenxiao Hao
Apr 12 '14 at 16:50
just any kind of arbitrary input... in this case any integer... But the program only prints the first 3 inputs...
– ezio3.1415
Apr 12 '14 at 17:03
"%d%d%dn"-->"%d%d%d","%lfn"-->"%lf"
– BLUEPIXY
Apr 12 '14 at 17:30
Avoid usingscanf(). Considerfgets()and then usesscanf()and/orstrto*(). If you must usescanf(), check its return value.
– chux
Apr 12 '14 at 20:20
add a comment |
If you want to print 3 output, you need to give 4 input, what is the forth input? Say the first 3 are a,b and c.
– Zhenxiao Hao
Apr 12 '14 at 16:50
just any kind of arbitrary input... in this case any integer... But the program only prints the first 3 inputs...
– ezio3.1415
Apr 12 '14 at 17:03
"%d%d%dn"-->"%d%d%d","%lfn"-->"%lf"
– BLUEPIXY
Apr 12 '14 at 17:30
Avoid usingscanf(). Considerfgets()and then usesscanf()and/orstrto*(). If you must usescanf(), check its return value.
– chux
Apr 12 '14 at 20:20
If you want to print 3 output, you need to give 4 input, what is the forth input? Say the first 3 are a,b and c.
– Zhenxiao Hao
Apr 12 '14 at 16:50
If you want to print 3 output, you need to give 4 input, what is the forth input? Say the first 3 are a,b and c.
– Zhenxiao Hao
Apr 12 '14 at 16:50
just any kind of arbitrary input... in this case any integer... But the program only prints the first 3 inputs...
– ezio3.1415
Apr 12 '14 at 17:03
just any kind of arbitrary input... in this case any integer... But the program only prints the first 3 inputs...
– ezio3.1415
Apr 12 '14 at 17:03
"%d%d%dn" --> "%d%d%d" , "%lfn" --> "%lf"– BLUEPIXY
Apr 12 '14 at 17:30
"%d%d%dn" --> "%d%d%d" , "%lfn" --> "%lf"– BLUEPIXY
Apr 12 '14 at 17:30
Avoid using
scanf(). Consider fgets() and then use sscanf() and/or strto*(). If you must use scanf(), check its return value.– chux
Apr 12 '14 at 20:20
Avoid using
scanf(). Consider fgets() and then use sscanf() and/or strto*(). If you must use scanf(), check its return value.– chux
Apr 12 '14 at 20:20
add a comment |
2 Answers
2
active
oldest
votes
A white character in scanf format matches a sequence of white characters in the input until a non-white character.
Newline is a white character and this explains the behavior of your program. Meaning that if your scanf format terminates by a newline, it does not finish until it sees an additional non-blank character after the last parsed input.
Hmm... This explains the first program... But look at the second one... If what you said is true then shouldn't I have to give 20 inputs? Maybe it happens cause each time it starts the loop it scans the previous given number as an input... Hmm,that explains things... Thank you... :)
– ezio3.1415
Apr 12 '14 at 17:09
In each loop it scans one number and all spaces until the next number (but this another number is not read). So, you have to insert 10 numbers and some other character (which may be another number) to scan 10 numbers.
– Marian
Apr 12 '14 at 17:18
Yes while running the loop for the first time it takes 2 inputs but reads only the first one... When the loop is run for the second time it takes another input but reads the 2nd input... And after running the loop for 10 times it doesn't need to read the 11th input...
– ezio3.1415
Apr 12 '14 at 17:24
add a comment |
scanf is only used to input values, now what you need is the your output to be on a new line, basically you want a newline to be printed on your screen, so you must use the n in the printf as you want a new line to be printed.As for why it is asking for four inputs , Im not sure, the syntax says that you must you use % and a letter according to the type of data to be accepted, Ill read more on this and Get back.
Thank You.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23033265%2fhow-scanf-works-if-i-add-a-new-line-n-at-the-end%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A white character in scanf format matches a sequence of white characters in the input until a non-white character.
Newline is a white character and this explains the behavior of your program. Meaning that if your scanf format terminates by a newline, it does not finish until it sees an additional non-blank character after the last parsed input.
Hmm... This explains the first program... But look at the second one... If what you said is true then shouldn't I have to give 20 inputs? Maybe it happens cause each time it starts the loop it scans the previous given number as an input... Hmm,that explains things... Thank you... :)
– ezio3.1415
Apr 12 '14 at 17:09
In each loop it scans one number and all spaces until the next number (but this another number is not read). So, you have to insert 10 numbers and some other character (which may be another number) to scan 10 numbers.
– Marian
Apr 12 '14 at 17:18
Yes while running the loop for the first time it takes 2 inputs but reads only the first one... When the loop is run for the second time it takes another input but reads the 2nd input... And after running the loop for 10 times it doesn't need to read the 11th input...
– ezio3.1415
Apr 12 '14 at 17:24
add a comment |
A white character in scanf format matches a sequence of white characters in the input until a non-white character.
Newline is a white character and this explains the behavior of your program. Meaning that if your scanf format terminates by a newline, it does not finish until it sees an additional non-blank character after the last parsed input.
Hmm... This explains the first program... But look at the second one... If what you said is true then shouldn't I have to give 20 inputs? Maybe it happens cause each time it starts the loop it scans the previous given number as an input... Hmm,that explains things... Thank you... :)
– ezio3.1415
Apr 12 '14 at 17:09
In each loop it scans one number and all spaces until the next number (but this another number is not read). So, you have to insert 10 numbers and some other character (which may be another number) to scan 10 numbers.
– Marian
Apr 12 '14 at 17:18
Yes while running the loop for the first time it takes 2 inputs but reads only the first one... When the loop is run for the second time it takes another input but reads the 2nd input... And after running the loop for 10 times it doesn't need to read the 11th input...
– ezio3.1415
Apr 12 '14 at 17:24
add a comment |
A white character in scanf format matches a sequence of white characters in the input until a non-white character.
Newline is a white character and this explains the behavior of your program. Meaning that if your scanf format terminates by a newline, it does not finish until it sees an additional non-blank character after the last parsed input.
A white character in scanf format matches a sequence of white characters in the input until a non-white character.
Newline is a white character and this explains the behavior of your program. Meaning that if your scanf format terminates by a newline, it does not finish until it sees an additional non-blank character after the last parsed input.
answered Apr 12 '14 at 16:47
MarianMarian
6,2722 gold badges15 silver badges29 bronze badges
6,2722 gold badges15 silver badges29 bronze badges
Hmm... This explains the first program... But look at the second one... If what you said is true then shouldn't I have to give 20 inputs? Maybe it happens cause each time it starts the loop it scans the previous given number as an input... Hmm,that explains things... Thank you... :)
– ezio3.1415
Apr 12 '14 at 17:09
In each loop it scans one number and all spaces until the next number (but this another number is not read). So, you have to insert 10 numbers and some other character (which may be another number) to scan 10 numbers.
– Marian
Apr 12 '14 at 17:18
Yes while running the loop for the first time it takes 2 inputs but reads only the first one... When the loop is run for the second time it takes another input but reads the 2nd input... And after running the loop for 10 times it doesn't need to read the 11th input...
– ezio3.1415
Apr 12 '14 at 17:24
add a comment |
Hmm... This explains the first program... But look at the second one... If what you said is true then shouldn't I have to give 20 inputs? Maybe it happens cause each time it starts the loop it scans the previous given number as an input... Hmm,that explains things... Thank you... :)
– ezio3.1415
Apr 12 '14 at 17:09
In each loop it scans one number and all spaces until the next number (but this another number is not read). So, you have to insert 10 numbers and some other character (which may be another number) to scan 10 numbers.
– Marian
Apr 12 '14 at 17:18
Yes while running the loop for the first time it takes 2 inputs but reads only the first one... When the loop is run for the second time it takes another input but reads the 2nd input... And after running the loop for 10 times it doesn't need to read the 11th input...
– ezio3.1415
Apr 12 '14 at 17:24
Hmm... This explains the first program... But look at the second one... If what you said is true then shouldn't I have to give 20 inputs? Maybe it happens cause each time it starts the loop it scans the previous given number as an input... Hmm,that explains things... Thank you... :)
– ezio3.1415
Apr 12 '14 at 17:09
Hmm... This explains the first program... But look at the second one... If what you said is true then shouldn't I have to give 20 inputs? Maybe it happens cause each time it starts the loop it scans the previous given number as an input... Hmm,that explains things... Thank you... :)
– ezio3.1415
Apr 12 '14 at 17:09
In each loop it scans one number and all spaces until the next number (but this another number is not read). So, you have to insert 10 numbers and some other character (which may be another number) to scan 10 numbers.
– Marian
Apr 12 '14 at 17:18
In each loop it scans one number and all spaces until the next number (but this another number is not read). So, you have to insert 10 numbers and some other character (which may be another number) to scan 10 numbers.
– Marian
Apr 12 '14 at 17:18
Yes while running the loop for the first time it takes 2 inputs but reads only the first one... When the loop is run for the second time it takes another input but reads the 2nd input... And after running the loop for 10 times it doesn't need to read the 11th input...
– ezio3.1415
Apr 12 '14 at 17:24
Yes while running the loop for the first time it takes 2 inputs but reads only the first one... When the loop is run for the second time it takes another input but reads the 2nd input... And after running the loop for 10 times it doesn't need to read the 11th input...
– ezio3.1415
Apr 12 '14 at 17:24
add a comment |
scanf is only used to input values, now what you need is the your output to be on a new line, basically you want a newline to be printed on your screen, so you must use the n in the printf as you want a new line to be printed.As for why it is asking for four inputs , Im not sure, the syntax says that you must you use % and a letter according to the type of data to be accepted, Ill read more on this and Get back.
Thank You.
add a comment |
scanf is only used to input values, now what you need is the your output to be on a new line, basically you want a newline to be printed on your screen, so you must use the n in the printf as you want a new line to be printed.As for why it is asking for four inputs , Im not sure, the syntax says that you must you use % and a letter according to the type of data to be accepted, Ill read more on this and Get back.
Thank You.
add a comment |
scanf is only used to input values, now what you need is the your output to be on a new line, basically you want a newline to be printed on your screen, so you must use the n in the printf as you want a new line to be printed.As for why it is asking for four inputs , Im not sure, the syntax says that you must you use % and a letter according to the type of data to be accepted, Ill read more on this and Get back.
Thank You.
scanf is only used to input values, now what you need is the your output to be on a new line, basically you want a newline to be printed on your screen, so you must use the n in the printf as you want a new line to be printed.As for why it is asking for four inputs , Im not sure, the syntax says that you must you use % and a letter according to the type of data to be accepted, Ill read more on this and Get back.
Thank You.
answered Mar 28 at 6:33
Mrak VladarMrak Vladar
1249 bronze badges
1249 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23033265%2fhow-scanf-works-if-i-add-a-new-line-n-at-the-end%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you want to print 3 output, you need to give 4 input, what is the forth input? Say the first 3 are a,b and c.
– Zhenxiao Hao
Apr 12 '14 at 16:50
just any kind of arbitrary input... in this case any integer... But the program only prints the first 3 inputs...
– ezio3.1415
Apr 12 '14 at 17:03
"%d%d%dn"-->"%d%d%d","%lfn"-->"%lf"– BLUEPIXY
Apr 12 '14 at 17:30
Avoid using
scanf(). Considerfgets()and then usesscanf()and/orstrto*(). If you must usescanf(), check its return value.– chux
Apr 12 '14 at 20:20