Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 604425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:02:13+00:00 2026-05-13T17:02:13+00:00

Why do I get -1 when I print the following? unsigned long long int

  • 0

Why do I get -1 when I print the following?

unsigned long long int largestIntegerInC = 18446744073709551615LL;

printf ("largestIntegerInC = %d\n", largestIntegerInC);

I know I should use llu instead of d, but why do I get -1 instead of 18446744073709551615LL?

Is it because of overflow?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:02:13+00:00Added an answer on May 13, 2026 at 5:02 pm

    In C (99), LLONG_MAX, the maximum value of long long int type is guaranteed to be at least 9223372036854775807. The maximum value of an unsigned long long int is guaranteed to be at least 18446744073709551615, which is 264−1 (0xffffffffffffffff).

    So, initialization should be:

    unsigned long long int largestIntegerInC = 18446744073709551615ULL;
    

    (Note the ULL.) Since largestIntegerInC is of type unsigned long long int, you should print it with the right format specifier, which is "%llu":

    $ cat test.c
    #include <stdio.h>
    
    int main(void)
    {
        unsigned long long int largestIntegerInC = 18446744073709551615ULL;
        /* good */
        printf("%llu\n", largestIntegerInC);
        /* bad */
        printf("%d\n", largestIntegerInC);
        return 0;
    }
    $ gcc  -std=c99 -pedantic test.c
    test.c: In function ‘main’:
    test.c:9: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’
    

    The second printf() above is wrong, it can print anything. You are using "%d", which means printf() is expecting an int, but gets a unsigned long long int, which is (most likely) not the same size as int. The reason you are getting -1 as your output is due to (bad) luck, and the fact that on your machine, numbers are represented using two’s complement representation.


    To see how this can be bad, let’s run the following program:

    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    int main(int argc, char *argv[])
    {
        const char *fmt;
        unsigned long long int x = ULLONG_MAX;
        unsigned long long int y = 42;
        int i = -1;
        if (argc != 2) {
            fprintf(stderr, "Need format string\n");
            return EXIT_FAILURE;
        }
        fmt = argv[1];
        printf(fmt, x, y, i);
        putchar('\n');
        return 0;
    }
    

    On my Macbook, running the program with "%d %d %d" gives me -1 -1 42, and on a Linux machine, the same program with the same format gives me -1 42 -1. Oops.


    In fact, if you are trying to store the largest unsigned long long int number in your largestIntegerInC variable, you should include limits.h and use ULLONG_MAX. Or you should store assing -1 to your variable:

    #include <limits.h>
    #include <stdio.h>
    
    int main(void)
    {
        unsigned long long int largestIntegerInC = ULLONG_MAX;
        unsigned long long int next = -1;
        if (next == largestIntegerInC) puts("OK");
        return 0;
    }
    

    In the above program, both largestIntegerInC and next contain the largest possible value for unsigned long long int type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.