Lesson 2 of 23
cat
The cat Command
cat (short for concatenate) reads files and writes their contents to stdout. In its simplest form:
$ cat notes.txt
Learn Linux
Practice daily
Have fun
The real cat reads from files or stdin, but the core operation is: take a string, print it unchanged.
Your Implementation
Write void my_cat(const char *s) that prints s exactly as-is — no extra newlines, no modifications.
The key difference from echo: cat does not add a newline. The string already contains its own newlines:
void my_cat(const char *s) {
printf("%s", s); // No \n — the content has its own
}
Walking Character by Character
You could also print one character at a time:
void my_cat(const char *s) {
while (*s) {
putchar(*s);
s++;
}
}
Both work. The putchar version shows you how cat actually works under the hood — it reads bytes from input and writes bytes to output, one at a time.
Your Task
Implement my_cat so it prints the string exactly as given.
TCC compiler loading...
Loading...
Click "Run" to execute your code.