In the following program, we will see how to initialize the variables with decimal, octal & Hex values.
#include <stdio.h>
int main(void) {
// Following is way to initialize the variables with decimal, octal & hexa decimal.
int decimal_variable = 10;
int octal_variable = 012;
int hexa_variable = 0xa;
// print decimal_variable as hexa
printf("Decimal value %d in Hexa is %X \n", decimal_variable, decimal_variable);
// print decimal_variable as octal
printf("Decimal value %d in Octal is %o \n",decimal_variable, decimal_variable);
// Print octal_variable as decimal
printf("Octal value %o in Decimal %d \n", octal_variable, octal_variable);
// Print octal_variable as hexa
printf("Octal value %o in Hexa %X \n", octal_variable, octal_variable);
// Print Hexa as decimal
printf("Hexa value %X in Decimal %d \n", hexa_variable, hexa_variable);
// Print Hexa as decimal
printf("Hexa value %X in Octal %o \n", hexa_variable, hexa_variable);
return 0;
}
Output:
Decimal value 10 in Hexa is A
Decimal value 10 in Octal is 12
Octal value 12 in Decimal 10
Octal value 12 in Hexa A
Hexa value A in Decimal 10
Hexa value A in Octal 12
Explanation
We must use 0, 0x notations as shown in the above example while initializing the octal and Hex values.
To print the values in decimal, octal & hexadecimal formats we have to use the %d %o, %X format specifiers in print.
Leave a Comment