ahrist123456   1년 전

double은 정확도가 더 높아서 9자리까지 표기 가능해서 double을 썼는데 그래도 틀렸어요

lcr7324   1년 전

예제를 통과하지 못합니다.

자세한 해설:

printf(3) man page

format string, 즉 서식 지정자는 아래 양식으로 작성하게 되어 있습니다.

%[argument$][flags][width][.precision][length modifier]conversion

%lf 서식 지정자는 Length modifier로 l을 사용하며 Conversion specifier로 f를 사용하는 것으로 해석하게 됩니다.

Length modifier 항목의 l 부분을 살펴보면,

... (중략) ... On a following a, A, e, E, f, F, g, or G conversion, this length modifier is ignored.

라고 쓰여 있습니다. 즉, 뒤에 f가 따라오는 경우 l은 의미를 가지지 않습니다.

Conversion specifier의 f, F 부분을 살펴보면,

The double argument is rounded and and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; ... (후략) ...

precision이 명시되지 않은 경우 소수점 아래 6자리까지 표기함을 설명하고 있습니다.

즉, precision을 명시하지 않으면 변수의 자료형을 무엇을 사용하든 틀리게 됩니다.

참고로, printf함수의 경우 %f와 %lf 모두 double형을 인자로 받습니다. 그러나 인자로 float형을 주든 double형을 주든 상관이 없는데, 이는 내부적으로 promotion이 일어나기 때문입니다. 이에 대한 자세한 설명은 StackOverflow 글로 갈음합니다.

댓글을 작성하려면 로그인해야 합니다.