How to read and assign big integers in C?
I am trying to assign big integer value to a variable in c and when I print I only get 10123456.
What is the issue?
int main(){
long a = 1234567890123456;
printf("n",sizeof(a));
printf("%ld",a);
}
c
|
show 2 more comments
I am trying to assign big integer value to a variable in c and when I print I only get 10123456.
What is the issue?
int main(){
long a = 1234567890123456;
printf("n",sizeof(a));
printf("%ld",a);
}
c
7
long
isn't that long, trylong long
– harold
Aug 20 '13 at 9:47
2
What printsprintf("n",sizeof(a));
?
– nouney
Aug 20 '13 at 9:48
stackoverflow.com/questions/124332/…
– Maroun
Aug 20 '13 at 9:49
Uselong long int
or some compiler specific types like__int64
orint64_t
.
– bkausbk
Aug 20 '13 at 9:51
1
@keltar: Look at the comment by nouney
– Don't You Worry Child
Aug 20 '13 at 10:18
|
show 2 more comments
I am trying to assign big integer value to a variable in c and when I print I only get 10123456.
What is the issue?
int main(){
long a = 1234567890123456;
printf("n",sizeof(a));
printf("%ld",a);
}
c
I am trying to assign big integer value to a variable in c and when I print I only get 10123456.
What is the issue?
int main(){
long a = 1234567890123456;
printf("n",sizeof(a));
printf("%ld",a);
}
c
c
edited Oct 7 '15 at 8:43


