You need to use a bignum (arbitrary precision big numbers) library, since native int
or long
have a precision limited by the C implementation - ie the hardware (often 32 or 64 bits; see also <stdint.h>
header and int32_t
& int64_t
types). I suggest using GMPlib; you need to be fluent in C to use such libraries. Read documentation of GMLib.
Don't try to code bignumber arithmetic yourself. You'll use inefficient algorithms. Efficient algorithms for bignums are difficult to understand and to invent. You can still get a PhD on that. So use some existing bignum library.
So you might code something like the following to multiply your bignum by 137 and print the result
mpz_t bign;
mpz_t bigr;
// initialize bign from a literal string
mpz_init_set_str (bign,
"12345678901234567890123456789012345678901234567890"
"12345678901234567890123456789012345678901234567890",
10);
// initialize bigr
mpz_init(bigr);
/// compute bigr = bign * 137 (137 is a long, not a bignum)
mpz_mul_si(bigr, bign, 137L);
/// print bigr on stdout in base 10
mpz_out_str (stdout, 10, bigr);
/// clear all the numbers
mpz_clear(bign);
mpz_clear(bigr);
On a Linux system with GMP installed, you could compile such a code (in yoursource.c
) using:
gcc -Wall -Wextra -g yoursource.c -lgmp -o yourprog
On other systems you might need some -I
and -L
arguments to gcc
. When you have debugged your program, ask the compiler to optimize it with -O2
Notice that some languages (Common Lisp, Scheme, Python) have built-in bignums.
BTW, your main
function is incorrectly defined. It should be defined as int main(int argc, char**argv)
...