STRCMP on numeric strings
so I have 3 variables:
char fromAge[4];
char toAge[4];
char age[4];
They can all have a number between 18 and 100 (include 18 and 100).
When I give them the follwing values, the following statement is wrong for some reason:
fromAge[4] = 18;
toAge[4] = 100;
age[4] = 25;
if (strcmp(age, fromAge) >= 0 && strcmp(age, toAge) <= 0)
{
//actions
}
It thinks "age" isn't smaller or equals to "toAge".
Any suggestions for why?
edit:
This is how I assign the variables, I leave 1 byte for ''
scanf("%s", fromAge);
scanf("%s", toAge);
and age is 2,5,''
c strcmp
add a comment |
so I have 3 variables:
char fromAge[4];
char toAge[4];
char age[4];
They can all have a number between 18 and 100 (include 18 and 100).
When I give them the follwing values, the following statement is wrong for some reason:
fromAge[4] = 18;
toAge[4] = 100;
age[4] = 25;
if (strcmp(age, fromAge) >= 0 && strcmp(age, toAge) <= 0)
{
//actions
}
It thinks "age" isn't smaller or equals to "toAge".
Any suggestions for why?
edit:
This is how I assign the variables, I leave 1 byte for ''
scanf("%s", fromAge);
scanf("%s", toAge);
and age is 2,5,''
c strcmp
2
the index in C starts from 0. so fromAge[4] doesn't exist. The definedfromAge[4]
is only fromfromAge[0]
tofromAge[3]
and so on.
– Tom Kuschel
Jan 2 at 9:32
This isn't how you assign numeric values to a character array. Use atoi()
– SPlatten
Jan 2 at 9:36
add a comment |
so I have 3 variables:
char fromAge[4];
char toAge[4];
char age[4];
They can all have a number between 18 and 100 (include 18 and 100).
When I give them the follwing values, the following statement is wrong for some reason:
fromAge[4] = 18;
toAge[4] = 100;
age[4] = 25;
if (strcmp(age, fromAge) >= 0 && strcmp(age, toAge) <= 0)
{
//actions
}
It thinks "age" isn't smaller or equals to "toAge".
Any suggestions for why?
edit:
This is how I assign the variables, I leave 1 byte for ''
scanf("%s", fromAge);
scanf("%s", toAge);
and age is 2,5,''
c strcmp
so I have 3 variables:
char fromAge[4];
char toAge[4];
char age[4];
They can all have a number between 18 and 100 (include 18 and 100).
When I give them the follwing values, the following statement is wrong for some reason:
fromAge[4] = 18;
toAge[4] = 100;
age[4] = 25;
if (strcmp(age, fromAge) >= 0 && strcmp(age, toAge) <= 0)
{
//actions
}
It thinks "age" isn't smaller or equals to "toAge".
Any suggestions for why?
edit:
This is how I assign the variables, I leave 1 byte for ''
scanf("%s", fromAge);
scanf("%s", toAge);
and age is 2,5,''
c strcmp
c strcmp
edited Jan 2 at 9:35
Idan
asked Jan 2 at 9:29