TryinHard
2,88122143
2,88122143
asked Aug 20 '13 at 9:46
user2684719
7
long
isn't that long, trylong long
– harold
Aug 20 '13 at 9:47
2
What printsprintf("n",sizeof(a));
?
– nouney
Aug 20 '13 at 9:48
stackoverflow.com/questions/124332/…
– Maroun
Aug 20 '13 at 9:49
Uselong long int
or some compiler specific types like__int64
orint64_t
.
– bkausbk
Aug 20 '13 at 9:51
1
@keltar: Look at the comment by nouney
– Don't You Worry Child
Aug 20 '13 at 10:18
|
show 2 more comments
7
long
isn't that long, trylong long
– harold
Aug 20 '13 at 9:47
2
What printsprintf("n",sizeof(a));
?
– nouney
Aug 20 '13 at 9:48
stackoverflow.com/questions/124332/…
– Maroun
Aug 20 '13 at 9:49
Uselong long int
or some compiler specific types like__int64
orint64_t
.
– bkausbk
Aug 20 '13 at 9:51
1
@keltar: Look at the comment by nouney
– Don't You Worry Child
Aug 20 '13 at 10:18
7
7
long
isn't that long, try long long
– harold
Aug 20 '13 at 9:47
long
isn't that long, try long long
– harold
Aug 20 '13 at 9:47
2
2
What prints
printf("n",sizeof(a));
?– nouney
Aug 20 '13 at 9:48
What prints
printf("n",sizeof(a));
?– nouney
Aug 20 '13 at 9:48
stackoverflow.com/questions/124332/…
– Maroun
Aug 20 '13 at 9:49
stackoverflow.com/questions/124332/…
– Maroun
Aug 20 '13 at 9:49
Use
long long int
or some compiler specific types like __int64
or int64_t
.– bkausbk
Aug 20 '13 at 9:51
Use
long long int
or some compiler specific types like __int64
or int64_t
.– bkausbk
Aug 20 '13 at 9:51
1
1
@keltar: Look at the comment by nouney
– Don't You Worry Child
Aug 20 '13 at 10:18
@keltar: Look at the comment by nouney
– Don't You Worry Child
Aug 20 '13 at 10:18
|
show 2 more comments
3 Answers
3
active
oldest
votes
Largest integer type is:
unsigned long long
dont forget about ULL suffix.
or if you need larger integers, take a look for some bigint libraries like gmp.
Of course, there is also long long
, but it is also for negative integers and have smaller limits.
Type min max
(signed) long long -9223372036854775808 9223372036854775807
unsigned long long 0 18446744073709551615
In C 1999 and later, the largest integer types described by the standard areintmax_t
anduintmax_t
. They are at least as large asunsigned long long
and might be larger.
– Eric Postpischil
Aug 20 '13 at 13:08
add a comment |
long a = 1234567890123456L;
If long is long enough, depends on compiler/OS. If not
long long a = 1234567890123456LL;
add a comment |
If the number is greater than 64-bit, i.e. longer than what unsigned long long
can hold, then no data type in C other than string(char
) will be able to accomodate that value, store it as a string & try writing your own functions to operate (e.g. add, subtract, etc.) on these "very large numbers".
There is nostring
in C
– Zaffy
Aug 20 '13 at 10:07
1
@Zaffy I know, that's why I wrote string(char
), sorry for writing string as acode
.
– Don't You Worry Child
Aug 20 '13 at 10:20
@Zaffy C11 7.1.1 1 "Definitions of terms. A string is a contiguous sequence of characters terminated by and including the first null character. ..."
– chux
Aug 20 '13 at 12:22
1
It is not generally true that an integer greater than what fits into 64 bits is not representable in some integer type in C. C 1999 and later provides for a variety of integer types, and an implementation may provide integer types of any size.
– Eric Postpischil
Aug 20 '13 at 13:09
1
@nishant: Clause 7.20 of the C standard describes thestdint.h
headers, which describes types such asintmax_t
anduintmax_t
for integers of the greatest width in the implementation, along with types for specific widths, macros for writing constants of the various types, and other embellishments. Theinttypes.h
header in clause 7.8 provides format specifiers for using the types withprintf
.
– Eric Postpischil
Aug 20 '13 at 17:51
|
show 2 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%2f18331913%2fhow-to-read-and-assign-big-integers-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Largest integer type is:
unsigned long long
dont forget about ULL suffix.
or if you need larger integers, take a look for some bigint libraries like gmp.
Of course, there is also long long
, but it is also for negative integers and have smaller limits.
Type min max
(signed) long long -9223372036854775808 9223372036854775807
unsigned long long 0 18446744073709551615
In C 1999 and later, the largest integer types described by the standard areintmax_t
anduintmax_t
. They are at least as large asunsigned long long
and might be larger.
– Eric Postpischil
Aug 20 '13 at 13:08
add a comment |
Largest integer type is:
unsigned long long
dont forget about ULL suffix.
or if you need larger integers, take a look for some bigint libraries like gmp.
Of course, there is also long long
, but it is also for negative integers and have smaller limits.
Type min max
(signed) long long -9223372036854775808 9223372036854775807
unsigned long long 0 18446744073709551615
In C 1999 and later, the largest integer types described by the standard areintmax_t
anduintmax_t
. They are at least as large asunsigned long long
and might be larger.
– Eric Postpischil
Aug 20 '13 at 13:08
add a comment |
Largest integer type is:
unsigned long long
dont forget about ULL suffix.
or if you need larger integers, take a look for some bigint libraries like gmp.
Of course, there is also long long
, but it is also for negative integers and have smaller limits.
Type min max
(signed) long long -9223372036854775808 9223372036854775807
unsigned long long 0 18446744073709551615
Largest integer type is:
unsigned long long
dont forget about ULL suffix.
or if you need larger integers, take a look for some bigint libraries like gmp.
Of course, there is also long long
, but it is also for negative integers and have smaller limits.
Type min max
(signed) long long -9223372036854775808 9223372036854775807
unsigned long long 0 18446744073709551615
edited Aug 20 '13 at 10:04
answered Aug 20 '13 at 9:50
ZaffyZaffy
12k83664
12k83664
In C 1999 and later, the largest integer types described by the standard areintmax_t
anduintmax_t
. They are at least as large asunsigned long long
and might be larger.
– Eric Postpischil
Aug 20 '13 at 13:08
add a comment |
In C 1999 and later, the largest integer types described by the standard areintmax_t
anduintmax_t
. They are at least as large asunsigned long long
and might be larger.
– Eric Postpischil
Aug 20 '13 at 13:08
In C 1999 and later, the largest integer types described by the standard are
intmax_t
and uintmax_t
. They are at least as large as unsigned long long
and might be larger.– Eric Postpischil
Aug 20 '13 at 13:08
In C 1999 and later, the largest integer types described by the standard are
intmax_t
and uintmax_t
. They are at least as large as unsigned long long
and might be larger.– Eric Postpischil
Aug 20 '13 at 13:08
add a comment |
long a = 1234567890123456L;
If long is long enough, depends on compiler/OS. If not
long long a = 1234567890123456LL;
add a comment |
long a = 1234567890123456L;
If long is long enough, depends on compiler/OS. If not
long long a = 1234567890123456LL;
add a comment |
long a = 1234567890123456L;
If long is long enough, depends on compiler/OS. If not
long long a = 1234567890123456LL;
long a = 1234567890123456L;
If long is long enough, depends on compiler/OS. If not
long long a = 1234567890123456LL;
answered Aug 20 '13 at 9:50
keltarkeltar
12.5k12534
12.5k12534
add a comment |
add a comment |
If the number is greater than 64-bit, i.e. longer than what unsigned long long
can hold, then no data type in C other than string(char
) will be able to accomodate that value, store it as a string & try writing your own functions to operate (e.g. add, subtract, etc.) on these "very large numbers".
There is nostring
in C
– Zaffy
Aug 20 '13 at 10:07
1
@Zaffy I know, that's why I wrote string(char
), sorry for writing string as acode
.
– Don't You Worry Child
Aug 20 '13 at 10:20
@Zaffy C11 7.1.1 1 "Definitions of terms. A string is a contiguous sequence of characters terminated by and including the first null character. ..."
– chux
Aug 20 '13 at 12:22
1
It is not generally true that an integer greater than what fits into 64 bits is not representable in some integer type in C. C 1999 and later provides for a variety of integer types, and an implementation may provide integer types of any size.
– Eric Postpischil
Aug 20 '13 at 13:09
1
@nishant: Clause 7.20 of the C standard describes thestdint.h
headers, which describes types such asintmax_t
anduintmax_t
for integers of the greatest width in the implementation, along with types for specific widths, macros for writing constants of the various types, and other embellishments. Theinttypes.h
header in clause 7.8 provides format specifiers for using the types withprintf
.
– Eric Postpischil
Aug 20 '13 at 17:51
|
show 2 more comments
If the number is greater than 64-bit, i.e. longer than what unsigned long long
can hold, then no data type in C other than string(char
) will be able to accomodate that value, store it as a string & try writing your own functions to operate (e.g. add, subtract, etc.) on these "very large numbers".
There is nostring
in C
– Zaffy
Aug 20 '13 at 10:07
1
@Zaffy I know, that's why I wrote string(char
), sorry for writing string as acode
.
– Don't You Worry Child
Aug 20 '13 at 10:20
@Zaffy C11 7.1.1 1 "Definitions of terms. A string is a contiguous sequence of characters terminated by and including the first null character. ..."
– chux
Aug 20 '13 at 12:22
1
It is not generally true that an integer greater than what fits into 64 bits is not representable in some integer type in C. C 1999 and later provides for a variety of integer types, and an implementation may provide integer types of any size.
– Eric Postpischil
Aug 20 '13 at 13:09
1
@nishant: Clause 7.20 of the C standard describes thestdint.h
headers, which describes types such asintmax_t
anduintmax_t
for integers of the greatest width in the implementation, along with types for specific widths, macros for writing constants of the various types, and other embellishments. Theinttypes.h
header in clause 7.8 provides format specifiers for using the types withprintf
.
– Eric Postpischil
Aug 20 '13 at 17:51
|
show 2 more comments
If the number is greater than 64-bit, i.e. longer than what unsigned long long
can hold, then no data type in C other than string(char
) will be able to accomodate that value, store it as a string & try writing your own functions to operate (e.g. add, subtract, etc.) on these "very large numbers".
If the number is greater than 64-bit, i.e. longer than what unsigned long long
can hold, then no data type in C other than string(char
) will be able to accomodate that value, store it as a string & try writing your own functions to operate (e.g. add, subtract, etc.) on these "very large numbers".
edited Aug 20 '13 at 10:21
answered Aug 20 '13 at 10:00


