0은 C에서 8 진수 또는 10 진수입니까?
이 질문에 이미 답변이 있습니다.
나는 이것을 읽었다 . C ++에서는 8 진수이고 Java에서는 10 진수입니다. 하지만 C에 대한 설명이 없습니까?
0이 8 진수 또는 10 진수이면 차이가 있습니까? 이것은 내 면접관이 묻는 질문입니다. 나는 아니오라고 말했고 그것이 8 진이든 10 진이든 상관없이 항상 0이라고 설명했다.
그런 다음 왜 C ++에서는 8 진수로, Java에서는 10 진수로 간주되는지 물었습니다. 나는 그것이 표준이라고 말했다. C로 무엇인지 알려주세요. 어떤 차이가 있습니까? 표준이 다른 이유는 무엇입니까?
차이가 거의 없지만 공식적으로 정수 상수 0는 C에서 8 진수입니다. C99 및 C11 표준에서 6.4.4.1 정수 상수
integer-constant :
10 진수 상수 정수 접미사 opt
8 진수 상수 정수 접미사 opt
16 진수 상수 정수 접미사 opt소수점 상수 :
제로 자리
십진수 상수의 자리8 진수 상수 :
0
8 진수 상수 8 진수16 진수 상수 :
...
...
8 진법.
C11 §6.4.4.1 정수 상수
octal-constant: 0 octal-constant octal-digit
그리고 이것은 C89 §3.1.3.2 이후로 사실 입니다.
그런 다음 그는 C ++에서는 8 진수로, Java에서는 10 진수로 간주되는 이유를 물었습니다.
완전성을 위해 Java 사양도 언급 할 가치가 있습니다. 에서 3.10.1 Java 언어 사양 :
DecimalNumeral: 0 NonZeroDigit Digitsopt NonZeroDigit Underscores Digits10 진수는 정수 0을 나타내는 단일 ASCII 숫자 0이거나, 선택적으로 1에서 9까지의 ASCII 숫자와 밑줄이있는 0에서 9 사이의 하나 이상의 ASCII 숫자로 구성되며 양의 정수를 나타냅니다.
OctalNumeral: 0 OctalDigits 0 Underscores OctalDigits8 진수는 ASCII 숫자 0과 밑줄이있는 ASCII 숫자 0-7 중 하나 이상으로 구성되며 양수, 0 또는 음의 정수를 나타낼 수 있습니다.
보시다시피 베어 0는 decimal 로 간주됩니다 . 앞에 오는 (비어 있지 않은) 숫자 시퀀스는 8 진수0 로 간주됩니다 .
그 문법에서 흥미롭게도 :
0십진수- 그러나
008 진수
8 진수입니다. 6.4.4.1 Integer constantsN1570 초안 섹션 참조 :
integer-constant:
decimal-constant integer-suffixopt
octal-constant integer-suffixopt
hexadecimal-constant integer-suffixopt
decimal-constant:
nonzero-digit
decimal-constant digit
octal-constant:
0
octal-constant octal-digit
hexadecimal-constant:
hexadecimal-prefix hexadecimal-digit
hexadecimal-constant hexadecimal-digit
hexadecimal-prefix: one of
0x 0X
nonzero-digit: one of
1 2 3 4 5 6 7 8 9
octal-digit: one of
0 1 2 3 4 5 6 7
hexadecimal-digit: one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
integer-suffix:
unsigned-suffix long-suffixopt
unsigned-suffix long-long-suffix
long-suffix unsigned-suffixopt
long-long-suffix unsigned-suffixopt
unsigned-suffix: one of
u U
long-suffix: one of
l L
long-long-suffix: one of
ll LL
또한:
- A decimal constant begins with a nonzero digit and consists of a sequence of decimal digits. An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only. A hexadecimal constant consists of the prefix 0x or 0X followed by a sequence of the decimal digits and the letters a (or A) through f (or F) with values 10 through 15 respectively.
From the C Standard (6.4.4.1 Integer constants)
octal-constant:
0
octal-constant octal-digit
In fact there is no any difference for zero because zero is a common digit for octal, decimal and hexadecimal numbers. It has meaning only when a number has other digits apart from the single (leading) zero.
Take into account that there are no such integral types as decimal, octal or hexadecimal.
I think it depends on compiler implementation. We have to see the source code to determine whether it flags a "0" constant as octal or not. I can define the non-octal reason in this way: Octals has "0" prefix. But there is no prefix. If the constant is 00, then it IS octal - "octal Zero" :)
참고URL : https://stackoverflow.com/questions/26625311/is-0-an-octal-or-a-decimal-in-c
'program story' 카테고리의 다른 글
| FrameLayout은 무엇을합니까? (0) | 2020.11.29 |
|---|---|
| JavaScript에서 문자열 이스케이프 (0) | 2020.11.29 |
| 크롤러를 작성하는 방법? (0) | 2020.11.29 |
| int를 바이너리로 변환하는 쉽고 빠른 방법? (0) | 2020.11.29 |
| 신속한 데이터를 16 진수 문자열로 변환하는 방법 (0) | 2020.11.29 |