-7

When I assign i value greater than INT_MAX in this code -

#include <stdio.h>
#include <limits.h>

int main()
{

   int i;
   i=INT_MAX+3;

   printf("%d \n",INT_MAX);  //INT_MAX = 2147483647

   if(i>INT_MAX) 
     {     
         printf("OVERFLOW");
     }
  else 
    {
         printf("%d",i);    
    }   
     return 0;
}

So in this as i=INT_MAX+3; then condition is true and overflow should be printed but it prints some negative value. Is it undefined behaviour ?

ameyCU
  • 16,489
  • 2
  • 26
  • 41

1 Answers1

5
  1. Yes, signed integer overflow has undefined behavior in C.

  2. i > INT_MAX is never true for any int i. How can you have an int greater than the maximum possible int?

melpomene
  • 84,125
  • 8
  • 85
  • 148