Add module2_datatypes.c

This commit is contained in:
2026-04-15 00:11:16 +02:00
parent 71f396b73a
commit 98191dd14e

View File

@@ -0,0 +1,130 @@
// ============================================
// Modul 2: Datentypen
// Beispiele in C
// ============================================
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <float.h>
// Beispiel 2.1: Typ-Größen und Grenzen
void demo_types() {
printf("=== C Datentypen ===\n\n");
// Ganzzahlige Typen
printf("Ganzzahlige Typen:\n");
printf(" char: %zu bytes, %d bis %d\n", sizeof(char), CHAR_MIN, CHAR_MAX);
printf(" short: %zu bytes, %d bis %d\n", sizeof(short), SHRT_MIN, SHRT_MAX);
printf(" int: %zu bytes, %d bis %d\n", sizeof(int), INT_MIN, INT_MAX);
printf(" long: %zu bytes, %ld bis %ld\n", sizeof(long), LONG_MIN, LONG_MAX);
printf(" long long: %zu bytes\n", sizeof(long long));
// Unsigned
printf("\nUnsigned Typen (nur positiv):\n");
printf(" unsigned int: 0 bis %u\n", UINT_MAX);
// Fließkomma
printf("\nFließkomma:\n");
printf(" float: %zu bytes, %d Dezimalstellen\n", sizeof(float), FLT_DIG);
printf(" double: %zu bytes, %d Dezimalstellen\n", sizeof(double), DBL_DIG);
// Fixed-Width Typen (stdint.h)
printf("\nFixed-Width Typen (portabel):\n");
printf(" int8_t: %zu bytes\n", sizeof(int8_t));
printf(" int32_t: %zu bytes\n", sizeof(int32_t));
printf(" int64_t: %zu bytes\n", sizeof(int64_t));
}
// Beispiel 2.2: Integer Overflow
void demo_overflow() {
printf("\n=== Integer Overflow ===\n");
int max = INT_MAX;
printf("INT_MAX = %d\n", max);
int overflow = max + 1;
printf("INT_MAX + 1 = %d (Overflow!)\n", overflow);
// Lösung: Größeren Typ nutzen oder vorher prüfen
long long safe = (long long)max + 1;
printf("Mit long long: %lld (Korrekt)\n", safe);
}
// Beispiel 2.3: Typ-Konvertierung
void demo_conversion() {
printf("\n=== Typ-Konvertierung ===\n");
// Implizit (automatisch)
int i = 10;
float f = i; // int → float (verlustfrei)
printf("int %d → float %f\n", i, f);
// Explizit (Cast)
float pi = 3.14159;
int approx = (int)pi; // float → int (Nachkommastellen verloren!)
printf("float %f → int %d (Truncation)\n", pi, approx);
// Gefährlich: signed ↔ unsigned
unsigned int u = 4000000000U;
int s = (int)u; // Implementation-defined!
printf("unsigned %u → signed %d (Problem!)\n", u, s);
}
// Beispiel 2.4: Strings in C (char arrays)
void demo_strings() {
printf("\n=== Strings in C ===\n");
// String-Literal (read-only!)
char* literal = "Hello"; // Im Data-Segment, nicht modifizierbar
printf("Literal: %s (Adresse: %p)\n", literal, (void*)literal);
// Modifizierbarer String (Stack oder Heap)
char mutable[] = "World"; // Kopie auf Stack
mutable[0] = 'w';
printf("Mutable: %s\n", mutable);
// Dynamischer String
char* dynamic = malloc(100);
strcpy(dynamic, "Dynamisch");
printf("Dynamic: %s\n", dynamic);
free(dynamic);
// String-Länge
printf("Länge von \"Hello\": %zu\n", strlen("Hello"));
}
// Beispiel 2.5: Bool in C (C99+)
#include <stdbool.h>
void demo_bool() {
printf("\n=== Boolean in C99 ===\n");
bool flag = true; // 1
bool other = false; // 0
printf("true = %d, false = %d\n", flag, other);
// In C: 0 ist falsy, alles andere truthy
if (42) {
printf("42 ist truthy!\n");
}
}
// ============================================
// Hauptprogramm
// ============================================
int main() {
demo_types();
demo_overflow();
demo_conversion();
demo_strings();
demo_bool();
printf("\n=== Wichtige Konzepte ===\n");
printf("1. Kenne die Grenzen deiner Typen\n");
printf("2. Overflow ist undefiniertes Verhalten bei signed\n");
printf("3. C-Strings sind Null-terminierte char-Arrays\n");
printf("4. Verwende stdint.h für portablen Code\n");
return 0;
}