Just began programming and having trouble with integers

This is a discussion on Just began programming and having trouble with integers within the C Programming forums, part of the General Programming Boards category; Hi, i'm trying to use a long long integer to store a number, but when i run the compiled code ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    Question Just began programming and having trouble with integers

    Hi, i'm trying to use a long long integer to store a number, but when i run the compiled code i get a negative number. any help?
    Code:
    #include <stdio.h>
    void main() {
        long long num1;
        num1=600851475143;
        printf("\n%d\n", num1);
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    Use unsigned long long. Also, use the correct format flag for printf.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    can you give me a list of C format flags? i never learned them

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    In that case add the "ll" suffix to that number, as it tells the compiler to store it using 64 bits.
    Code:
    num1=600851475143ll;

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    adding the "ll" still returns a minus. using an unsigned long long doesn't work either. Any code examples? Thanks

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    I'd be %llu, and using unsigned long long together.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Thank you, so I was just using the wrong format flag. I am also using a signed variable, thanks. Problem solved. Now to figure out this Euler problem...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sfkc1588 View Post
    can you give me a list of C format flags? i never learned them
    Well that's easy... Click Here!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integers trouble
    By robots in forum C Programming
    Replies: 3
    Last Post: 09-27-2009, 12:57 AM
  2. Programming trouble.
    By francusmag in forum C Programming
    Replies: 10
    Last Post: 11-29-2007, 05:36 AM
  3. factor of integers trouble
    By surfingbum18 in forum C Programming
    Replies: 4
    Last Post: 09-27-2007, 01:35 AM
  4. Turbo C programming - integers in C
    By unregistered in forum C Programming
    Replies: 4
    Last Post: 02-03-2002, 12:44 AM
  5. having trouble programming
    By amma21 in forum C Programming
    Replies: 6
    Last Post: 11-11-2001, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21