I need to write a program in C that prints out all integral types’ size and rang
ID: 3883317 • Letter: I
Question
I need to write a program in C that prints out all integral types’ size and range. And then I have to overflow each using addition of the same type. When I run the program using gcc, I get:
Storage size for char : 1
Floating Point exception (core dumped).
And the the program exits. What am I doing wrong?
#include <limits.h>
#include <stdio.h>
#include <string.h>
#define CHAR_MAX 127/255
#define CHAR_MIN -128/0
#define INT_MAX 32767
#define INIT_MIN -32767
#define LONG_MAX 2147483647
#define LONG_MIN -2147483647
#define SCHAR_MAX 127
#define SCHAR_MIN -127
#define SHRT_MAX 32767
#define SHRT_MIN -32767
#define UCHAR_MAX 255
#define UINIT_MAX 65535
#define ULONG_MAX 4294967295
#define USHRT_MAX 65536
int main() {
//Question 1
printf("storage size for char: %d ", sizeof(char));
printf("Upper bound for char is %d and lower bound is %d ", CHAR_MAX, CHAR_MIN);
printf("storage size for int: %d ", sizeof(int));
printf("Upper bound for int is %d and lower bound is %d ", INT_MAX, INT_MIN);
printf("storage size for long: %d ", sizeof(long));
printf("Upper bound for long is %d and lower bound is %ld ", LONG_MAX, LONG_MIN);
printf("storage size for signed char: %d ", sizeof(signed char));
printf("Upper bound for signed char is %d and lower bound is %d ", SCHAR_MAX, SCHAR_MIN);
printf("storage size for short: %d ", sizeof(short));
printf("Upper bound for short is %d and lower bound is %d ", SHRT_MAX, SHRT_MIN);
printf("storage size for unsigned char: %d ", sizeof(unsigned char));
printf("Upper bound for unsigned char is %d ", UCHAR_MAX);
printf("storage size for unsigned int: %d ", sizeof(unsigned int));
printf("Upper bound for unsigned int is %d ", UINT_MAX);
printf("storage size for unsigned long: %d ", sizeof(unsigned long));
printf("Upper bound for unsigned long is %d ", ULONG_MAX);
printf("storage size for unsigned short: %d ", sizeof(unsigned short));
printf("Upper bound for unsigned short is %d ", USHRT_MAX);
//Question 2
int i1 = 500;
int i2 = 1000;
int sumi = i1 + i2;
printf("sum of int 1 and int 2 is = %d ", sumi);
char c1 = 'a';
char c2 = 'b';
char sumc = c1 + c2;
printf("sum of char 1 and char 2 is = %c ", sumc);
long l1 = 99999999;
long l2 = 9999999999;
long suml = l1 + l2;
printf("sum of long 1 and long 2 is = %ld ", suml);
short s1 = 1000;
short s2 = 9999;
short sums = s1 + s2;
printf("sum of short 1 and short 2 is = %h ", sums);
signed char sc1 = 'a';
signed char sc2 = 'b';
signed char sumsc = sc1 + sc2;
printf("sum of signed char 1 and signed char 2 is = %c ", sumsc);
unsigned int ui1 = 10000;
unsigned int ui2 = 99999;
unsigned int sumui = ui1 + ui2;
printf("sum of unsigned int 1 and unsigned int 2 is = %u ", sumui);
unsigned char uc1 = 'a';
unsigned char uc2 = 'b';
unsigned char sumuc = uc1 + uc2;
printf("sum of unsigned char 1 and unsigned char 2 is = %c ", sumuc);
unsigned long ul1 = 1000000;
unsigned long ul2 = 9999999;
unsigned long sumul = ul1 + ul2;
printf("sum of unsigned long 1 and unsigned long 2 is = %lu ", sumul);
unsigned short us1 = 1000;
unsigned short us2 = 9999;
unsigned short sumus = us1 + us2;
printf("sum of unsigned short 1 and unsigned short 2 is = %hu ", sumus);
}
Explanation / Answer
In your case the problem is simple ,ie there is an run time error thats why it shows Floating Point exception (core dumped).
i mean in the #define part you write like this
#define CHAR_MAX 127/255
#define CHAR_MIN -128/0
Here CHAR_MAX means 127 divide by 255
and CHAR_MIN means -128 divide by 0 : and here is the problem there is a divide by zero error
i know you mean -128 or 0 but it is single integer so you can't decalre like this
you can use like this
unsigned char 0 to 255
signed char -128 to 127
your program will work fine after you comment or rewrite this lines
eg:
#include <limits.h>
#include <stdio.h>
#include <string.h>
#define CHAR_MAX 127/255
#define CHAR_MIN -128/0
#define INT_MAX 32767
#define INIT_MIN -32767
#define LONG_MAX 2147483647
#define LONG_MIN -2147483647
#define SCHAR_MAX 127
#define SCHAR_MIN -127
#define SHRT_MAX 32767
#define SHRT_MIN -32767
#define UCHAR_MAX 255
#define UINIT_MAX 65535
#define ULONG_MAX 4294967295
#define USHRT_MAX 65536
int main() {
//Question 1
printf("storage size for char: %d ", sizeof(char));
// printf("Upper bound for char is %d and lower bound is %d ", CHAR_MAX, CHAR_MIN);
printf("storage size for int: %d ", sizeof(int));
printf("Upper bound for int is %d and lower bound is %d ", INT_MAX, INT_MIN);
printf("storage size for long: %d ", sizeof(long));
printf("Upper bound for long is %d and lower bound is %ld ", LONG_MAX, LONG_MIN);
printf("storage size for signed char: %d ", sizeof(signed char));
printf("Upper bound for signed char is %d and lower bound is %d ", SCHAR_MAX, SCHAR_MIN);
printf("storage size for short: %d ", sizeof(short));
printf("Upper bound for short is %d and lower bound is %d ", SHRT_MAX, SHRT_MIN);
printf("storage size for unsigned char: %d ", sizeof(unsigned char));
printf("Upper bound for unsigned char is %d ", UCHAR_MAX);
printf("storage size for unsigned int: %d ", sizeof(unsigned int));
printf("Upper bound for unsigned int is %d ", UINT_MAX);
printf("storage size for unsigned long: %d ", sizeof(unsigned long));
printf("Upper bound for unsigned long is %d ", ULONG_MAX);
printf("storage size for unsigned short: %d ", sizeof(unsigned short));
printf("Upper bound for unsigned short is %d ", USHRT_MAX);
//Question 2
int i1 = 500;
int i2 = 1000;
int sumi = i1 + i2;
printf("sum of int 1 and int 2 is = %d ", sumi);
char c1 = 'a';
char c2 = 'b';
char sumc = c1 + c2;
printf("sum of char 1 and char 2 is = %c ", sumc);
long l1 = 99999999;
long l2 = 9999999999;
long suml = l1 + l2;
printf("sum of long 1 and long 2 is = %ld ", suml);
short s1 = 1000;
short s2 = 9999;
short sums = s1 + s2;
printf("sum of short 1 and short 2 is = %h ", sums);
signed char sc1 = 'a';
signed char sc2 = 'b';
signed char sumsc = sc1 + sc2;
printf("sum of signed char 1 and signed char 2 is = %c ", sumsc);
unsigned int ui1 = 10000;
unsigned int ui2 = 99999;
unsigned int sumui = ui1 + ui2;
printf("sum of unsigned int 1 and unsigned int 2 is = %u ", sumui);
unsigned char uc1 = 'a';
unsigned char uc2 = 'b';
unsigned char sumuc = uc1 + uc2;
printf("sum of unsigned char 1 and unsigned char 2 is = %c ", sumuc);
unsigned long ul1 = 1000000;
unsigned long ul2 = 9999999;
unsigned long sumul = ul1 + ul2;
printf("sum of unsigned long 1 and unsigned long 2 is = %lu ", sumul);
unsigned short us1 = 1000;
unsigned short us2 = 9999;
unsigned short sumus = us1 + us2;
printf("sum of unsigned short 1 and unsigned short 2 is = %hu ", sumus);
}