I wrote code to determinate ranges of char,short int and long variables,both signed and unsigned.Please help me to improve my code :) Here is the code:
#include <stdio.h>
//function declerations...
long power(int base, int power);
int main(void) {
//From 2^(N-1) to 2^(N-1)-1 (Two's complement)
int intmin = power(2, sizeof(int) * 8 - 1);
int intmax = power(2, sizeof(int) * 8 - 1) - 1;
unsigned unsignedintmax = power(2, sizeof(int) * 8) - 1;
char minchar = -(power(2, sizeof(char) * 8 - 1));
char maxchar = power(2, sizeof(char) * 8 - 1) - 1;
unsigned char unsignedcharmax = power(2, sizeof(char) * 8) - 1;
short shortmin = -(power(2, sizeof(short) * 8 - 1));
short shortmax = power(2, sizeof(short) * 8 - 1) - 1;
unsigned short unsignedshortmax = power(2, sizeof(short) * 8) - 1;
long minlong = power(2, sizeof(long) * 8 - 1);
long maxlong = power(2, sizeof(long) * 8 - 1) - 1;
unsigned long unsignedlongmax = power(2, sizeof(long) * 8) - 1;
minlong*=-1;
printf("\nSigned char can be minimum: %d and maximum: %d\n", minchar, maxchar);
printf("\nUnsigned char can be minimum: %d and maximum: %u\n", 0, unsignedcharmax);
printf("\nSigned short can be minimum: %d and maximum: %d\n", shortmin, shortmax);
printf("\nUnsigned short can be minimum: %d and maximum: %u\n", 0, unsignedshortmax);
printf("\nSigned int can be minimum: %d and maximum: %d\n", intmin, intmax);
printf("\nUnsigned int can be minimum: %d and maximum: %u\n", 0, unsignedintmax);
printf("\nSigned long can be minimum: %ld and maximum: %ld\n", minlong, maxlong);
printf("\nUnsigned long can be minimum: %d and maximum: %lu\n\n", 0, unsignedlongmax);
return 0;
}
long power(int base, int power) {
long pf = 1;
for (int i = 0; i < power; i++) {
pf *= base;
}
return pf;
}