After partial matching, scanf() ignores the remainder of the input?












1















Is it an intended behavior for scanf(), to ignore the remainder of the input after partially matching the format string?



The source code:



#include <stdio.h>

int main()
{
int a=0, b=0;
b = scanf("abc %d def", &a);
printf("a=%d, b=%dn", a, b);
return 0;
}


The output (BTW, I'm using GCC 6):




$ ./test_scanf01
abc 123 def
a=123, b=1
$ ./test_scanf01
fff 444 zzz
a=0, b=0
$ ./test_scanf01
abc 333 rrrr
a=333, b=1










share|improve this question


















  • 2





    There's no way to find out about mismatches after the last conversion specification (that is assigned and counted — excluding %*d because it is not assigned, and %n because it is not counted). It's a design limitation of the scanf() family of functions.

    – Jonathan Leffler
    Nov 16 '18 at 0:25











  • @JonathanLeffler Disagree. int n = 0; scanf("abc %d def%n", &a, &n); if (n) ... is a simple way to find out about mismatches after the "last" conversion specification.

    – chux
    Nov 16 '18 at 3:51


















1















Is it an intended behavior for scanf(), to ignore the remainder of the input after partially matching the format string?



The source code:



#include <stdio.h>

int main()
{
int a=0, b=0;
b = scanf("abc %d def", &a);
printf("a=%d, b=%dn", a, b);
return 0;
}


The output (BTW, I'm using GCC 6):




$ ./test_scanf01
abc 123 def
a=123, b=1
$ ./test_scanf01
fff 444 zzz
a=0, b=0
$ ./test_scanf01
abc 333 rrrr
a=333, b=1










share|improve this question


















  • 2





    There's no way to find out about mismatches after the last conversion specification (that is assigned and counted — excluding %*d because it is not assigned, and %n because it is not counted). It's a design limitation of the scanf() family of functions.

    – Jonathan Leffler
    Nov 16 '18 at 0:25











  • @JonathanLeffler Disagree. int n = 0; scanf("abc %d def%n", &a, &n); if (n) ... is a simple way to find out about mismatches after the "last" conversion specification.

    – chux
    Nov 16 '18 at 3:51
















1












1








1








Is it an intended behavior for scanf(), to ignore the remainder of the input after partially matching the format string?



The source code:



#include <stdio.h>

int main()
{
int a=0, b=0;
b = scanf("abc %d def", &a);
printf("a=%d, b=%dn", a, b);
return 0;
}


The output (BTW, I'm using GCC 6):




$ ./test_scanf01
abc 123 def
a=123, b=1
$ ./test_scanf01
fff 444 zzz
a=0, b=0
$ ./test_scanf01
abc 333 rrrr
a=333, b=1










share|improve this question














Is it an intended behavior for scanf(), to ignore the remainder of the input after partially matching the format string?



The source code:



#include <stdio.h>

int main()
{
int a=0, b=0;
b = scanf("abc %d def", &a);
printf("a=%d, b=%dn", a, b);
return 0;
}


The output (BTW, I'm using GCC 6):




$ ./test_scanf01
abc 123 def
a=123, b=1
$ ./test_scanf01
fff 444 zzz
a=0, b=0
$ ./test_scanf01
abc 333 rrrr
a=333, b=1







c scanf






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 0:14









bobbibbobbib

1286




1286








  • 2





    There's no way to find out about mismatches after the last conversion specification (that is assigned and counted — excluding %*d because it is not assigned, and %n because it is not counted). It's a design limitation of the scanf() family of functions.

    – Jonathan Leffler
    Nov 16 '18 at 0:25











  • @JonathanLeffler Disagree. int n = 0; scanf("abc %d def%n", &a, &n); if (n) ... is a simple way to find out about mismatches after the "last" conversion specification.

    – chux
    Nov 16 '18 at 3:51
















  • 2





    There's no way to find out about mismatches after the last conversion specification (that is assigned and counted — excluding %*d because it is not assigned, and %n because it is not counted). It's a design limitation of the scanf() family of functions.

    – Jonathan Leffler
    Nov 16 '18 at 0:25











  • @JonathanLeffler Disagree. int n = 0; scanf("abc %d def%n", &a, &n); if (n) ... is a simple way to find out about mismatches after the "last" conversion specification.

    – chux
    Nov 16 '18 at 3:51










2




2





There's no way to find out about mismatches after the last conversion specification (that is assigned and counted — excluding %*d because it is not assigned, and %n because it is not counted). It's a design limitation of the scanf() family of functions.

– Jonathan Leffler
Nov 16 '18 at 0:25





There's no way to find out about mismatches after the last conversion specification (that is assigned and counted — excluding %*d because it is not assigned, and %n because it is not counted). It's a design limitation of the scanf() family of functions.

– Jonathan Leffler
Nov 16 '18 at 0:25













@JonathanLeffler Disagree. int n = 0; scanf("abc %d def%n", &a, &n); if (n) ... is a simple way to find out about mismatches after the "last" conversion specification.

– chux
Nov 16 '18 at 3:51







@JonathanLeffler Disagree. int n = 0; scanf("abc %d def%n", &a, &n); if (n) ... is a simple way to find out about mismatches after the "last" conversion specification.

– chux
Nov 16 '18 at 3:51














1 Answer
1






active

oldest

votes


















4














Yes, scanf reads as long as the input matches the format. Once there is a mismatch scanf stops reading and leaves the rest in the buffer.



For example if you have



scanf("%d %d", &int_var_1, &int_var_2);


and the input is



123 abc


then only the "123 " part would be read. The letters "abc" (and the trailing newline) would be left in the input buffer for the next input operation to read.






share|improve this answer


























  • Hmmm, had the scanf() format been "%d%d" instead, would you expect the remainder to be "abcn" or " abcn"?

    – chux
    Nov 16 '18 at 3:58













  • @chux Considering that the %d format skips leading space, the first ("abcn").

    – Some programmer dude
    Nov 16 '18 at 8:30











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/3.0/"u003ecc by-sa 3.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%2f53329642%2fafter-partial-matching-scanf-ignores-the-remainder-of-the-input%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Yes, scanf reads as long as the input matches the format. Once there is a mismatch scanf stops reading and leaves the rest in the buffer.



For example if you have



scanf("%d %d", &int_var_1, &int_var_2);


and the input is



123 abc


then only the "123 " part would be read. The letters "abc" (and the trailing newline) would be left in the input buffer for the next input operation to read.






share|improve this answer


























  • Hmmm, had the scanf() format been "%d%d" instead, would you expect the remainder to be "abcn" or " abcn"?

    – chux
    Nov 16 '18 at 3:58













  • @chux Considering that the %d format skips leading space, the first ("abcn").

    – Some programmer dude
    Nov 16 '18 at 8:30
















4














Yes, scanf reads as long as the input matches the format. Once there is a mismatch scanf stops reading and leaves the rest in the buffer.



For example if you have



scanf("%d %d", &int_var_1, &int_var_2);


and the input is



123 abc


then only the "123 " part would be read. The letters "abc" (and the trailing newline) would be left in the input buffer for the next input operation to read.






share|improve this answer


























  • Hmmm, had the scanf() format been "%d%d" instead, would you expect the remainder to be "abcn" or " abcn"?

    – chux
    Nov 16 '18 at 3:58













  • @chux Considering that the %d format skips leading space, the first ("abcn").

    – Some programmer dude
    Nov 16 '18 at 8:30














4












4








4







Yes, scanf reads as long as the input matches the format. Once there is a mismatch scanf stops reading and leaves the rest in the buffer.



For example if you have



scanf("%d %d", &int_var_1, &int_var_2);


and the input is



123 abc


then only the "123 " part would be read. The letters "abc" (and the trailing newline) would be left in the input buffer for the next input operation to read.






share|improve this answer















Yes, scanf reads as long as the input matches the format. Once there is a mismatch scanf stops reading and leaves the rest in the buffer.



For example if you have



scanf("%d %d", &int_var_1, &int_var_2);


and the input is



123 abc


then only the "123 " part would be read. The letters "abc" (and the trailing newline) would be left in the input buffer for the next input operation to read.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 0:22

























answered Nov 16 '18 at 0:17









Some programmer dudeSome programmer dude

303k25265426




303k25265426













  • Hmmm, had the scanf() format been "%d%d" instead, would you expect the remainder to be "abcn" or " abcn"?

    – chux
    Nov 16 '18 at 3:58













  • @chux Considering that the %d format skips leading space, the first ("abcn").

    – Some programmer dude
    Nov 16 '18 at 8:30



















  • Hmmm, had the scanf() format been "%d%d" instead, would you expect the remainder to be "abcn" or " abcn"?

    – chux
    Nov 16 '18 at 3:58













  • @chux Considering that the %d format skips leading space, the first ("abcn").

    – Some programmer dude
    Nov 16 '18 at 8:30

















Hmmm, had the scanf() format been "%d%d" instead, would you expect the remainder to be "abcn" or " abcn"?

– chux
Nov 16 '18 at 3:58







Hmmm, had the scanf() format been "%d%d" instead, would you expect the remainder to be "abcn" or " abcn"?

– chux
Nov 16 '18 at 3:58















@chux Considering that the %d format skips leading space, the first ("abcn").

– Some programmer dude
Nov 16 '18 at 8:30





@chux Considering that the %d format skips leading space, the first ("abcn").

– Some programmer dude
Nov 16 '18 at 8:30




















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%2f53329642%2fafter-partial-matching-scanf-ignores-the-remainder-of-the-input%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

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly