형식화 된 IO 함수에서 변환 지정자 %i와 %d의 차이점은 무엇입니까 ( *printf / *scanf)

StackOverflow https://stackoverflow.com/questions/1893490

문제

차이점은 무엇입니까? %d 그리고 %i 형식 지정자로 사용하는 경우 printf?

도움이 되었습니까?

해결책

그들은 출력에 사용될 때와 동일합니다. printf.

그러나 입력 지정자로 사용하면 다음과 같습니다. scanf, 어디 %d 정수를 서명 된 소수점 번호로 스캔하지만 %i 기본값은 10 진수로, 또한 16 진수를 허용합니다 (앞에있는 경우 0x) 및 10 월 (앞에있는 경우 0).

그래서 033 27 세가 될 것입니다 %i 그러나 33 %d.

다른 팁

이것들은 동일합니다 printf 그러나 다릅니다 scanf. 을 위한 printf, 둘 다 %d 그리고 %i 서명 된 10 진수 정수를 지정하십시오. 을 위한 scanf, %d 그리고 %i 또한 서명 된 정수를 의미하지만 %i 이전의 경우 입력을 16 진수로 확인하십시오. 0x 그리고 앞에있는 경우 8 월 0 그렇지 않으면 입력을 소수점으로 해석합니다.

사이에는 차이가 없습니다 %i 그리고 %d 형식 지정자 printf. 우리는 이것을 볼 수 있습니다 초안 C99 표준 부분 7.19.6.1 fprintf 함수 여기에서도 다룹니다 printf 형식 지정자와 관련하여 단락에 표시됩니다. 8:

변환 지정자와 그 의미는 다음과 같습니다.

다음은 총알을 포함합니다.

d,i     The int argument is converted to signed decimal in the style
        [−]dddd. The precision specifies the minimum number of digits to
        appear; if the value being converted can be represented in fewer
        digits, it is expanded with leading zeros. The default precision is
        1. The result of converting a zero value with a precision of zero is
        no characters.

반면에 scanf 차이가 있습니다. %d 베이스 10을 가정하십시오 %i 자동으로베이스를 감지합니다. 우리는 섹션에 가면 이것을 볼 수 있습니다 7.19.6.2 fscanf 함수 커버 scanf 형식 지정자와 관련하여 단락 12 그것은 말한다 :

변환 지정자와 그 의미는 다음과 같습니다.

다음을 포함합니다.

d     Matches an optionally signed decimal integer, whose format is the
      same as expected for the subject sequence of the strtol function with
      the value 10 for the base argument. The corresponding argument shall
      be a pointer to signed integer.

i     Matches an optionally signed integer, whose format is the same as
      expected for the subject sequence of the strtol function with the
      value 0 for the base argument. The corresponding argument shall be a
      pointer to signed integer.

아무것도 없습니다 printf - 두 사람은 동의어입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top