There are some less known facts in C related to octal numbers. Let us look at the below example code first.
Surprisingly the output is not 12. This happens because an integer digit preceded by 0 is interpreted as an octal number. An octal number for decimal value 12 is 10. But what if x equals 092 well then the compiler will show an error because 92 is not a valid octal number.
Octal escape sequence
In C octal escape sequence is represented by \ followed by three octal digits. Note that one or two octal digits are also allowed. An octal sequence ends either ends after three octal digits following \ or when a digit after \ is not an octal digit.
If we look at the output then after 31 a newline character is printed and then 67 is printed. This is because \012 is interpreted as \n or new line character.Actually, \012 represents octal escape sequence for \n or newline. Octal value of 12 in decimal is 10 and in ASCII it represents newline.
In str \123 is interpreted as octal escape sequence and value of octal 123 is 83 and character corresponding to it in my machine is ‘S’ as my machine uses ASCII. It can be different if character set used is not ASCII.
In str2 \77 is interpreted as an octal escape sequence and not \779. So, the value of 77 is 63 in decimal and in ASCII it is ‘?’.
This article is contributed by Ashish Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Octal literals in C
- Program to Convert Hexadecimal to Octal
- Add two numbers using ++ and/or --
- Converting Strings to Numbers in C/C++
- Why variable name does not start with numbers in C ?
- To find sum of two numbers without using any operator
- How will you print numbers from 1 to 100 without using loop?
- Modulus on Negative Numbers
- How will you print numbers from 1 to 100 without using loop? | Set-2
- C Program to print numbers from 1 to N without using semicolon?
- Program to print a pattern of numbers
- Modulus of two float or double numbers
- LEX program to add line numbers to a given file
- How to Count Variable Numbers of Arguments in C?
- Print numbers in sequence using thread synchronization