Don't You Worry ChildDon't You Worry Child
4,80111844
4,80111844
There is nostring
in C
– Zaffy
Aug 20 '13 at 10:07
1
@Zaffy I know, that's why I wrote string(char
), sorry for writing string as acode
.
– Don't You Worry Child
Aug 20 '13 at 10:20
@Zaffy C11 7.1.1 1 "Definitions of terms. A string is a contiguous sequence of characters terminated by and including the first null character. ..."
– chux
Aug 20 '13 at 12:22
1
It is not generally true that an integer greater than what fits into 64 bits is not representable in some integer type in C. C 1999 and later provides for a variety of integer types, and an implementation may provide integer types of any size.
– Eric Postpischil
Aug 20 '13 at 13:09
1
@nishant: Clause 7.20 of the C standard describes thestdint.h
headers, which describes types such asintmax_t
anduintmax_t
for integers of the greatest width in the implementation, along with types for specific widths, macros for writing constants of the various types, and other embellishments. Theinttypes.h
header in clause 7.8 provides format specifiers for using the types withprintf
.
– Eric Postpischil
Aug 20 '13 at 17:51
|
show 2 more comments
There is nostring
in C
– Zaffy
Aug 20 '13 at 10:07
1
@Zaffy I know, that's why I wrote string(char
), sorry for writing string as acode
.
– Don't You Worry Child
Aug 20 '13 at 10:20
@Zaffy C11 7.1.1 1 "Definitions of terms. A string is a contiguous sequence of characters terminated by and including the first null character. ..."
– chux
Aug 20 '13 at 12:22
1
It is not generally true that an integer greater than what fits into 64 bits is not representable in some integer type in C. C 1999 and later provides for a variety of integer types, and an implementation may provide integer types of any size.
– Eric Postpischil
Aug 20 '13 at 13:09
1
@nishant: Clause 7.20 of the C standard describes thestdint.h
headers, which describes types such asintmax_t
anduintmax_t
for integers of the greatest width in the implementation, along with types for specific widths, macros for writing constants of the various types, and other embellishments. Theinttypes.h
header in clause 7.8 provides format specifiers for using the types withprintf
.
– Eric Postpischil
Aug 20 '13 at 17:51
There is no
string
in C– Zaffy
Aug 20 '13 at 10:07
There is no
string
in C– Zaffy
Aug 20 '13 at 10:07
1
1
@Zaffy I know, that's why I wrote string(
char
), sorry for writing string as a code
.– Don't You Worry Child
Aug 20 '13 at 10:20
@Zaffy I know, that's why I wrote string(
char
), sorry for writing string as a code
.– Don't You Worry Child
Aug 20 '13 at 10:20
@Zaffy C11 7.1.1 1 "Definitions of terms. A string is a contiguous sequence of characters terminated by and including the first null character. ..."
– chux
Aug 20 '13 at 12:22
@Zaffy C11 7.1.1 1 "Definitions of terms. A string is a contiguous sequence of characters terminated by and including the first null character. ..."
– chux
Aug 20 '13 at 12:22
1
1
It is not generally true that an integer greater than what fits into 64 bits is not representable in some integer type in C. C 1999 and later provides for a variety of integer types, and an implementation may provide integer types of any size.
– Eric Postpischil
Aug 20 '13 at 13:09
It is not generally true that an integer greater than what fits into 64 bits is not representable in some integer type in C. C 1999 and later provides for a variety of integer types, and an implementation may provide integer types of any size.
– Eric Postpischil
Aug 20 '13 at 13:09
1
1
@nishant: Clause 7.20 of the C standard describes the
stdint.h
headers, which describes types such as intmax_t
and uintmax_t
for integers of the greatest width in the implementation, along with types for specific widths, macros for writing constants of the various types, and other embellishments. The inttypes.h
header in clause 7.8 provides format specifiers for using the types with printf
.– Eric Postpischil
Aug 20 '13 at 17:51
@nishant: Clause 7.20 of the C standard describes the
stdint.h
headers, which describes types such as intmax_t
and uintmax_t
for integers of the greatest width in the implementation, along with types for specific widths, macros for writing constants of the various types, and other embellishments. The inttypes.h
header in clause 7.8 provides format specifiers for using the types with printf
.– Eric Postpischil
Aug 20 '13 at 17:51
|
show 2 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%2f18331913%2fhow-to-read-and-assign-big-integers-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
7
long
isn't that long, trylong long
– harold
Aug 20 '13 at 9:47
2
What prints
printf("n",sizeof(a));
?– nouney
Aug 20 '13 at 9:48
stackoverflow.com/questions/124332/…
– Maroun
Aug 20 '13 at 9:49
Use
long long int
or some compiler specific types like__int64
orint64_t
.– bkausbk
Aug 20 '13 at 9:51
1
@keltar: Look at the comment by nouney
– Don't You Worry Child
Aug 20 '13 at 10:18