Junior
Câtă memorie ocupă tipurile de date precum bool, char, short, int, long long, float și double?
sobes.tech AI
Răspuns de la AI
Dimensiunea tipurilor de date încorporate în C și C++ nu este fixată strict de standard, cu excepția sizeof(char), care este întotdeauna egal cu 1. Celelalte dimensiuni pot varia în funcție de arhitectura procesorului și de compilator. Cu toate acestea, există intervale minime garantate și dimensiuni tipice pe majoritatea sistemelor moderne (de exemplu, x86-64):
| Tip de date | Interval minim (C/C++) | Dimensiune obișnuită (octeți) |
|---|---|---|
bool |
Nu este definit explicit de standard | 1 |
char |
Bazat pe un octet (>= 8 biți) | 1 |
short |
>= [-32767, +32767] (>= 16 biți) | 2 |
int |
>= short (>= 16 biți) |
4 |
long long |
>= [-9223372036854775807, +9223372036854775807] (>= 64 biți) | 8 |
float |
Precizie simplă (de obicei IEEE 754) | 4 |
double |
Precizie dublă (de obicei IEEE 754) | 8 |
Dimensiunea reală poate fi determinată folosind operatorul sizeof:
#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;
}