Skip to content

Instantly share code, notes, and snippets.

@ericonr
Last active May 9, 2024 17:09
Show Gist options
  • Save ericonr/7a5d7c9c651cb4203ca1f012d91607ba to your computer and use it in GitHub Desktop.
Save ericonr/7a5d7c9c651cb4203ca1f012d91607ba to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <limits.h>
int main()
{
printf("an int has %lu bytes ", sizeof(int));
printf("and it can fit up to %d inside it\n", INT_MAX);
printf("a long long has %lu bytes ", sizeof(long long));
printf("and it can fit up to %lld inside it\n\n", LLONG_MAX);
printf("20000000000 is a number that needs 64 bits\n");
int factor1 = 20000000, factor2 = 1000;
printf("however, these factors fit inside 32-bit ints: %d and %d\n", factor1, factor2);
printf("let's check if the multiplication works as ints: %d * %d = %d\n", factor1, factor2, 20000000 * 1000);
printf("at least the compiler warned me about it!\n");
printf("let's use long longs: %d * %d = %lld\n", factor1, factor2, 20000000LL * 1000);
printf("what if we make only the *result* long long? (long long)(%d * %d) = %lld\n", factor1, factor2, (long long)(20000000 * 1000));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment