2
\$\begingroup\$

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;
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$
  • Signedness of unqualified char is implementation defined. It may well be possible that char is in fact unsigned. Change char to signed 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 to char) always make me uncomfortable. A better technique to get a value with only MSB set is (TYPE is either char,int,long,whatever)

    unsigned TYPE value = ~(((unsigned TYPE) -1) >> 1)
    
\$\endgroup\$
5
  • \$\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
    Oct 6, 2016 at 20:14
  • \$\begingroup\$ @MuhamedCicak, you're wrong. Wikipedia hosted small reference page for your question. \$\endgroup\$ 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
    Oct 6, 2016 at 20:48
  • \$\begingroup\$ Than how ASCII Code works ?? I mean its not 255 but 127 ?? \$\endgroup\$
    – Silidrone
    Oct 6, 2016 at 20:53
  • \$\begingroup\$ @MuhamedCicak Check this: stackoverflow.com/questions/2098149/… \$\endgroup\$
    – vnp
    Oct 6, 2016 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.