Lecture 04 Integers

Joseph Haugh

University of New Mexico

Integer Values - Different Bases

/* File: 04_int_diffBases.c */
#include <stdio.h>

int main() {
    int x = 12;
    int someN = 45;
    int foo_1 = 27455;

    printf("x = dec: %d oct: %o hex: %X\n", 
            x, x, x);

    printf("someN = dec: %d oct: %o hex: %X\n",
            someN, someN, someN);

    printf("foo_1 = dec: %d oct: %o hex: %X\n",
            foo_1, foo_1, foo_1);

    return 0;
}
  • A value can be represented many different ways.
  • Recall that:
    • 40610 = CCCCVI
    • 40610 = 1100101102
$ gcc 04_int_diffBases.c
$ ./a.out
x = dec: 12 oct: 14 hex: C
someN = dec: 45 oct: 55 hex: 2D
foo_1 = dec: 27455 oct: 65477 hex: 6B3F

Integer Values - Different Bases

/* File: 04_int_diffBases.c */
#include <stdio.h>

int main() {
    int x = 12;
    int someN = 45;
    int foo_1 = 27455;

    printf("x = dec: %d oct: %o hex: %X\n", 
            x, x, x);

    printf("someN = dec: %d oct: %o hex: %X\n",
            someN, someN, someN);

    printf("foo_1 = dec: %d oct: %o hex: %X\n",
            foo_1, foo_1, foo_1);

    return 0;
}
  • printf has “conversion specifiers” that indicate how your want the value to be displayed:
    • %d – decimal (base 10)
    • %o – octal (base 8)
    • %x – hex (base 16)
    • %X – hex (base 16)
$ gcc 04_int_diffBases.c
$ ./a.out
x = dec: 12 oct: 14 hex: C
someN = dec: 45 oct: 55 hex: 2D
foo_1 = dec: 27455 oct: 65477 hex: 6B3F

Controlling Format of Output

/* File: 04_int_format.c */
#include <stdio.h>

int main() {
    int x = 12;
    int someN = 45;
    int foo_1 = 27455;
    printf("x is decimal %d, octal %o, hex %X\n", x, x, x);
    printf("x = %d, which is a dozen\n", x);
    printf("x = %6d, which is a dozen\n", x);   // field width
    printf("x = %10d, which is a dozen\n", x);
    printf("x = %010d, which is a dozen\n\n", x); // leading 0's
    printf("x = %-6d, which is a dozen\n", x);  // left justified
    printf("x = %+d, which is a dozen\n", x);   // show + sign
    printf("x = %+6d, which is a dozen\n", x);  // left just, show +
    printf("x = %-+6d, which is a dozen\n", x); // left just, show +
    return 0;
}
  • How exactly should the value of x (i.e., 12) be displayed in the output?

Controlling Format of Output

printf("x is decimal %d, octal %o, hex %X\n", x, x, x);
printf("x = %d, which is a dozen\n", x);
printf("x = %6d, which is a dozen\n", x);   // field width
printf("x = %10d, which is a dozen\n", x);
printf("x = %010d, which is a dozen\n\n", x); // leading 0's
x is decimal 12, octal 14, hex C
x = 12, which is a dozen
x =     12, which is a dozen
x =         12, which is a dozen
x = 0000000012, which is a dozen

Controlling Output Format

printf("x = %-6d, which is a dozen\n", x);  // left justified
printf("x = %+d, which is a dozen\n", x);   // show + sign
printf("x = %+6d, which is a dozen\n", x);  // left just, show +
printf("x = %-+6d, which is a dozen\n", x); // left just, show +
x = 12    , which is a dozen
x = +12, which is a dozen
x =    +12, which is a dozen
x = +12   , which is a dozen

Integer Values - Different Bases

/* File: 04_int_diffBases2.c */
#include <stdio.h>

int main() {
    // the decimal number 27455 = octal 65477 = hex 6B3F

    int x = 27455;

    int y = 065477;    // leading 0 indicates octal

    int z = 0X6B3F;    // leading 0X or 0x indicates hex

    printf("x = %d, y = %d, z = %d\n", x, y, z);
    return 0;
}
x = 27455, y = 27455, z = 27455
  • leading 0
    • octal notation
  • leading 0X or 0x
    • hex notation