Operatoren und Ausdrücke
Operatoren verbinden Werte zu Ausdrücken. C und C++ teilen fast alle, auch die Regeln der Ganzzahldivision, die Einsteiger überraschen.
Was ist Operatoren und Ausdrücke?
Operatoren verbinden Werte zu Ausdrücken. C und C++ teilen fast alle, auch die Regeln der Ganzzahldivision, die Einsteiger überraschen.
Verwende Rechen-, Zuweisungs-, Vergleichs- und logische Operatoren in C und C++.
Wichtige Punkte
- Aktiviere Compiler-Warnungen und behebe sie vollständig.
- Kenne Typ und Lebensdauer jedes Werts.
- Prüfe Eingaben und Array-Grenzen explizit.
Codebeispiele in C und C++
#include <stdio.h>
int main(void) {
int a = 17, b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b); /* integer division drops the fraction */
printf("a %% b = %d\n", a % b); /* remainder */
printf("a / 5.0 = %.1f\n", a / 5.0); /* one double operand gives a double */
int total = 10;
total += 5;
total *= 2;
printf("total = %d\n", total);
int count = 0;
count++;
printf("count = %d\n", count);
/* Comparisons and logical operators give 1 (true) or 0 (false). */
printf("a > b: %d, a == 17: %d\n", a > b, a == 17);
printf("a > 10 && b > 10: %d\n", a > 10 && b > 10);
printf("a > 10 || b > 10: %d\n", a > 10 || b > 10);
return 0;
}
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
a / 5.0 = 3.4
total = 30
count = 1
a > b: 1, a == 17: 1
a > 10 && b > 10: 0
a > 10 || b > 10: 1
#include <iostream>
int main() {
int a = 17, b = 5;
std::cout << "a + b = " << a + b << '\n';
std::cout << "a - b = " << a - b << '\n';
std::cout << "a * b = " << a * b << '\n';
std::cout << "a / b = " << a / b << '\n'; // integer division drops the fraction
std::cout << "a % b = " << a % b << '\n'; // remainder
std::cout << "a / 5.0 = " << a / 5.0 << '\n'; // one double operand gives a double
int total = 10;
total += 5;
total *= 2;
std::cout << "total = " << total << '\n';
int count = 0;
count++;
std::cout << "count = " << count << '\n';
// bool values print as true/false with std::boolalpha.
std::cout << std::boolalpha;
std::cout << "a > b: " << (a > b) << ", a == 17: " << (a == 17) << '\n';
std::cout << "a > 10 && b > 10: " << (a > 10 && b > 10) << '\n';
std::cout << "a > 10 || b > 10: " << (a > 10 || b > 10) << '\n';
}
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
a / 5.0 = 3.4
total = 30
count = 1
a > b: true, a == 17: true
a > 10 && b > 10: false
a > 10 || b > 10: true
Vergleich zwischen C und C++
Die Division zweier ints schneidet den Nachkommaanteil ab, daher ist 17 / 5 gleich 3; mach einen Operanden zum double, um ihn zu behalten. Vergleiche und && / || ergeben in C 1 oder 0 und in C++ true oder false, was std::boolalpha als Wörter ausgibt.
C stellt kleine prozedurale APIs und Repräsentationsdetails offen dar.
C++ behält das Low-Level-Modell und ergänzt sicherere Bibliothekstypen.
Übungsaufgaben
Führe beide Versionen aus und untersuche die unterschiedlichen Garantien.
- Füge ungültige und Grenzwerte hinzu.
- Kompiliere mit -Wall -Wextra -Wpedantic.
- Teile Deklaration und Implementierung auf.
Mit einer Challenge üben
Nutze den Inhalt dieser Lektion, um ein echtes Problem zu lösen. Jede Challenge läuft im Browser, mit sofortigen Tests und einem KI-Review.
- Celsius to Fahrenheit Convert a temperature from Celsius to Fahrenheit. Einfach
- Simple Interest Find the total amount after simple interest. Einfach