-4

aim: i had a requirement for accuracy i need 100 digits to be displayed or atleast 50 digits

try 1: first i had used integer variable then it displayed only 10 digits

try 2: with the help of my friend i used pointers concept it successfully had input 50 to 100 digits but pointer variable display only 10 digits

the program i had written was

#include <stdio.h>
#include <string.h>

main() 
 {
    int *p;
    p=(int*)malloc(100*sizeof(int));
    *p=1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890;
    printf("%d",*p); 
 }

on executing the above procedure i'm getting a garbage value

thanks in advance

Smart003
  • 1,119
  • 2
  • 16
  • 31
  • 6
    Please turn on your compiler's warnings (`-Wall` at _least_ for GCC). Your code has more problems than lines of code. – Mat Oct 25 '14 at 06:58
  • 1
    `#include ` instead of ``, and you are reserving space for an array of 100 `int`'s, not for an `int` capable to represent 100 digits – David Ranieri Oct 25 '14 at 07:02
  • 1
    Have you thought about this number. 2 digits about the size of a class/team, 4 digits in terms of a school. 5 digits a small village 6 digits a small city. 8 digits we are talking capitals. (BTW not up to 10 yet). Not we need bacteria in your gut to the 10s millions. Get the picture – Ed Heal Oct 25 '14 at 07:11
  • The python language has big number support by default. Just so you don't waste too much time reinventing the wheel... – Paul Oct 25 '14 at 07:33
  • 1. A pointer doesn't **display** anything, it only stores the address to some object. 2. `malloc` is defined in `stdlib.h`. 3. [Don't cast the result of malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc), it'll hide problems when malloc is undefined because of not including stdlib.h like in your case – phuclv Oct 25 '14 at 07:57
  • 2
    Decimal places and pointers have nothing to do with each other. – whatsisname Oct 25 '14 at 07:24
  • That one doesn't even compile for me. I get an C2177 "constant too large". What ehm ... smart compiler do you use that eats this construct w/o complaining? BTW, the line `p=(int*)malloc(100*sizeof(int));` alone should make you think about what you are trying to do. You want an `int` with 100x the size of an `int` ... [and you honestly expect this to work](http://therewillbeasia.files.wordpress.com/2013/04/indiatrain.jpg)? – JensG Oct 25 '14 at 09:20
  • 2
    You see to be confusing 100 *digits* and 100 *integers* – Andrew Oct 25 '14 at 10:47

5 Answers5

4

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) ...

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
3

To answer the first part, a single signed integer (assuming 32 bits) can take values between -2,147,483,648 and +2,147,483,647

This is where your ten digits comes from.

Depending on your compiler, you may have unsigned long long available (64 bits) but that still only offers you a maximum or 0..18,446,744,073,709,551,615 (20 digits)

To handle 100-digit numbers will require some jiggery-pokery!

Andrew
  • 2,046
  • 1
  • 24
  • 37
  • Like implementing a BCD algorithm or using a library like [GMP](http://gmplib.org/) to allow for numbers of arbitrary precision. Of course, it has its own printf function since the standard one cannot handle such a type. –  Oct 25 '14 at 13:38
2

You can't store such a long digit in an int variable or a pointer which is pointing to type int.

If int is of 32 bit, the range is [−2147483647,+2147483647]. C data types

To store the large value you can use a char pointer and initialized it with the string constant.

For example:


#include <stdio.h>
#include <stdlib.h>

main() 
{
    char *p = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
    printf("%s\n",p); 
}
ani627
  • 5,578
  • 8
  • 39
  • 45
2
int * p = malloc(100*sizeof(int));

Allocates memory for an array of 100 int.

*p= ....

In fact is the same as

p[0]=...

So this

*p=1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890;

assigns to an int.

An int may hold not more then a 32 bits (less on some platforms). The largest 32bit (positive) integer is 2147483647.

alk
  • 69,737
  • 10
  • 105
  • 255
2

C is a language close to the hardware. So you need to understand the storage capabilities and limitations of its data types.

An int on a PC is only able to store 32 bit or 64 bit values, depending on your hardware and OS version.

Yes, you can implement routines for handling larger value, and using a linked list of digits is one way do it.

mvw
  • 5,075
  • 1
  • 28
  • 34
  • Don't re-implement Bignum arithmetic by yourself. The algorithms are difficult if you want them to be efficient. Use some existing Bignum library! – Basile Starynkevitch Oct 25 '14 at 07:14
  • The question gives no background. It might be an assignment for some programming education or a requirement during application development. In the latter case the advice to stick to existing tested code would hold for any non trivial piece of software. – mvw Oct 25 '14 at 08:06