Advertisement
informaticage

Tutorial C - casting and overflow

Feb 7th, 2015
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. int main()
  6. {
  7.     int a;
  8.     long long int tot;
  9.     float num;
  10.     int div0, div1;
  11.  
  12.     printf("Short limit %d", SHRT_MAX);
  13.     printf("\nInteger limit %d", INT_MAX);
  14.     printf("\nLong limit %ld", LONG_MAX);
  15.     printf("\nLong long limit %lld\n\n", LONG_LONG_MAX);
  16.  
  17.     a = INT_MAX * 10;
  18.     printf("\ta overflow = %d", a);
  19.  
  20.     a = INT_MAX;
  21.     printf("\n\ta = %d", a);
  22.  
  23.     tot = a * 10;
  24.     printf("\n\ttot no casting = %lld", tot);
  25.  
  26.     tot = (long long)a * 10;
  27.     printf("\n\ttot casting = %lld", tot);
  28.  
  29.     div0 = 35;
  30.     div1 = 3;
  31.     num = div0 / div1; // 11,66666666666667
  32.     printf("\n\tnum no casting %f", num);
  33.  
  34.     div0 = 35;
  35.     div1 = 3;
  36.     num = (float)div0 / div1; // 11,66666666666667
  37.     printf("\n\tnum casting %f\n", num);
  38.    
  39.     getchar();
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement