Home > Backend Development > C++ > How Are Zero-Prefixed Numbers Interpreted in C ?

How Are Zero-Prefixed Numbers Interpreted in C ?

Susan Sarandon
Release: 2024-11-26 12:07:10
Original
1039 people have browsed it

How Are Zero-Prefixed Numbers Interpreted in C  ?

Zero-Prefixed Numbers in C

When dealing with integer literals in C , certain behaviors arise when the number starts with zero. These behaviors revolve around the base of the interpreted number.

Octal Interpretation

When an integer literal begins with zero, it is typically interpreted as an octal number, also known as base-8. This means the digits used are from 0 to 7. For instance, 07 is equivalent to the decimal value 7.

Special Case: 00x

However, when an integer literal is 00x, it is treated as a hexadecimal number, which uses base-16. It starts with the prefix 0x and employs digits 0-9 and A-F to represent decimal values 0-15.

Error with 08

An error occurs when an integer literal starts with 08 because this combination is undefined in C . There is no such thing as an octal digit 8.

Leading Zero Sequence

If an integer literal starts with multiple zeros, all leading zeros except the first one are ignored. For example, 00016 is interpreted as the octal number 16, which equates to the decimal value 14. This is because 016 represents 14 in octal.

Therefore, in your given examples:

  • 07 is 7 in decimal.
  • 16 is 16 in decimal.
  • 00016 is 14 in decimal.
  • 05016 is 2574 in decimal (not 14, as it is interpreted as an octal value).
  • 08 leads to a compile error because it is not a valid octal digit.

The above is the detailed content of How Are Zero-Prefixed Numbers Interpreted in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template