본문 바로가기

C++ 기초 1

[C++] sizeof 연산자

sizeof 연산자

   sizeof 연산자는 변수, 수식, 상수 및 데이터형이 메모리 중에서 차지하는 크기를 바이트(byte)수로 구해주는 연산자이다.

 

예제
 
#include <iostream>
using namespace std;
int main()
{
	int a, b;
	float c;
	double d;
	a = sizeof(int);
	b = sizeof(char);
	cout << "int = " << a << "byte, " << "char = " << b << "byte" << endl;
	cout << "float = " << sizeof(c) << "byte, " << "double = " << sizeof(d) << "byte" << endl;
	return 0;
}
실행결과
int = 4byte, char = 1byte
float = 4byte, double = 8byte
  • sizeof(데이터형), sizeof(변수)에 대한 결과값이 메모리의 바이트로 출력된다.
참고

1. 장인성 외 5인, (초보자도 쉽게 따라 할 수 있는) C++프로그래밍, 광문각, 2017.02.13

 

'C++ 기초 1' 카테고리의 다른 글

[C++] 조건 연산자  (0) 2024.01.16
[C++] 캐스트(cast) 연산자  (0) 2024.01.15
[C++] goto문 설명 및 예제  (1) 2024.01.11
[C++] 6-5 프렌드 연산자 함수  (1) 2024.01.10
[C++] 6-4 []연산자의 오버로드  (1) 2024.01.09