Sobes.tech
Junior

Wie viel Speicherplatz benötigen Datentypen wie bool, char, short, int, long long, float und double?

sobes.tech KI

Antwort von AI

Die Größe der eingebauten Datentypen in C und C++ ist nicht strikt durch den Standard festgelegt, außer sizeof(char), das immer 1 ist. Die anderen Größen können je nach Prozessorarchitektur und Compiler variieren. Es gibt jedoch garantierte Mindestbereiche und typische Größen auf den meisten modernen Systemen (z.B. x86-64):

Datentyp Minimale Bereich (C/C++) Übliche Größe (Bytes)
bool Nicht explizit durch den Standard definiert 1
char Basierend auf einem Byte (>= 8 Bit) 1
short >= [-32767, +32767] (>= 16 Bit) 2
int >= short (>= 16 Bit) 4
long long >= [-9223372036854775807, +9223372036854775807] (>= 64 Bit) 8
float Einfachpräzision (üblicherweise IEEE 754) 4
double Doppelpräzision (üblicherweise IEEE 754) 8

Die tatsächliche Größe kann mit dem Operator sizeof ermittelt werden:

#include <iostream>

int main() {
    std::cout << "sizeof(bool): " << sizeof(bool) << " Bytes" << std::endl;
    std::cout << "sizeof(char): " << sizeof(char) << " Bytes" << std::endl;
    std::cout << "sizeof(short): " << sizeof(short) << " Bytes" << std::endl;
    std::cout << "sizeof(int): " << sizeof(int) << " Bytes" << std::endl;
    std::cout << "sizeof(long long): " << sizeof(long long) << " Bytes" << std::endl;
    std::cout << "sizeof(float): " << sizeof(float) << " Bytes" << std::endl;
    std::cout << "sizeof(double): " << sizeof(double) << " Bytes" << std::endl;
    return 0;
}