Home »
C programs »
C scanf() programs
Input octal value using scanf() in C
Here, we are going to learn how to input an octal value in C programming language? To read octal value, we use "%o" format specifier in scanf() in C language.
By IncludeHelp Last updated : March 10, 2024
Here, we have to declare an unsigned int variable and input a value which should be entered in octal format.
Input octal value using scanf()
To input and print a value in octal format - we use "%o" format specifier.
Program
#include <stdio.h>
int main(void)
{
unsigned int value;
printf("Input octal value: ");
scanf("%o", &value);
printf("value in octal format: %o\n", value);
printf("value in decimal format: %d\n", value);
//testing with invalid value
printf("Input octal value: ");
scanf("%o", &value);
printf("value in octal format: %o\n", value);
printf("value in decimal format: %d\n", value);
return 0;
}
Output
Input octal value: 127
value in octal format: 127
value in decimal format: 87
Input octal value: 1278
value in octal format: 127
value in decimal format: 87
Explanation
See the second input and its result, the input value is 1278 and the accepted value is 127 because 8 is not a valid octal digit. Octal numbers have only 8 digits which are 0, 1, 2, 3, 4, 5, 6 and 7.
C scanf() Programs »