4.9 printf() 함수가 인자들을 해석하는 과정

공기훈·2021년 8월 5일
0

홍정모의 따배씨

목록 보기
12/49

기본적으로 float는 4byte, double은 8byte, int는 4byte이다.
그런데 float는 저장될 때 double로 변환되어 저장된다.

#include <stdio.h>
#include <limits.h>

int main()
{
	float n1 = 3.14;
	double n2 = 1.234;
	int n3 = 1024;

	printf("%f %f %d\n\n", n1, n2, n3);
	
	// Note the warnings in output window
	printf("%d %d %d\n\n", n1, n2, n3);	// 4, 4, 4 (N, N, N)
	printf("%lld %lld %d\n\n", n1, n2, n3); // 8, 8, 4 (N, N, Y)
	printf("%f %d %d\n\n", n1, n2, n3);	// 8, 4, 4 (Y, N, N)
	printf("%f %lld %d\n\n", n1, n2, n3);	// 8, 8, 4 (Y, N, Y)

	return 0;
} 

Warnings

  1. float와 double을 읽으려면 8byte가 필요한데, 4byte로 읽으려 했으므로 오류가 발생한다.
  2. type이 다르다. float를 정수형으로 읽으려 했으므로 오류가 발생한다.
  3. type과 크기 오류 발생
  4. 두 번째 type 오류
profile
be a coding master

0개의 댓글