Typy danych C++


Typy danych C++

Jak wyjaśniono w rozdziale Zmienne , zmienna w C++ musi być określonym typem danych:

Przykład

int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99;     // Floating point number
double myDoubleNum = 9.98;   // Floating point number
char myLetter = 'D';         // Character
bool myBoolean = true;       // Boolean
string myText = "Hello";     // String

Podstawowe typy danych

Typ danych określa rozmiar i typ informacji, które zmienna będzie przechowywać:

Data Type Size Description
int 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits
boolean 1 byte Stores true or false values
char 1 byte Stores a single character/letter/number, or ASCII values

Więcej o poszczególnych typach danych dowiesz się w kolejnych rozdziałach.


Ćwiczenia C++

Sprawdź się za pomocą ćwiczeń

Ćwiczenie:

Dodaj poprawny typ danych dla następujących zmiennych:

 myNum = 9;
 myDoubleNum = 8.99;
 myLetter = 'A';
 myBool = false;
 myText = "Hello World";