Zum Inhalt, überspringe Kopfzeile Zur Navigation, überspringe Kopfzeile
1: /** 2: * Projekt: Datentypen 3: * 4: * @author Stefan Jahn 5: * @version 20070316 6: * @date 16.03.2007 7: * 8: * @file main.cpp 9: */ 10: 11: #include <iostream> 12: using namespace std; 13: 14: /** 15: * main-Funktion 16: */ 17: int main (int argc, char const *argv[]) { 18: cout << "Datentypen:" << endl; 19: cout << "===========" << endl; 20: 21: // Integer 22: int Integer = 5; 23: 24: // Float: Fließkommazahlen 25: float Float = 3.14; 26: 27: // Double: Fließkommazahlen mit doppelter Genauigkeit 28: double Double = 2.5; 29: 30: // Ein Character (Buchstabe) oder Zahl von 0...255 31: char Char1 = ' '; 32: char Char2 = 10; 33: 34: // Boolean: Wahr (true = 1) oder Unwahr (false = 0) 35: bool Bool1 = true; 36: bool Bool2 = 0; 37: 38: // Standardmässig sind alle Zahlen-Datentypen signed, also mit Vorzeichen 39: // und können somit negative und positive Zahlen enthalten. Man kann 40: // aber auch gezielt ein unsigned Datentyp angeben. 41: signed int SignedInteger = 5; 42: 43: // Benötigt man kein Vorzeichen bzw. negative Zahlen so erreicht man das 44: // mit dem Modifikator unsigned. Vorteil: Der Zahlenbereich vergrößert 45: // sich um ein Bit links da nun das Vorzeichen nicht mehr gespeichert 46: // werden muß. 47: unsigned int UnsignedInteger = 5; 48: unsigned char UnsignedChar = ' '; 49: signed char SignedChar = ' '; 50: 51: // Man kann die Größe des Datentypes mit den Modifikatoren long und 52: // short beeinflussen. 53: long int LongInteger1 = 5; 54: short int ShortInteger1 = 5; 55: long double LongDouble = 2.5; 56: 57: // Wird der Datentyp bei long und short weggelassen, so wird automatisch 58: // von einem int ausgegangen. 59: long Longinteger2 = 5; 60: short ShortInteger2 = 5; 61: 62: // Man kann Vorzeichen und Größe auch kombinieren 63: unsigned short int UnsignedShortInt = 5; 64: signed short int SignedShortInt = 5; 65: unsigned long int UnsignedLongInt = 5; 66: signed long int SignedLongInt = 5; 67: 68: // Folgende Modifikator-Varianten sind nicht möglich bzw. erlaubt: 69: // - short double 70: // - long float 71: // - short float 72: // - short char 73: // - long char 74: // - unsigned float 75: // - signed float 76: 77: // Speicherbelegung der einzelnen Datentypen in Bytes ermittelt man 78: // mit der Funktion sizeof(). Der Speicherverbrauch kann/ist von 79: // Plattform (z.B. CPU-Typ) zu Plattform unterschiedlich. 80: cout << "Größe der einzelnen Datentypen in Bytes:" << endl; 81: cout << "int = " << sizeof(Integer) << endl; 82: cout << "float = " << sizeof(Float) << endl; 83: cout << "double = " << sizeof(Double) << endl; 84: cout << "char = " << sizeof(Char1) << endl; 85: cout << "bool = " << sizeof(Bool1) << endl; 86: cout << "signed int = " << sizeof(SignedInteger) << endl; 87: cout << "unsigned int = " << sizeof(UnsignedInteger) << endl; 88: cout << "signed char = " << sizeof(SignedChar) << endl; 89: cout << "unsigned char = " << sizeof(UnsignedChar) << endl; 90: cout << "short int = " << sizeof(ShortInteger1) << endl; 91: cout << "long int = " << sizeof(LongInteger1) << endl; 92: cout << "long double = " << sizeof(LongDouble) << endl; 93: cout << "unsigned short int = " << sizeof(UnsignedShortInt) << endl; 94: cout << "signed short int = " << sizeof(SignedShortInt) << endl; 95: cout << "unsigned long int = " << sizeof(UnsignedLongInt) << endl; 96: cout << "signed long int = " << sizeof(SignedLongInt) << endl; 97: 98: return 0; 99: }
Zum Inhalt zurück, überspringe Navigation | Zum Seitenanfang