Skip to content

Instantly share code, notes, and snippets.

@danielwagn3r
Created December 2, 2017 20:00
Show Gist options
  • Save danielwagn3r/d52ad965f2994b8a031f662cfdef7f2f to your computer and use it in GitHub Desktop.
Save danielwagn3r/d52ad965f2994b8a031f662cfdef7f2f to your computer and use it in GitHub Desktop.
Overflow Check in C
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
}
if ((b > 0 && a <= INT_MAX / b && a >= INT_MIN / b) ||
(b == 0) ||
(b == -1 && a >= -INT_MAX) ||
(b < -1 && a >= INT_MAX / b && a <= INT_MIN / b))
{
result = a * b;
}
else
{
/* calculation would overflow */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment