c-01-bits/hallo-welt.c

38 lines
817 B
C
Raw Normal View History

2026-06-08 20:27:18 +02:00
#include "hallo-welt.h"
2026-06-08 22:22:47 +02:00
#include <stdbool.h>
2026-06-08 20:27:18 +02:00
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef HALLO
#define HALLO "Hallo Welt"
#endif
void hallo_welt(void) { printf(HALLO); }
2026-06-08 22:22:47 +02:00
int zweierkomplement(int32_t i) { return ~i + 1; }
2026-06-08 20:27:18 +02:00
// use more then one int type
2026-06-08 22:22:47 +02:00
const char *binaerzahl(int32_t bin) {
2026-06-08 20:27:18 +02:00
char *str = malloc(sizeof(bin) * 8 +
1); // Allocate 8 bits for actual data and 1 Bit for the \0
// sign indicating the end of the string
2026-06-08 22:22:47 +02:00
bool is_negative;
2026-06-08 20:27:18 +02:00
if (!str)
return NULL;
2026-06-08 22:22:47 +02:00
if (bin < 0) {
bin = zweierkomplement(bin);
is_negative = true;
2026-06-08 23:17:22 +02:00
return str;
2026-06-08 22:22:47 +02:00
}
2026-06-08 20:27:18 +02:00
}
2026-06-08 21:01:48 +02:00
int getint(void) {
2026-06-08 21:09:41 +02:00
uint32_t i;
2026-06-08 22:22:47 +02:00
printf("Geben Sie eine Dezimahlzahl ein: ");
2026-06-08 21:09:41 +02:00
scanf("%u", &i);
2026-06-08 21:01:48 +02:00
return i;
2026-06-08 20:27:18 +02:00
}
2026-06-08 21:01:48 +02:00
int main(void) { printf("%s", binaerzahl(getint())); }