How to calculate the largest positive integer in a 64-bit system in C

2 Answers

0 votes
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

/*
 * Returns the largest positive value that can be stored
 * in a signed 64-bit integer.
 */
int64_t get_max_int64(void) {
    return INT64_MAX;
}

int main(void)
{
    int64_t max_value = get_max_int64();

    printf("Maximum int64_t value: %" PRId64 "\n", max_value);

    return 0;
}


/*
run:

Maximum int64_t value: 9223372036854775807

*/

 



answered Oct 25 by avibootz
0 votes
#include <stdio.h>
#include <stdint.h>

/*
 * Computes the maximum signed 64-bit integer value.
 */
int64_t calculate_max_int64(void) {
    return (int64_t)((1ULL << 63) - 1);
}

int main(void)
{
    printf("%lld\n", (long long)calculate_max_int64());

    return 0;
}


/*
run:

9223372036854775807

*/

 



answered Oct 25 by avibootz
edited Oct 25 by avibootz
...