Computer Systems Fundamentals



Print size and min and max values of integer types
#include <stdio.h>
#include <limits.h>

int main(void) {

    char c;
    printf("char               %lu bytes min=%20d, max=%20d\n", sizeof c, CHAR_MIN, CHAR_MAX);
    signed char sc;
    printf("signed char        %lu bytes min=%20d, max=%20d\n", sizeof sc, SCHAR_MIN, SCHAR_MAX);
    unsigned char uc;
    printf("unsigned char      %lu bytes min=%20d, max=%20d\n", sizeof uc, 0, UCHAR_MAX);

    short s;
    printf("short              %lu bytes min=%20d, max=%20d\n", sizeof s, SHRT_MIN, SHRT_MAX);
    unsigned short us;
    printf("unsigned short     %lu bytes min=%20d, max=%20d\n", sizeof us, 0, USHRT_MAX);

    int i;
    printf("int                %lu bytes min=%20d, max=%20d\n", sizeof i, INT_MIN, INT_MAX);
    unsigned int ui;
    printf("unsigned int       %lu bytes min=%20d, max=%20d\n", sizeof ui, 0, UINT_MAX);

    long l;
    printf("long               %lu bytes min=%20ld, max=%20ld\n", sizeof l, LONG_MIN, LONG_MAX);
    unsigned long ul;
    printf("unsigned long      %lu bytes min=%20d, max=%20lu\n", sizeof ul, 0, ULONG_MAX);

    long long ll;
    printf("long long          %lu bytes min=%20lld, max=%20lld\n", sizeof ll, LLONG_MIN, LLONG_MAX);
    unsigned long long ull;
    printf("unsigned long long %lu bytes min=%20d, max=%20llu\n", sizeof ull, 0, ULLONG_MAX);

    return 0;
}
#include <stdint.h>

int main(void) {

                 // range of values for type
                 //             minimum               maximum
    int8_t   i1; //                 -128                  127
    uint8_t  i2; //                    0                  255
    int16_t  i3; //               -32768                32767
    uint16_t i4; //                    0                65535
    int32_t  i5; //          -2147483648           2147483647
    uint32_t i6; //                    0           4294967295
    int64_t  i7; // -9223372036854775808  9223372036854775807
    uint64_t i8; //                    0 18446744073709551615

    return 0;
}
#include <stdio.h>

int main(void) {
    // common C bug
    //
    // char may be signed (e.g. x86) or unsigned (powerpc)
    //
    // if char is signed (-128..127)
    // loop will incorrect exit for a byte containing 0xFF
    //
    // if char is unsigned (0..255)
    // loop will never exit
    //
    // fix bug by making c int
    //

    char c;
    while ((c = getchar()) != EOF) {
        putchar(c);
    }
    return 0;
}


Print binary representation of ints
#include <stdio.h>

void print_bits(int value);
int get_nth_bit(int value, int n);

int main(void) {
    int a = 0;
    printf("Enter an int: ");
    scanf("%d", &a);
    print_bits(a);
    printf("\n");
    return 0;
}

// print the binary representation of a value
void print_bits(int value) {
    // sizeof returns size in bytes and 1 byte == 8 bits
    int how_many_bits = 8 * (sizeof value);
    for (int i = how_many_bits - 1; i >= 0; i--) {
        int bit = get_nth_bit(value, i);
        printf("%d", bit);
    }
}

// extract the nth bit from a value
int get_nth_bit(int value, int n) {
    return (value >> n) & 1;
}