C/C++ラーニング-基本タイプのバイト数

5213 ワード

基本タイプ
ここでいう基本的なタイプは、次のようなものです.
 int
 unsigned int
 long int
 long long
 short int
 char
 float
 double

これらのタイプが占めるバイト数は、実際には一定ではありません.特にintというタイプです.
32ビットコンパイラと64ビットコンパイラでは異なる可能性があります.
64ビットコンパイラで実行した結果を次に示します.
皆さんは自分の机の下で実行して见ることができます.
//
// main.cpp
// TypeBit
//
// Created by Alps on 15/4/1.
// Copyright (c) 2015  chen. All rights reserved.
//

#include <iostream>
using namespace std;

int main(int argc, const char * argv[]) {
    short int a = 0;
    int b = 0;
    unsigned int c = 0;
    long int d = 0;
    long long e = 0;
    char f = 0;
    float g = 0;
    double h = 0;

    printf("short int is: %lu byte
"
,sizeof(a)); printf("int is : %lu byte
"
,sizeof(b)); printf("unsigned int is: %lu byte
"
,sizeof(c)); printf("long int is : %lu byte
"
,sizeof(d)); printf("long long is : %lu byte
"
,sizeof(e)); printf("char is : %lu byte
"
,sizeof(f)); printf("float is : %lu byte
"
,sizeof(g)); printf("double is : %lu byte
"
,sizeof(h)); return 0; }

実行結果は次のとおりです.
short int is: 2 byte
int is : 4 byte
unsigned int is: 4 byte
long int is : 8 byte
long long is : 8 byte
char is : 1 byte
float is : 4 byte
double is : 8 byte
Program ended with exit code: 0
charはバイトです他はコンパイラとオペレーティングシステムを見なければなりません.
そうだ~1バイトは8 bit.