IdanIdan
225
225
2
the index in C starts from 0. so fromAge[4] doesn't exist. The definedfromAge[4]
is only fromfromAge[0]
tofromAge[3]
and so on.
– Tom Kuschel
Jan 2 at 9:32
This isn't how you assign numeric values to a character array. Use atoi()
– SPlatten
Jan 2 at 9:36
add a comment |
2
the index in C starts from 0. so fromAge[4] doesn't exist. The definedfromAge[4]
is only fromfromAge[0]
tofromAge[3]
and so on.
– Tom Kuschel
Jan 2 at 9:32
This isn't how you assign numeric values to a character array. Use atoi()
– SPlatten
Jan 2 at 9:36
2
2
the index in C starts from 0. so fromAge[4] doesn't exist. The defined
fromAge[4]
is only from fromAge[0]
to fromAge[3]
and so on.– Tom Kuschel
Jan 2 at 9:32
the index in C starts from 0. so fromAge[4] doesn't exist. The defined
fromAge[4]
is only from fromAge[0]
to fromAge[3]
and so on.– Tom Kuschel
Jan 2 at 9:32
This isn't how you assign numeric values to a character array. Use atoi()
– SPlatten
Jan 2 at 9:36
This isn't how you assign numeric values to a character array. Use atoi()
– SPlatten
Jan 2 at 9:36
add a comment |
2 Answers
2
active
oldest
votes
strcmp
compares strings by comparing the individual characters from left to right and it will return as soon as two characters differ. As the character '1'
is less than the character '2'
, the string "100" will be considered less than the string "25".
Try this code and give the input "100" and "25"
int main()
{
char toAge[4] = {0};
char age[4]={0};
scanf("%3s", age);
scanf("%3s", toAge);
// Using string compare
if (strcmp(age, toAge) < 0)
printf(""%s" is less than "%s"n", age, toAge);
else if (strcmp(age, toAge) > 0)
printf(""%s" is greater than "%s"n", age, toAge);
else
printf(""%s" is equal to "%s"n", age, toAge);
// Using integer compare
if (atoi(age) < atoi(toAge))
printf("%s is less than %sn", age, toAge);
else if (atoi(age) > atoi(toAge))
printf("%s is greater than %sn", age, toAge);
else
printf("%s is equal to %sn", age, toAge);
return 0;
}
Output:
"100" is less than "25"
100 is greater than 25
As you can see the function atoi
can be used to get the result you expect.
You're amazing. PROBLEM SOLVED. thanks!
– Idan
Jan 2 at 10:02
add a comment |
You are checking strings, which means you are checking it alphabetically, and indeed : "2" does not come before "1" in the alphabet: in ASCII, the value for "2" is 50, while the value for "1" is 49, and indeed: 50 does not come before 49.
Therefore it's always a bad idea to use numbers a strings, just treat them as numbers and everything should be fine:
int FromAge;
int ToAge;
...
Good luck
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%2f54003931%2fstrcmp-on-numeric-strings%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
strcmp
compares strings by comparing the individual characters from left to right and it will return as soon as two characters differ. As the character '1'
is less than the character '2'
, the string "100" will be considered less than the string "25".
Try this code and give the input "100" and "25"
int main()
{
char toAge[4] = {0};
char age[4]={0};
scanf("%3s", age);
scanf("%3s", toAge);
// Using string compare
if (strcmp(age, toAge) < 0)
printf(""%s" is less than "%s"n", age, toAge);
else if (strcmp(age, toAge) > 0)
printf(""%s" is greater than "%s"n", age, toAge);
else
printf(""%s" is equal to "%s"n", age, toAge);
// Using integer compare
if (atoi(age) < atoi(toAge))
printf("%s is less than %sn", age, toAge);
else if (atoi(age) > atoi(toAge))
printf("%s is greater than %sn", age, toAge);
else
printf("%s is equal to %sn", age, toAge);
return 0;
}
Output:
"100" is less than "25"
100 is greater than 25
As you can see the function atoi
can be used to get the result you expect.
You're amazing. PROBLEM SOLVED. thanks!
– Idan
Jan 2 at 10:02
add a comment |
strcmp
compares strings by comparing the individual characters from left to right and it will return as soon as two characters differ. As the character '1'
is less than the character '2'
, the string "100" will be considered less than the string "25".
Try this code and give the input "100" and "25"
int main()
{
char toAge[4] = {0};
char age[4]={0};
scanf("%3s", age);
scanf("%3s", toAge);
// Using string compare
if (strcmp(age, toAge) < 0)
printf(""%s" is less than "%s"n", age, toAge);
else if (strcmp(age, toAge) > 0)
printf(""%s" is greater than "%s"n", age, toAge);
else
printf(""%s" is equal to "%s"n", age, toAge);
// Using integer compare
if (atoi(age) < atoi(toAge))
printf("%s is less than %sn", age, toAge);
else if (atoi(age) > atoi(toAge))
printf("%s is greater than %sn", age, toAge);
else
printf("%s is equal to %sn", age, toAge);
return 0;
}
Output:
"100" is less than "25"
100 is greater than 25
As you can see the function atoi
can be used to get the result you expect.
You're amazing. PROBLEM SOLVED. thanks!
– Idan
Jan 2 at 10:02
add a comment |
strcmp
compares strings by comparing the individual characters from left to right and it will return as soon as two characters differ. As the character '1'
is less than the character '2'
, the string "100" will be considered less than the string "25".
Try this code and give the input "100" and "25"
int main()
{
char toAge[4] = {0};
char age[4]={0};
scanf("%3s", age);
scanf("%3s", toAge);
// Using string compare
if (strcmp(age, toAge) < 0)
printf(""%s" is less than "%s"n", age, toAge);
else if (strcmp(age, toAge) > 0)
printf(""%s" is greater than "%s"n", age, toAge);
else
printf(""%s" is equal to "%s"n", age, toAge);
// Using integer compare
if (atoi(age) < atoi(toAge))
printf("%s is less than %sn", age, toAge);
else if (atoi(age) > atoi(toAge))
printf("%s is greater than %sn", age, toAge);
else
printf("%s is equal to %sn", age, toAge);
return 0;
}
Output:
"100" is less than "25"
100 is greater than 25
As you can see the function atoi
can be used to get the result you expect.
strcmp
compares strings by comparing the individual characters from left to right and it will return as soon as two characters differ. As the character '1'
is less than the character '2'
, the string "100" will be considered less than the string "25".
Try this code and give the input "100" and "25"
int main()
{
char toAge[4] = {0};
char age[4]={0};
scanf("%3s", age);
scanf("%3s", toAge);
// Using string compare
if (strcmp(age, toAge) < 0)
printf(""%s" is less than "%s"n", age, toAge);
else if (strcmp(age, toAge) > 0)
printf(""%s" is greater than "%s"n", age, toAge);
else
printf(""%s" is equal to "%s"n", age, toAge);
// Using integer compare
if (atoi(age) < atoi(toAge))
printf("%s is less than %sn", age, toAge);
else if (atoi(age) > atoi(toAge))
printf("%s is greater than %sn", age, toAge);
else
printf("%s is equal to %sn", age, toAge);
return 0;
}
Output:
"100" is less than "25"
100 is greater than 25
As you can see the function atoi
can be used to get the result you expect.
edited Jan 2 at 10:10
answered Jan 2 at 9:52
43864274386427
21.8k31846
21.8k31846
You're amazing. PROBLEM SOLVED. thanks!
– Idan
Jan 2 at 10:02
add a comment |
You're amazing. PROBLEM SOLVED. thanks!
– Idan
Jan 2 at 10:02
You're amazing. PROBLEM SOLVED. thanks!
– Idan
Jan 2 at 10:02
You're amazing. PROBLEM SOLVED. thanks!
– Idan
Jan 2 at 10:02
add a comment |
You are checking strings, which means you are checking it alphabetically, and indeed : "2" does not come before "1" in the alphabet: in ASCII, the value for "2" is 50, while the value for "1" is 49, and indeed: 50 does not come before 49.
Therefore it's always a bad idea to use numbers a strings, just treat them as numbers and everything should be fine:
int FromAge;
int ToAge;
...
Good luck
add a comment |
You are checking strings, which means you are checking it alphabetically, and indeed : "2" does not come before "1" in the alphabet: in ASCII, the value for "2" is 50, while the value for "1" is 49, and indeed: 50 does not come before 49.
Therefore it's always a bad idea to use numbers a strings, just treat them as numbers and everything should be fine:
int FromAge;
int ToAge;
...
Good luck
add a comment |
You are checking strings, which means you are checking it alphabetically, and indeed : "2" does not come before "1" in the alphabet: in ASCII, the value for "2" is 50, while the value for "1" is 49, and indeed: 50 does not come before 49.
Therefore it's always a bad idea to use numbers a strings, just treat them as numbers and everything should be fine:
int FromAge;
int ToAge;
...
Good luck
You are checking strings, which means you are checking it alphabetically, and indeed : "2" does not come before "1" in the alphabet: in ASCII, the value for "2" is 50, while the value for "1" is 49, and indeed: 50 does not come before 49.
Therefore it's always a bad idea to use numbers a strings, just treat them as numbers and everything should be fine:
int FromAge;
int ToAge;
...
Good luck
answered Jan 2 at 9:34
DominiqueDominique
2,37242042
2,37242042
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%2f54003931%2fstrcmp-on-numeric-strings%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
the index in C starts from 0. so fromAge[4] doesn't exist. The defined
fromAge[4]
is only fromfromAge[0]
tofromAge[3]
and so on.– Tom Kuschel
Jan 2 at 9:32
This isn't how you assign numeric values to a character array. Use atoi()
– SPlatten
Jan 2 at 9:36