Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What does it mean when a numeric constant in C/C++ is prefixed with a 0?
When a numeric constant in C is prefixed with a 0, it indicates that the number is an octal number (base 8). Octal literals must start with 0 to distinguish them from decimal numbers. For example, the octal number 25 is written as 025 in C.
Syntax
0octal_digits
Where octal_digits are valid octal digits (0-7).
Example: Octal Number Conversion
The following program demonstrates how octal literals are converted to decimal values −
#include <stdio.h>
int main() {
int a = 025; /* Octal 25 = 2*8^1 + 5*8^0 = 16 + 5 = 21 */
int b = 063; /* Octal 63 = 6*8^1 + 3*8^0 = 48 + 3 = 51 */
int c = 0100; /* Octal 100 = 1*8^2 + 0*8^1 + 0*8^0 = 64 */
printf("Decimal of 025(Octal) is %d\n", a);
printf("Decimal of 063(Octal) is %d\n", b);
printf("Decimal of 0100(Octal) is %d\n", c);
return 0;
}
Decimal of 025(Octal) is 21 Decimal of 063(Octal) is 51 Decimal of 0100(Octal) is 64
Key Points
- Octal numbers use only digits 0-7. Using 8 or 9 will cause a compilation error.
- The leading zero is mandatory for octal literals in C.
- Octal is base 8, so each digit represents a power of 8.
- Common use cases include file permissions in Unix systems (e.g.,
0755).
Conclusion
The 0 prefix in C indicates octal notation, where numbers are represented in base 8. This allows programmers to work with octal values directly in their code, which is particularly useful in system programming contexts.
Advertisements
