Skip to content
Related Articles

Related Articles

Octal literals in C
  • Last Updated : 19 Aug, 2018

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.

Examples:

Input : 0101
Output : 65

Input : 01010
Output : 520




#include<iostream>
using namespace std;
int main()
{  
    int x = 0101; 
    cout << x; 
    return 0;
}
Output:
65




#include<iostream>
using namespace std;
int main()
{  
    int x = 020; 
    cout << x; 
    return 0;
}
Output:
16




#include<iostream>
using namespace std;
int main()
{  
    int x = 090; 
    cout << x; 
    return 0;
}

Output :

Compiler Error : 9 is not a valid digit in octal number.
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :