Skip to content

Instantly share code, notes, and snippets.

@HarshKapadia2
Forked from ericonr/overflow.c
Created May 9, 2024 17:09
Show Gist options
  • Save HarshKapadia2/eaf501d241b2798d5672ed102b780b82 to your computer and use it in GitHub Desktop.
Save HarshKapadia2/eaf501d241b2798d5672ed102b780b82 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