Thread: variable problem (very big number)

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    variable problem (very big number) [solved]

    Hello,

    I try to make a exercise where I have to store a number which exist of 12 numbers in a variable. The number looks like this : 869144826000
    But as far as I know a long int or a unsigned int can go to a number exist of 9 numbers.

    How can I store this number in a variable ?
    I know there are arrays but in this book but that is chapter 8 and I work now in chapter5.

    I use this book : C Programming : A modern Approach 2nd Edition.

    Roelof



    Roelof
    Last edited by roelof; 05-23-2011 at 06:45 AM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Bangalore
    Posts
    20
    You can store this number using structure like
    Code:
    struct Number
    {
    	double base;
    	int power;
    };
    Then you number is like a * 10^b.
    Last edited by ShashiKantSuman; 05-23-2011 at 02:46 AM.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by ShashiKantSuman View Post
    You can store this number using structure like
    Code:
    struct Number
    {
    	double base;
    	int power;
    };
    Then you number is like a * 10^b.
    I am new in c programming. Could you please give an example code in using that struct?

    how would you assign the value "869144826000" to that?

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by ShashiKantSuman View Post
    You can store this number using structure like
    Code:
    struct Number
    {
    	double base;
    	int power;
    };
    Then you number is like a * 10^b.
    I am new in c programming. Could you please give an example code in using that struct?

    how would you assign the value "869144826000" to that?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hoi,

    Structs are not the solution.

    Im also new in C programming and structs are even later discussed in the book then array's.

    Roelof

  6. #6
    Registered User
    Join Date
    Apr 2011
    Location
    Bangalore
    Posts
    20
    This is a sample program for multiplication.
    Code:
    #include <stdio.h>
    # include <stdlib.h>
    typedef	struct Number
    {
    	float Base;
    	int Power;
    }NUMBER;
    int main()
    {
    	NUMBER *num1,*num2,*num3;
    	num1 = (NUMBER*)malloc(sizeof(NUMBER));
    	num2 = (NUMBER*)malloc(sizeof(NUMBER));
    	num3 = (NUMBER*)malloc(sizeof(NUMBER));
    	printf("Enter two number in a * 10^b format\n\n");
    	scanf("%f%d%f%d",&num1->Base,&num1->Power,&num2->Base,&num2->Power);
    	num3->Base = num1->Base * num2->Base;
    	num3->Power = num1->Power+num2->Power;
    	printf("Result is: %f * 10^%d\n",num3->Base,num3->Power);
    	getch();
    	return 1;
    }
    how would you assign the value "869144826000" to that?
    You can assign as 8.69144 * 10 ^11.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why wouldn't you just use a long long int ?
    (uint64_t if you have the stdint.h header)


    They get up to 15 digits very nicely.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I did not know that a long long int exist.
    But it does the job.

    Problem solved.

    Roelof

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You'r compiler's documentation should give you details on the types and their ranges...

    What compiler are you using... (I've probably asked this before, but can't recall right now)

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    230
    hello,

    I use GCC on a Debain testing box.

    Roelof

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... I'm not linux people so maybe one of the others can point you to the right place...

    One thing you can try in the mean time is to open the stdint.h file in a text editor --don't make changes!-- and look at what's available there.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I don't know of where to find a list of valid types supported by gcc, other than knowing that gcc supports most of C99, and thus should support valid C99 types, including long long. As for what's valid C99, if you have a really good, trustworthy book, they might mention all the integral types, or you can check out the standard: http://www.open-std.org/jtc1/sc22/wg...docs/n1362.pdf.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    I don't know of where to find a list of valid types supported by gcc, other than knowing that gcc supports most of C99, and thus should support valid C99 types, including long long. As for what's valid C99, if you have a really good, trustworthy book, they might mention all the integral types, or you can check out the standard: http://www.open-std.org/jtc1/sc22/wg...docs/n1362.pdf.
    WOW... that's awful ... it gives whole new meaning to the old saw about bugs... "Undocumented Features"...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how many number of character many be in a variable name?
    By surrounded in forum C Programming
    Replies: 3
    Last Post: 02-26-2009, 05:41 PM
  2. Variable number of args
    By cboard_member in forum C Programming
    Replies: 2
    Last Post: 09-02-2005, 06:18 AM
  3. How would i do a variable number in a array?
    By kzar in forum C Programming
    Replies: 6
    Last Post: 11-21-2004, 06:02 AM
  4. Variable number of arguments
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-10-2004, 08:58 AM
  5. random number between 0 and variable value
    By HybridM in forum C++ Programming
    Replies: 23
    Last Post: 02-06-2003, 10:14 AM