C is a programming language that had a massive impact on the evolution of programming languages. It was created at Bell Labs and designed to be a system programming language for the Unix operating system.
C programming language efficiency led to impactful system development with global significance. Operating systems like Microsoft Windows and its likes were written in C.
C programming language introduced a few concepts that modern programming languages inherited. C introduced the concept of structured programming, pointers, efficiency, etc. Overall, C influenced the development of programming.
But the C programming language also introduced a harmful keyword (well, it was considered dangerous). This keyword is described as leading to an unmaintainable spaghetti code.
It is easy to write spaghetti code. With this keyword, it is almost impossible to avoid it. What is the keyword? Well, it is called GOTO. It is considered deprecated. Many code-style tools omit the keyword. But it's still available and rarely used.
The GOTO keyword sounds obsolete, as modern C programming tutorials and books tend to skip the topic.
GOTO
So, what's harmful about the keyword? I'm glad you asked. Edsger W. Dijkstra wrote a letter in 1968 on how the GOTO keyword complicates the task of analysing and verifying the correctness of programs. He described that the GOTO keyword makes it hard to examine a program's functionality state. The keyword complicates the process of debugging and testing.
The creator of the C programming language, Dennis Ritchie, warned that the GOTO keyword can be abused indefinitely.
The "GOTO" keyword is powerful and harmful. It can introduce hard-to-read code and make one's code complex and unmaintainable. I believe it's a god-like keyword in the C programming language and should be avoided at all costs.
Is the "GOTO" keyword still used today? No, and Yes (I saw someone use the keyword in his code; hence, I assumed). According to computer science professor John Regehr, in 2013, there were about 100,000 instances of goto in the Linux kernel code, so it's probably still used to date, but I'll say it's rarely used.
So, how can you use the "GOTO" keyword? Well, it's simple (and harmful, lol).
#include <stdio.h> // to access printf
int main(int argc, char **argv) {
int number = 1;
int max = 5;
loop:
printf("%d ", number++);
if (number <= max)
goto loop;
return (0);
}
Compile and run the code above.
$ gcc goto.c -o goto
$ ./goto
1
2
3
4
5
$
As you can see, That's another way to create a loop without the "while" and "for" loops. The "goto" keyword instructs the program to, as its name implies, go to or jump to the "loop" label after printing the value of "number." It does this until the "number" is 5.
When the "goto" keyword is used, it introduces this jumping-around style in code, making your code difficult to read and, with time, becomes complicated, irrational, and unmaintainable.
Here's another example:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int i = 1;
say_hello:
printf("Hello %d\n", i);
loop:
printf("%d\n", i++);
if (i == 5)
goto say_hello;
if (i <= 10)
goto loop;
return 0;
}
Here's the output:
Hello 1
1
2
3
4
Hello 5
5
6
7
8
9
10
You can see the jumping-around effect of the "GOTO" keyword in this code.
Conclusion
The GOTO keyword is not a mistake. Unknowingly, everything man has ever made has never been perfect. Our inventions and innovations have always solved one or many problems yet still introduce a new one.
This is the case with the GOTO keyword here. The keyword is a powerful feature in the C programming language. But not everyone can handle the great responsibility that comes with it.
I hope you learn from this article and enjoy it. Please let me know if you enjoyed this article. You can help me by leaving a like and a comment for me. I appreciate you for reading the article to this point.
Do you like to read more about programming languages such as JavaScript, Python, C, C++, etc? Then please follow me and subscribe to my newsletter.
Happy coding! ⌨️