c-01-bits/hallo-welt.c

36 lines
775 B
C
Raw Normal View History

2026-06-08 20:27:18 +02:00
#include "hallo-welt.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef HALLO
#define HALLO "Hallo Welt"
#endif
void hallo_welt(void) { printf(HALLO); }
// use more then one int type
const char *binaerzahl(uint8_t bin) {
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
if (!str)
return NULL;
uint8_t bb;
for (int i = sizeof(bin) * 8 - 1; i >= 0; i--) {
bb = bin >> i;
str[i] = (bb % 2 == 1) ? 1 : 0;
printf("%d", str[i]);
}
str[sizeof(bin)] = '\0';
return str;
}
2026-06-08 21:01:48 +02:00
int getint(void) {
uint8_t i;
scanf("%hhu", &i);
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())); }