#include <iostream>
using namespace std;
int main() {
int i = 12;
// keine ganze Zahl, daher wird abgeschnitten: j = 23
int j = 23.5;
cout << i << "," << j << endl;
// compiliert nicht, da 23.5 kein int ist
// und Informationsverlust droht bei der Umwandlung
// von double in int:
// int j3 {23.5};
return 0;
}
#include <iostream>
using namespace std;
int main() {
int i = 12;
// keine ganze Zahl, daher wird abgeschnitten: j = 23
int j = 23.5;
// k ist nicht initialisiert
// k kann irgendeinen Wert haben
int k;
cout << i << "," << j << "," << k << endl;
// alternative Definition in einer Zeile:
int i2 = 12, j2 = 23.5;
// Vorsicht! i3 nicht initialisiert!
int i3, j3 = 100;
cout << i3 << "," << j3 << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
// Hinweis: unsigned short m -> dann würde es passen
m = 32767;
cout << "m=" << m << endl;
m = m + 1;
cout << "m=" << m << endl;
unsigned short us = 100;
us = us - 101;
cout << "us=" << us << endl;
us = us + 1;
cout << "us=" << us << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
// character vs. ASCII-Code
char x = 98;
cout << x << " <-> " << static_cast<int>(x) << endl;
// 1023 kann von unsigned char nicht dargestellt werden
unsigned char uc = 1023;
// um den Zahlen-Wert von uc zu bekommen:
int uci = uc;
cout << "uci=" << uci << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
// besser:
// compiliert nicht, da 1023 nicht durch unsigned char
// dargestellt werden kann:
// unsigned char uc2 {1023};
// 1023 passt natürlich auch nicht in einen signed char,
// da Initialisierung mit "=" compiliert das aber:
// signed char sc = 1023;
// gleiches Problem: -123 ist kein unsigned
// unsigned int ui = -123;
// cout << "ui=" << ui << endl;
// das wuerde nicht compilieren:
// unsigned int ui2 {-123};
return 0;
}
#include <iostream>
#include <limits>
using namespace std;
int main() {
cout << "int: "
<< numeric_limits<int>::min() << " ... "
<< numeric_limits<int>::max() << endl;
cout << "double: "
<< numeric_limits<double>::min() << " ... "
<< numeric_limits<double>::max() << endl;
cout << "Anzahl Bytes von char: " << sizeof(char) << endl;
cout << "Anzahl Bytes von int: " << sizeof(int) << endl;
cout << "Anzahl Bytes von double: " << sizeof(double) << endl;
int i = 10;
cout << "sizeof(i)=" << sizeof(i) << endl;
return 0;
}
Achtung bei Operationen mit mehreren Operanden unterschiedlicher Größer:
Compiler wandelt Typen ggf. um → potentieller Verlust von Informationen
#include <iostream>
using namespace std;
int main() {
{ // A
int a = 1024;
short b = 256, c = 1025;
short result = b + c * a;
cout << "A: result = " << result << endl;
}
{ // B
int a = 1, b = 256, c = 1024;
int result = b + a / 2 * c;
cout << "B: result = " << result << endl;
}
return 0;
}
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
float f = 0.1;
double d = 0.1;
cout << std::setprecision(6);
cout << "f = " << f << "\nd = " << d << endl;
cout << std::setprecision(32);
cout << "f = " << f << "\nd = " << d << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
bool test = true;
cout << "test=" << test << endl;
test = 1;
cout << "test=" << test << endl;
// damit "true" und "false" ausgegeben wird, statt 1 und 0
cout << boolalpha;
cout << "test=" << test << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 100, k = 123;
bool c = k;
// bool d {123}; // kompiliert nicht
bool f = 0.1; // alles != 0 ist true
cout << "c=" << c << ", f=" << f << endl;
if (a) {
cout << "a ist true" << endl;
cout << "bla" << endl;
}
if (!a) cout << "a ist false" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int feld[5] = {1,2,3,4,5};
cout << feld[0] << "," << feld[1] << "," << feld[2] << "," << feld[3] << "," << feld[4] << endl;
// Zuweisung an Feld-Element
feld[0] = 11;
// Ausgabe von Feld-Element
cout << feld[0] << endl;
// Vorsicht! Bereichs-Überschreitung
cout << feld[5] << endl;
cout << feld[50000000] << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {2,3,6,8};
// Zugriff wie beim Array
cout << v[0] << "," << v[1] << endl;
v[2] = 5;
// Größe
cout << "|v|=" << v.size() << endl;
// Einfügen
v.push_back(13);
cout << "|v|=" << v.size() << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {2,3,6,8};
cout << v[0] << ","
<< v[1] << ","
<< v[2] << ","
<< v[3] << endl;
// Vorsicht: Bereichsueberschreitung
cout << v[10] << endl;
// mit v.at(10) greift man auch auf das 10.
// schmeißt ggf. Exception (Kapitel 12)
cout << v.at(10) << endl;
return 0;
}
#include <iostream>
using namespace std;
struct UnserDatentyp {
char name[40];
unsigned int matrikel;
bool eidp;
};
int main() {
UnserDatentyp student = {"Hugo Hase", 44221, true};
UnserDatentyp studenten[50000];
unsigned int mnr = student.matrikel;
cout << student.name << " " << mnr << endl;
return 0;
}
Vector
char
; immer mit NullterminierungString
struct
)
studierende.name