After partial matching, scanf() ignores the remainder of the input?
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
add a comment |
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
2
There's no way to find out about mismatches after the last conversion specification (that is assigned and counted — excluding%*dbecause it is not assigned, and%nbecause it is not counted). It's a design limitation of thescanf()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
add a comment |
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
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
c scanf
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%*dbecause it is not assigned, and%nbecause it is not counted). It's a design limitation of thescanf()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
add a comment |
2
There's no way to find out about mismatches after the last conversion specification (that is assigned and counted — excluding%*dbecause it is not assigned, and%nbecause it is not counted). It's a design limitation of thescanf()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
add a comment |
1 Answer
1
active
oldest
votes
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.
Hmmm, had thescanf()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%dformat skips leading space, the first ("abcn").
– Some programmer dude
Nov 16 '18 at 8:30
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/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
});
}
});
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%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
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.
Hmmm, had thescanf()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%dformat skips leading space, the first ("abcn").
– Some programmer dude
Nov 16 '18 at 8:30
add a comment |
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.
Hmmm, had thescanf()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%dformat skips leading space, the first ("abcn").
– Some programmer dude
Nov 16 '18 at 8:30
add a comment |
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.
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.
edited Nov 16 '18 at 0:22
answered Nov 16 '18 at 0:17
Some programmer dudeSome programmer dude
303k25265426
303k25265426
Hmmm, had thescanf()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%dformat skips leading space, the first ("abcn").
– Some programmer dude
Nov 16 '18 at 8:30
add a comment |
Hmmm, had thescanf()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%dformat 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
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%2f53329642%2fafter-partial-matching-scanf-ignores-the-remainder-of-the-input%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
2
There's no way to find out about mismatches after the last conversion specification (that is assigned and counted — excluding
%*dbecause it is not assigned, and%nbecause it is not counted). It's a design limitation of thescanf()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