Imagine you're a new programmer that started learning C. You start traditionally with the hello world program:
printf("Hello, World!");
You're happy, you love this language, you would grade it
. To celebrate you modify your program:
printf("This is cool! 100%");
Uh, oh. Now you are doing undefined behavior, and are exposed to the wrath of nasal demons, because you used printf format specifier wrong.
Okay, let's ignore that and go to the second lesson. Computers are for computing! You ask your tutorial how to add two numbers:
int8_t a = 2;
int8_t b = 3;
int8_t c = a + b;
Great! But these are chump numbers! I can calculate that in my head!
int8_t a = 123;
int8_t b = 87;
int8_t c = a + b;
Oh no, nasal demons again. And Xcode didn't even warn me about that.
What's next? If staments! I wanna write my own AI
char *message;
bool b = true;
if (b) {
message = "b is set to true";
} else {
message = "b is set to false";
}
printf("%s\n", message);
but this program is too abstract. Let's write something about me personally:
char *message;
bool iLikeMushrooms = false;
if (iLikeMushrooms) {
message = "cukr loves mushrooms a lot!";
}
printf("%s\n", message);
Oh no, nasal demons again because of uninitialized variable.
What is the next thing new programmers learn? Loops! Tutorial teaches you how to write "Hello world" forever:
while (true) {
printf("Hello, world!\n");
}
Nice. But I don't like welcoming the world that much. Let's make it more silent:
while (true) { }
Did you know that in C++ and earlier versions of C infinite loops that don't do anything are undefined behavior?
At this point you are angry. Why every time you do seemingly innocent changes to your program, you cause undefined behavior, which theoretically can delete your hard drive? This time you don't want any of that. You will copy the tutorial, and not change even a single letter of the source code.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter the first value:");
scanf("%d", &a);
printf("Enter the second value:");
scanf("%d", &b);
c = a + b;
printf("%d + %d = %d\n", a, b, c);
return 0;
}
You run it, and... no tutorial I could find checks if scanf failed. You buy a bottle of vodka to drink with your band of nasal demons. You start thinking about rewriting your brain in rust.