Simple calculator not working with big numbers in C
I'm trying to build a simple calculator that would perform multiplication and addition.
I have this code
#include <stdio.h>
#include <string.h>
long result = 0;
long *resultPointer = &result;
long plus(long *current, long num) {
return (*current + num);
}
long krat(long *current, long num) {
return (*current * num);
}
int main() {
char operator[4];
long num;
printf("%dn", 0);
while(scanf("%s %li", &operator[0], &num) != EOF){
if (num > 0) {
if (strcmp(operator, "krat") == 0) {
*resultPointer = krat(&result, num);
}
if (strcmp(operator, "plus") == 0) {
*resultPointer = plus(&result, num);
}
printf("%lin", result);
}
}
return 0;
}
this is the input for the program
plus 123456789
krat 123456789
plus 0
krat 2
krat 3
krat 4
krat 5
krat 6
and this is the output
0
123456789
15241578750190521
30483157500381042
91449472501143126
365797890004572504
1828989450022862520
-7472807373572376496
The problem is that when the numbers get bigger and bigger they turn to negative. Is this the problem with memory allocation for the variables and how to address this?
c algorithm
|
show 1 more comment
I'm trying to build a simple calculator that would perform multiplication and addition.
I have this code
#include <stdio.h>
#include <string.h>
long result = 0;
long *resultPointer = &result;
long plus(long *current, long num) {
return (*current + num);
}
long krat(long *current, long num) {
return (*current * num);
}
int main() {
char operator[4];
long num;
printf("%dn", 0);
while(scanf("%s %li", &operator[0], &num) != EOF){
if (num > 0) {
if (strcmp(operator, "krat") == 0) {
*resultPointer = krat(&result, num);
}
if (strcmp(operator, "plus") == 0) {
*resultPointer = plus(&result, num);
}
printf("%lin", result);
}
}
return 0;
}
this is the input for the program
plus 123456789
krat 123456789
plus 0
krat 2
krat 3
krat 4
krat 5
krat 6
and this is the output
0
123456789
15241578750190521
30483157500381042
91449472501143126
365797890004572504
1828989450022862520
-7472807373572376496
The problem is that when the numbers get bigger and bigger they turn to negative. Is this the problem with memory allocation for the variables and how to address this?
c algorithm
1
You should remove around 90% of the input and output. It is not needed to understand your problem.
– Broman
Nov 25 '18 at 15:18
Unrelated to your numbers but most likely undefined behaviour:char operator[4];
is too small for"krat"
and"plus"
which are 4 characters + 1 for the terminating 0 long. sooperator
should bechar operator[5];
and you should limitscanf()
to read 4 characters with"%4s"
.
– Swordfish
Nov 25 '18 at 15:19
And reason why you didn't simply useresult = operator(result, num)
?
– Groo
Nov 25 '18 at 15:20
You have usedlong
. Why? Wouldn'tshort
orint
suffice? Why, in your opinion, does C have these different types?
– n.m.
Nov 25 '18 at 15:24
If your compiler supports it you could use<stdint.h>
and replacelong
withint64_t
.
– Swordfish
Nov 25 '18 at 15:26
|
show 1 more comment
I'm trying to build a simple calculator that would perform multiplication and addition.
I have this code
#include <stdio.h>
#include <string.h>
long result = 0;
long *resultPointer = &result;
long plus(long *current, long num) {
return (*current + num);
}
long krat(long *current, long num) {
return (*current * num);
}
int main() {
char operator[4];
long num;
printf("%dn", 0);
while(scanf("%s %li", &operator[0], &num) != EOF){
if (num > 0) {
if (strcmp(operator, "krat") == 0) {
*resultPointer = krat(&result, num);
}
if (strcmp(operator, "plus") == 0) {
*resultPointer = plus(&result, num);
}
printf("%lin", result);
}
}
return 0;
}
this is the input for the program
plus 123456789
krat 123456789
plus 0
krat 2
krat 3
krat 4
krat 5
krat 6
and this is the output
0
123456789
15241578750190521
30483157500381042
91449472501143126
365797890004572504
1828989450022862520
-7472807373572376496
The problem is that when the numbers get bigger and bigger they turn to negative. Is this the problem with memory allocation for the variables and how to address this?
c algorithm
I'm trying to build a simple calculator that would perform multiplication and addition.
I have this code
#include <stdio.h>
#include <string.h>
long result = 0;
long *resultPointer = &result;
long plus(long *current, long num) {
return (*current + num);
}
long krat(long *current, long num) {
return (*current * num);
}
int main() {
char operator[4];
long num;
printf("%dn", 0);
while(scanf("%s %li", &operator[0], &num) != EOF){
if (num > 0) {
if (strcmp(operator, "krat") == 0) {
*resultPointer = krat(&result, num);
}
if (strcmp(operator, "plus") == 0) {
*resultPointer = plus(&result, num);
}
printf("%lin", result);
}
}
return 0;
}
this is the input for the program
plus 123456789
krat 123456789
plus 0
krat 2
krat 3
krat 4
krat 5
krat 6
and this is the output
0
123456789
15241578750190521
30483157500381042
91449472501143126
365797890004572504
1828989450022862520
-7472807373572376496
The problem is that when the numbers get bigger and bigger they turn to negative. Is this the problem with memory allocation for the variables and how to address this?
c algorithm
c algorithm
edited Nov 25 '18 at 15:31
Broman
7,204112543
7,204112543
asked Nov 25 '18 at 15:05
intelisintelis
3,19893374
3,19893374
1
You should remove around 90% of the input and output. It is not needed to understand your problem.
– Broman
Nov 25 '18 at 15:18
Unrelated to your numbers but most likely undefined behaviour:char operator[4];
is too small for"krat"
and"plus"
which are 4 characters + 1 for the terminating 0 long. sooperator
should bechar operator[5];
and you should limitscanf()
to read 4 characters with"%4s"
.
– Swordfish
Nov 25 '18 at 15:19
And reason why you didn't simply useresult = operator(result, num)
?
– Groo
Nov 25 '18 at 15:20
You have usedlong
. Why? Wouldn'tshort
orint
suffice? Why, in your opinion, does C have these different types?
– n.m.
Nov 25 '18 at 15:24
If your compiler supports it you could use<stdint.h>
and replacelong
withint64_t
.
– Swordfish
Nov 25 '18 at 15:26
|
show 1 more comment
1
You should remove around 90% of the input and output. It is not needed to understand your problem.
– Broman
Nov 25 '18 at 15:18
Unrelated to your numbers but most likely undefined behaviour:char operator[4];
is too small for"krat"
and"plus"
which are 4 characters + 1 for the terminating 0 long. sooperator
should bechar operator[5];
and you should limitscanf()
to read 4 characters with"%4s"
.
– Swordfish
Nov 25 '18 at 15:19
And reason why you didn't simply useresult = operator(result, num)
?
– Groo
Nov 25 '18 at 15:20
You have usedlong
. Why? Wouldn'tshort
orint
suffice? Why, in your opinion, does C have these different types?
– n.m.
Nov 25 '18 at 15:24
If your compiler supports it you could use<stdint.h>
and replacelong
withint64_t
.
– Swordfish
Nov 25 '18 at 15:26
1
1
You should remove around 90% of the input and output. It is not needed to understand your problem.
– Broman
Nov 25 '18 at 15:18
You should remove around 90% of the input and output. It is not needed to understand your problem.
– Broman
Nov 25 '18 at 15:18
Unrelated to your numbers but most likely undefined behaviour:
char operator[4];
is too small for "krat"
and "plus"
which are 4 characters + 1 for the terminating 0 long. so operator
should be char operator[5];
and you should limit scanf()
to read 4 characters with "%4s"
.– Swordfish
Nov 25 '18 at 15:19
Unrelated to your numbers but most likely undefined behaviour:
char operator[4];
is too small for "krat"
and "plus"
which are 4 characters + 1 for the terminating 0 long. so operator
should be char operator[5];
and you should limit scanf()
to read 4 characters with "%4s"
.– Swordfish
Nov 25 '18 at 15:19
And reason why you didn't simply use
result = operator(result, num)
?– Groo
Nov 25 '18 at 15:20
And reason why you didn't simply use
result = operator(result, num)
?– Groo
Nov 25 '18 at 15:20
You have used
long
. Why? Wouldn't short
or int
suffice? Why, in your opinion, does C have these different types?– n.m.
Nov 25 '18 at 15:24
You have used
long
. Why? Wouldn't short
or int
suffice? Why, in your opinion, does C have these different types?– n.m.
Nov 25 '18 at 15:24
If your compiler supports it you could use
<stdint.h>
and replace long
with int64_t
.– Swordfish
Nov 25 '18 at 15:26
If your compiler supports it you could use
<stdint.h>
and replace long
with int64_t
.– Swordfish
Nov 25 '18 at 15:26
|
show 1 more comment
2 Answers
2
active
oldest
votes
You're overflowing the variables.
You have two options here. Either find a solution for arbitrarily large numbers (there are libraries you can use) or accept that you cannot use numbers that are to big.
The largest number a signed 64 bit integer can hold (it's obvious from your output that long
is 64 bit on your system) is 9223372036854775807 and the largest for an unsigned 64 bit is 18446744073709551615.
It could be worth mentioning that overflow only have a defined behavior for unsigned types.
Other comments
char[4]
is not enough to hold "plus", since you need place for the ''
. Use char[5]
instead.
There is no reason to use pointers in this code. Unless you have a very good reason, I would suggest changing to:
long plus(long current, long num) {
return current + num;
}
Which in turn means that you can completely skip the function. You don't need a function to perform addition of two integers since you have the +
operator to do that very thing.
Also, your usage of scanf
is unsafe. You may write past the end of the array. Do this instead: scanf("%4s %li"
Note the 4. It gives a maxlength for the string. On top of that, you should not check scanf
for EOF
. Read the documentation about what it actually does return.
add a comment |
Variables in programming languages have different size. In c++, long
size is at least 32 bits (according to Fundamental Types (C++) (Microsoft website) and C++ Data Types (tutorialspoint website)). So, when you assign a number bigger which needs more bits to store number, it turns to negative (one's complement and 2's complement system in storing numbers).
read C++ Data Types (tutorialspoint website) about the size of each variable in c++ language and also wikipedia has good articles about one's complement and two's complement.
1
long size is 4 bytes. – Is that so? Reference?
– Swordfish
Nov 25 '18 at 15:20
1
"In c++, long size is 4 bytes." Wrong. Downvoted.
– n.m.
Nov 25 '18 at 15:21
1
@meowgoesthedog I read other links now, and I understood this is at least 4 bytes and updated my answer.
– hamid ghasemi
Nov 25 '18 at 15:29
2
What platforms have something other than 8 bit char? also seeCHAR_BIT
.
– Swordfish
Nov 25 '18 at 15:42
1
It would be nice if you gave your links a title other than "link" so people know where they take them without having to click. Most websites have headlines that are appropriate as title for a link. Please also note, that relevant information to be considered to be part of your answer has to be contained within it. Use cites if appropriate. Links can become invalid and when that happens the answer is worthless.
– Swordfish
Nov 25 '18 at 15:45
|
show 9 more comments
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%2f53468809%2fsimple-calculator-not-working-with-big-numbers-in-c%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
You're overflowing the variables.
You have two options here. Either find a solution for arbitrarily large numbers (there are libraries you can use) or accept that you cannot use numbers that are to big.
The largest number a signed 64 bit integer can hold (it's obvious from your output that long
is 64 bit on your system) is 9223372036854775807 and the largest for an unsigned 64 bit is 18446744073709551615.
It could be worth mentioning that overflow only have a defined behavior for unsigned types.
Other comments
char[4]
is not enough to hold "plus", since you need place for the ''
. Use char[5]
instead.
There is no reason to use pointers in this code. Unless you have a very good reason, I would suggest changing to:
long plus(long current, long num) {
return current + num;
}
Which in turn means that you can completely skip the function. You don't need a function to perform addition of two integers since you have the +
operator to do that very thing.
Also, your usage of scanf
is unsafe. You may write past the end of the array. Do this instead: scanf("%4s %li"
Note the 4. It gives a maxlength for the string. On top of that, you should not check scanf
for EOF
. Read the documentation about what it actually does return.
add a comment |
You're overflowing the variables.
You have two options here. Either find a solution for arbitrarily large numbers (there are libraries you can use) or accept that you cannot use numbers that are to big.
The largest number a signed 64 bit integer can hold (it's obvious from your output that long
is 64 bit on your system) is 9223372036854775807 and the largest for an unsigned 64 bit is 18446744073709551615.
It could be worth mentioning that overflow only have a defined behavior for unsigned types.
Other comments
char[4]
is not enough to hold "plus", since you need place for the ''
. Use char[5]
instead.
There is no reason to use pointers in this code. Unless you have a very good reason, I would suggest changing to:
long plus(long current, long num) {
return current + num;
}
Which in turn means that you can completely skip the function. You don't need a function to perform addition of two integers since you have the +
operator to do that very thing.
Also, your usage of scanf
is unsafe. You may write past the end of the array. Do this instead: scanf("%4s %li"
Note the 4. It gives a maxlength for the string. On top of that, you should not check scanf
for EOF
. Read the documentation about what it actually does return.
add a comment |
You're overflowing the variables.
You have two options here. Either find a solution for arbitrarily large numbers (there are libraries you can use) or accept that you cannot use numbers that are to big.
The largest number a signed 64 bit integer can hold (it's obvious from your output that long
is 64 bit on your system) is 9223372036854775807 and the largest for an unsigned 64 bit is 18446744073709551615.
It could be worth mentioning that overflow only have a defined behavior for unsigned types.
Other comments
char[4]
is not enough to hold "plus", since you need place for the ''
. Use char[5]
instead.
There is no reason to use pointers in this code. Unless you have a very good reason, I would suggest changing to:
long plus(long current, long num) {
return current + num;
}
Which in turn means that you can completely skip the function. You don't need a function to perform addition of two integers since you have the +
operator to do that very thing.
Also, your usage of scanf
is unsafe. You may write past the end of the array. Do this instead: scanf("%4s %li"
Note the 4. It gives a maxlength for the string. On top of that, you should not check scanf
for EOF
. Read the documentation about what it actually does return.
You're overflowing the variables.
You have two options here. Either find a solution for arbitrarily large numbers (there are libraries you can use) or accept that you cannot use numbers that are to big.
The largest number a signed 64 bit integer can hold (it's obvious from your output that long
is 64 bit on your system) is 9223372036854775807 and the largest for an unsigned 64 bit is 18446744073709551615.
It could be worth mentioning that overflow only have a defined behavior for unsigned types.
Other comments
char[4]
is not enough to hold "plus", since you need place for the ''
. Use char[5]
instead.
There is no reason to use pointers in this code. Unless you have a very good reason, I would suggest changing to:
long plus(long current, long num) {
return current + num;
}
Which in turn means that you can completely skip the function. You don't need a function to perform addition of two integers since you have the +
operator to do that very thing.
Also, your usage of scanf
is unsafe. You may write past the end of the array. Do this instead: scanf("%4s %li"
Note the 4. It gives a maxlength for the string. On top of that, you should not check scanf
for EOF
. Read the documentation about what it actually does return.
edited Nov 25 '18 at 16:28
answered Nov 25 '18 at 15:16
BromanBroman
7,204112543
7,204112543
add a comment |
add a comment |
Variables in programming languages have different size. In c++, long
size is at least 32 bits (according to Fundamental Types (C++) (Microsoft website) and C++ Data Types (tutorialspoint website)). So, when you assign a number bigger which needs more bits to store number, it turns to negative (one's complement and 2's complement system in storing numbers).
read C++ Data Types (tutorialspoint website) about the size of each variable in c++ language and also wikipedia has good articles about one's complement and two's complement.
1
long size is 4 bytes. – Is that so? Reference?
– Swordfish
Nov 25 '18 at 15:20
1
"In c++, long size is 4 bytes." Wrong. Downvoted.
– n.m.
Nov 25 '18 at 15:21
1
@meowgoesthedog I read other links now, and I understood this is at least 4 bytes and updated my answer.
– hamid ghasemi
Nov 25 '18 at 15:29
2
What platforms have something other than 8 bit char? also seeCHAR_BIT
.
– Swordfish
Nov 25 '18 at 15:42
1
It would be nice if you gave your links a title other than "link" so people know where they take them without having to click. Most websites have headlines that are appropriate as title for a link. Please also note, that relevant information to be considered to be part of your answer has to be contained within it. Use cites if appropriate. Links can become invalid and when that happens the answer is worthless.
– Swordfish
Nov 25 '18 at 15:45
|
show 9 more comments
Variables in programming languages have different size. In c++, long
size is at least 32 bits (according to Fundamental Types (C++) (Microsoft website) and C++ Data Types (tutorialspoint website)). So, when you assign a number bigger which needs more bits to store number, it turns to negative (one's complement and 2's complement system in storing numbers).
read C++ Data Types (tutorialspoint website) about the size of each variable in c++ language and also wikipedia has good articles about one's complement and two's complement.
1
long size is 4 bytes. – Is that so? Reference?
– Swordfish
Nov 25 '18 at 15:20
1
"In c++, long size is 4 bytes." Wrong. Downvoted.
– n.m.
Nov 25 '18 at 15:21
1
@meowgoesthedog I read other links now, and I understood this is at least 4 bytes and updated my answer.
– hamid ghasemi
Nov 25 '18 at 15:29
2
What platforms have something other than 8 bit char? also seeCHAR_BIT
.
– Swordfish
Nov 25 '18 at 15:42
1
It would be nice if you gave your links a title other than "link" so people know where they take them without having to click. Most websites have headlines that are appropriate as title for a link. Please also note, that relevant information to be considered to be part of your answer has to be contained within it. Use cites if appropriate. Links can become invalid and when that happens the answer is worthless.
– Swordfish
Nov 25 '18 at 15:45
|
show 9 more comments
Variables in programming languages have different size. In c++, long
size is at least 32 bits (according to Fundamental Types (C++) (Microsoft website) and C++ Data Types (tutorialspoint website)). So, when you assign a number bigger which needs more bits to store number, it turns to negative (one's complement and 2's complement system in storing numbers).
read C++ Data Types (tutorialspoint website) about the size of each variable in c++ language and also wikipedia has good articles about one's complement and two's complement.
Variables in programming languages have different size. In c++, long
size is at least 32 bits (according to Fundamental Types (C++) (Microsoft website) and C++ Data Types (tutorialspoint website)). So, when you assign a number bigger which needs more bits to store number, it turns to negative (one's complement and 2's complement system in storing numbers).
read C++ Data Types (tutorialspoint website) about the size of each variable in c++ language and also wikipedia has good articles about one's complement and two's complement.
edited Nov 25 '18 at 15:51
answered Nov 25 '18 at 15:17
hamid ghasemihamid ghasemi
6812725
6812725
1
long size is 4 bytes. – Is that so? Reference?
– Swordfish
Nov 25 '18 at 15:20
1
"In c++, long size is 4 bytes." Wrong. Downvoted.
– n.m.
Nov 25 '18 at 15:21
1
@meowgoesthedog I read other links now, and I understood this is at least 4 bytes and updated my answer.
– hamid ghasemi
Nov 25 '18 at 15:29
2
What platforms have something other than 8 bit char? also seeCHAR_BIT
.
– Swordfish
Nov 25 '18 at 15:42
1
It would be nice if you gave your links a title other than "link" so people know where they take them without having to click. Most websites have headlines that are appropriate as title for a link. Please also note, that relevant information to be considered to be part of your answer has to be contained within it. Use cites if appropriate. Links can become invalid and when that happens the answer is worthless.
– Swordfish
Nov 25 '18 at 15:45
|
show 9 more comments
1
long size is 4 bytes. – Is that so? Reference?
– Swordfish
Nov 25 '18 at 15:20
1
"In c++, long size is 4 bytes." Wrong. Downvoted.
– n.m.
Nov 25 '18 at 15:21
1
@meowgoesthedog I read other links now, and I understood this is at least 4 bytes and updated my answer.
– hamid ghasemi
Nov 25 '18 at 15:29
2
What platforms have something other than 8 bit char? also seeCHAR_BIT
.
– Swordfish
Nov 25 '18 at 15:42
1
It would be nice if you gave your links a title other than "link" so people know where they take them without having to click. Most websites have headlines that are appropriate as title for a link. Please also note, that relevant information to be considered to be part of your answer has to be contained within it. Use cites if appropriate. Links can become invalid and when that happens the answer is worthless.
– Swordfish
Nov 25 '18 at 15:45
1
1
long size is 4 bytes. – Is that so? Reference?
– Swordfish
Nov 25 '18 at 15:20
long size is 4 bytes. – Is that so? Reference?
– Swordfish
Nov 25 '18 at 15:20
1
1
"In c++, long size is 4 bytes." Wrong. Downvoted.
– n.m.
Nov 25 '18 at 15:21
"In c++, long size is 4 bytes." Wrong. Downvoted.
– n.m.
Nov 25 '18 at 15:21
1
1
@meowgoesthedog I read other links now, and I understood this is at least 4 bytes and updated my answer.
– hamid ghasemi
Nov 25 '18 at 15:29
@meowgoesthedog I read other links now, and I understood this is at least 4 bytes and updated my answer.
– hamid ghasemi
Nov 25 '18 at 15:29
2
2
What platforms have something other than 8 bit char? also see
CHAR_BIT
.– Swordfish
Nov 25 '18 at 15:42
What platforms have something other than 8 bit char? also see
CHAR_BIT
.– Swordfish
Nov 25 '18 at 15:42
1
1
It would be nice if you gave your links a title other than "link" so people know where they take them without having to click. Most websites have headlines that are appropriate as title for a link. Please also note, that relevant information to be considered to be part of your answer has to be contained within it. Use cites if appropriate. Links can become invalid and when that happens the answer is worthless.
– Swordfish
Nov 25 '18 at 15:45
It would be nice if you gave your links a title other than "link" so people know where they take them without having to click. Most websites have headlines that are appropriate as title for a link. Please also note, that relevant information to be considered to be part of your answer has to be contained within it. Use cites if appropriate. Links can become invalid and when that happens the answer is worthless.
– Swordfish
Nov 25 '18 at 15:45
|
show 9 more comments
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%2f53468809%2fsimple-calculator-not-working-with-big-numbers-in-c%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 should remove around 90% of the input and output. It is not needed to understand your problem.
– Broman
Nov 25 '18 at 15:18
Unrelated to your numbers but most likely undefined behaviour:
char operator[4];
is too small for"krat"
and"plus"
which are 4 characters + 1 for the terminating 0 long. sooperator
should bechar operator[5];
and you should limitscanf()
to read 4 characters with"%4s"
.– Swordfish
Nov 25 '18 at 15:19
And reason why you didn't simply use
result = operator(result, num)
?– Groo
Nov 25 '18 at 15:20
You have used
long
. Why? Wouldn'tshort
orint
suffice? Why, in your opinion, does C have these different types?– n.m.
Nov 25 '18 at 15:24
If your compiler supports it you could use
<stdint.h>
and replacelong
withint64_t
.– Swordfish
Nov 25 '18 at 15:26