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;








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?










share|improve this question


























  • 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(). 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


















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?










share|improve this question


























  • 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(). 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














0












0








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?










share|improve this question
















#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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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(). 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

















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













2 Answers
2






active

oldest

votes


















1
















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.






share|improve this answer

























  • 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



















0
















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.






share|improve this answer



























    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
    );



    );














    draft saved

    draft discarded
















    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









    1
















    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.






    share|improve this answer

























    • 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
















    1
















    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.






    share|improve this answer

























    • 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














    1














    1










    1









    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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














    0
















    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.






    share|improve this answer





























      0
















      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.






      share|improve this answer



























        0














        0










        0









        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 28 at 6:33









        Mrak VladarMrak Vladar

        1249 bronze badges




        1249 bronze badges































            draft saved

            draft discarded















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해