Open In App

Octal literals in C

When we initialize a value by putting ‘0’ before a number, the number is treated as octal. For instance ’10’ is read as 10 but ‘010’ is read as 8. Octal numbers are the numbers with base 8 and octal literals are used to represent the octal integer values

Example



Input : 0101
Output : 65

Input : 01010
Output : 520

Examples of Octal Literals

Example 1




// C Program to illustrate octal literals in C
#include <stdio.h>
 
int main()
{
    // initializing an integer with an octal literal i.e.
    // starts with zero
    int x = 0101;
    printf("x = %d", x);
 
    return 0;
}

Output
x = 65

Example 2




// C Program to illustrate the octal literals
int main()
{
    int x = 020; // int with 20 octal value
    printf("x = %d", x);
 
    return 0;
}

Output

x = 16

Example 3

As the octal number system only have 8 digits, we cannot use 8 and 9 numbers as a digit in octal literal.




// C Program to illustrate the octal literals
#include <stdio.h>
 
int main()
{
    int x = 080;
    printf("x = %d", x);
    return 0;
}

Output

Compiler Error : 8 is not a valid digit in octal number.

Article Tags :