티스토리 뷰

Piece

Built-In Fundamental Primitive Data Types

More Code 2018. 6. 20. 08:06

기본 자료형의 size를 byte 단위로 표시해보자.


참고 : [C#]

Reference Tables for Types

Default Values


참고 : [Java]

Primitive Data Types

Class Number

Class Boolean

Class Character


참고 : [C++]

Fundamental Types

Data Type Ranges

Integer Limits




// C# : Built-In Types
using System;

namespace Sharp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("sizeof(byte) : " + sizeof(byte) + " bytes");
Console.WriteLine("sizeof(short) : " + sizeof(short) + " bytes");
Console.WriteLine("sizeof(int) : " + sizeof(int) + " bytes");
Console.WriteLine("sizeof(long) : " + sizeof(long) + " bytes");
Console.WriteLine("sizeof(float) : " + sizeof(float) + " bytes");
Console.WriteLine("sizeof(double) : " + sizeof(double) + " bytes");
Console.WriteLine("sizeof(decimal) : " + sizeof(decimal) + " bytes");
Console.WriteLine("sizeof(bool) : " + sizeof(bool) + " bytes");
Console.WriteLine("sizeof(char) : " + sizeof(char) + " bytes");
}
}
}

/* Output
sizeof(byte) : 1 bytes
sizeof(short) : 2 bytes
sizeof(int) : 4 bytes
sizeof(long) : 8 bytes
sizeof(float) : 4 bytes
sizeof(double) : 8 bytes
sizeof(decimal) : 16 bytes
sizeof(bool) : 1 bytes
sizeof(char) : 2 bytes
*/




// Java : Primitive Data Types
package package1;

public class Program {
public static void main(String[] args) {
System.out.println("size of byte: " + Byte.SIZE + " bits " + Byte.BYTES + " bytes");
System.out.println("size of short: " + Short.SIZE + " bits " + Short.BYTES + " bytes");
System.out.println("size of int: " + Integer.SIZE + " bits " + Integer.BYTES + " bytes");
System.out.println("size of long: " + Long.SIZE + " bits " + Long.BYTES + " bytes");
System.out.println("size of float: " + Float.SIZE + " bits " + Float.BYTES + " bytes");
System.out.println("size of double: " + Double.SIZE + " bits " + Double.BYTES + " bytes");
System.out.println("size of char: " + Character.SIZE + " bits " + Character.BYTES + " bytes");
}
}

/* Output
size of byte: 8 bits 1 bytes
size of short: 16 bits 2 bytes
size of int: 32 bits 4 bytes
size of long: 64 bits 8 bytes
size of float: 32 bits 4 bytes
size of double: 64 bits 8 bytes
size of char: 16 bits 2 bytes
*/


Java는 boolean형에 대한 SIZE를 제공하지 않는다.


Range

[C#] byte  : 0 ~ 255

[C#] sbyte : -128 ~ 127

[Java] byte : -128 ~ 127




// C++ : Fundamental Types
#include <iostream>

using namespace std;

int main()
{
cout << "sizeof(short) = " << sizeof(short) << " bytes" << endl;
cout << "sizeof(int) = " << sizeof(int) << " bytes" << endl;
cout << "sizeof(long) = " << sizeof(long) << " bytes" << endl;
cout << "sizeof(long long) = " << sizeof(long long) << " bytes" << endl;
cout << "sizeof(float) = " << sizeof(float) << " bytes" << endl;
cout << "sizeof(double) = " << sizeof(double) << " bytes" << endl;
cout << "sizeof(long double) = " << sizeof(long double) << " bytes" << endl;
cout << "sizeof(bool) = " << sizeof(bool) << " bytes" << endl;
cout << "sizeof(char) = " << sizeof(char) << " bytes" << endl;
cout << "sizeof(wchar_t) = " << sizeof(wchar_t) << " bytes" << endl;
return 0;
}

/* Output of Visual C++
sizeof(short) = 2 bytes
sizeof(int) = 4 bytes
sizeof(long) = 4 bytes
sizeof(long long) = 8 bytes
sizeof(float) = 4 bytes
sizeof(double) = 8 bytes
sizeof(long double) = 8 bytes
sizeof(bool) = 1 bytes
sizeof(char) = 1 bytes
sizeof(wchar_t) = 2 bytes
*/

/* Output of GCC(MinGW)
sizeof(short) = 2 bytes
sizeof(int) = 4 bytes
sizeof(long) = 4 bytes
sizeof(long long) = 8 bytes
sizeof(float) = 4 bytes
sizeof(double) = 8 bytes
sizeof(long double) = 12 bytes
sizeof(bool) = 1 bytes
sizeof(char) = 1 bytes
sizeof(wchar_t) = 2 bytes
*/

/* Output of C++Builder
sizeof(short) = 2 bytes sizeof(int) = 4 bytes sizeof(long) = 4 bytes sizeof(long long) = 8 bytes sizeof(float) = 4 bytes sizeof(double) = 8 bytes sizeof(long double) = 10 bytes sizeof(bool) = 1 bytes sizeof(char) = 1 bytes sizeof(wchar_t) = 2 bytes
*/


sizeof(long double) is

08 bytes : Visual C++

12 bytes : GCC(MinGW)

10 bytes : C++Builder



공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함