Joseph Haugh
University of New Mexico
/* 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;
}
$ 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
/* 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;
}
$ 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
/* 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;
}
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
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
/* 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