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;
}
1 Answer 1
Signedness of unqualified
char
is implementation defined. It may well be possible thatchar
is in fact unsigned. Changechar
tosigned char
.A char is not guaranteed to have 8 bits (it is guaranteed to have at least 8 bits). Use
CHAR_BIT
instead.Narrowing types (e.g. assigning
long
tochar
) always make me uncomfortable. A better technique to get a value with only MSB set is (TYPE
is eitherchar,int,long,whatever
)unsigned TYPE value = ~(((unsigned TYPE) -1) >> 1)
-
\$\begingroup\$ I just don't understand why is char not guaranteed to be 8 bits.Because as i know it must be 8 bits.Please correct me if i am wrong. \$\endgroup\$Silidrone– Silidrone2016年10月06日 20:14:23 +00:00Commented Oct 6, 2016 at 20:14
-
\$\begingroup\$ @MuhamedCicak, you're wrong. Wikipedia hosted small reference page for your question. \$\endgroup\$Incomputable– Incomputable2016年10月06日 20:24:28 +00:00Commented Oct 6, 2016 at 20:24
-
\$\begingroup\$ i have worked on computers where char is 7 bits, where null is 0xffffffff. C deals with all sorts of types of machine \$\endgroup\$pm100– pm1002016年10月06日 20:48:51 +00:00Commented Oct 6, 2016 at 20:48
-
\$\begingroup\$ Than how ASCII Code works ?? I mean its not 255 but 127 ?? \$\endgroup\$Silidrone– Silidrone2016年10月06日 20:53:28 +00:00Commented Oct 6, 2016 at 20:53
-
\$\begingroup\$ @MuhamedCicak Check this: stackoverflow.com/questions/2098149/… \$\endgroup\$vnp– vnp2016年10月06日 21:09:29 +00:00Commented Oct 6, 2016 at 21:09