Test integer overflow, floating-point overflow, and floating point underflow - C Data Type

C examples for Data Type:Introduction

Description

Test integer overflow, floating-point overflow, and floating point underflow

Demo Code

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

int main(void)
{
  int int_overflow;
  int MAX_INTEGER = INT_MAX;
  float flt_overflow, flt_underflow;
  float MIN_FLOAT = FLT_MIN;
  float MAX_FLOAT = FLT_MAX;
  
  // create over/underflow
  int_overflow = INT_MAX + 1;
  flt_overflow = FLT_MAX * 2.;//from ww w.j a  v  a 2  s .c  om
  flt_underflow = FLT_MIN / 2.;
  
  // print results
  printf("Max integer: %d \tMax integer + 1: %d\n", INT_MAX, int_overflow);
  printf("Max float: %f \tMax float * 2: %f\n", FLT_MAX, flt_overflow);
  printf("Min float: %f \tMin float / 2: %f\n", FLT_MIN, flt_underflow);

  return 0;
}

Result


Related Tutorials