How to scanf if there could be a char or int in the same input position
For example, if I have an input that looks like this : 10 3 128, but also have a second input that looks like this: 16 2 F
How can i scan these values?
I've tried reading it as a string and then trying to convert it to an int or leaving it as a char if needed.
fscanf(in, "%d %d %c or %d, from, to, numorhex);
My program is basically a numeral system converter, and i'd like to know how to scan hex characters as well as ints.
c char int scanf
add a comment |
For example, if I have an input that looks like this : 10 3 128, but also have a second input that looks like this: 16 2 F
How can i scan these values?
I've tried reading it as a string and then trying to convert it to an int or leaving it as a char if needed.
fscanf(in, "%d %d %c or %d, from, to, numorhex);
My program is basically a numeral system converter, and i'd like to know how to scan hex characters as well as ints.
c char int scanf
1
You want to read base 16 numbers? Did you read the documentation?
– Shawn
Nov 28 '18 at 16:20
add a comment |
For example, if I have an input that looks like this : 10 3 128, but also have a second input that looks like this: 16 2 F
How can i scan these values?
I've tried reading it as a string and then trying to convert it to an int or leaving it as a char if needed.
fscanf(in, "%d %d %c or %d, from, to, numorhex);
My program is basically a numeral system converter, and i'd like to know how to scan hex characters as well as ints.
c char int scanf
For example, if I have an input that looks like this : 10 3 128, but also have a second input that looks like this: 16 2 F
How can i scan these values?
I've tried reading it as a string and then trying to convert it to an int or leaving it as a char if needed.
fscanf(in, "%d %d %c or %d, from, to, numorhex);
My program is basically a numeral system converter, and i'd like to know how to scan hex characters as well as ints.
c char int scanf
c char int scanf
asked Nov 28 '18 at 16:15
Stan MarshStan Marsh
216
216
1
You want to read base 16 numbers? Did you read the documentation?
– Shawn
Nov 28 '18 at 16:20
add a comment |
1
You want to read base 16 numbers? Did you read the documentation?
– Shawn
Nov 28 '18 at 16:20
1
1
You want to read base 16 numbers? Did you read the documentation?
– Shawn
Nov 28 '18 at 16:20
You want to read base 16 numbers? Did you read the documentation?
– Shawn
Nov 28 '18 at 16:20
add a comment |
2 Answers
2
active
oldest
votes
Firstly, you can store everything you scanned in int and also as char.
When you want to print it after scanning. You can do it from both types.
add a comment |
You can do something like that :
int main(void)
{
char *line = {"10 3 128", "16 2 F", NULL};
int nb1;
int nb2;
int nb3;
char letter;
char end;
for (size_t i = 0; line[i]; ++i) {
if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
printf("number number number : %d %d %dn", nb1, nb2, nb3);
} else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
printf("number number letter : %d %d %cn", nb1, nb2, letter);
} else {
printf("format line not supported : %sn", line[i]);
}
}
return (0);
}
Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.
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%2f53523783%2fhow-to-scanf-if-there-could-be-a-char-or-int-in-the-same-input-position%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
Firstly, you can store everything you scanned in int and also as char.
When you want to print it after scanning. You can do it from both types.
add a comment |
Firstly, you can store everything you scanned in int and also as char.
When you want to print it after scanning. You can do it from both types.
add a comment |
Firstly, you can store everything you scanned in int and also as char.
When you want to print it after scanning. You can do it from both types.
Firstly, you can store everything you scanned in int and also as char.
When you want to print it after scanning. You can do it from both types.
answered Nov 28 '18 at 16:27
mato0mato0
33
33
add a comment |
add a comment |
You can do something like that :
int main(void)
{
char *line = {"10 3 128", "16 2 F", NULL};
int nb1;
int nb2;
int nb3;
char letter;
char end;
for (size_t i = 0; line[i]; ++i) {
if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
printf("number number number : %d %d %dn", nb1, nb2, nb3);
} else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
printf("number number letter : %d %d %cn", nb1, nb2, letter);
} else {
printf("format line not supported : %sn", line[i]);
}
}
return (0);
}
Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.
add a comment |
You can do something like that :
int main(void)
{
char *line = {"10 3 128", "16 2 F", NULL};
int nb1;
int nb2;
int nb3;
char letter;
char end;
for (size_t i = 0; line[i]; ++i) {
if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
printf("number number number : %d %d %dn", nb1, nb2, nb3);
} else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
printf("number number letter : %d %d %cn", nb1, nb2, letter);
} else {
printf("format line not supported : %sn", line[i]);
}
}
return (0);
}
Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.
add a comment |
You can do something like that :
int main(void)
{
char *line = {"10 3 128", "16 2 F", NULL};
int nb1;
int nb2;
int nb3;
char letter;
char end;
for (size_t i = 0; line[i]; ++i) {
if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
printf("number number number : %d %d %dn", nb1, nb2, nb3);
} else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
printf("number number letter : %d %d %cn", nb1, nb2, letter);
} else {
printf("format line not supported : %sn", line[i]);
}
}
return (0);
}
Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.
You can do something like that :
int main(void)
{
char *line = {"10 3 128", "16 2 F", NULL};
int nb1;
int nb2;
int nb3;
char letter;
char end;
for (size_t i = 0; line[i]; ++i) {
if (end = EOF, sscanf(line[i], "%d %d %d%c", &nb1, &nb2, &nb3, &end) == 3 && end == EOF) {
printf("number number number : %d %d %dn", nb1, nb2, nb3);
} else if (end = EOF, sscanf(line[i], "%d %d %c%c", &nb1, &nb2, &letter, &end) == 3 && end == EOF) {
printf("number number letter : %d %d %cn", nb1, nb2, letter);
} else {
printf("format line not supported : %sn", line[i]);
}
}
return (0);
}
Be really warry that le "%c" will accept any character, like '1', so maybe you will want to restrict the value get in %c at something you consider a letter.
answered Nov 28 '18 at 16:43
Tom'sTom's
2,042420
2,042420
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%2f53523783%2fhow-to-scanf-if-there-could-be-a-char-or-int-in-the-same-input-position%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
1
You want to read base 16 numbers? Did you read the documentation?
– Shawn
Nov 28 '18 at 16:20