技術的なエラー , Regarding Variables
When learning how to code, variables can generate a massive amount of errors, a lot of them are pretty easy to avoid. We will see in this article how to get rid of some.
This very short article is mainly dedicated to beginners but might be useful to everyone. The idea is not only to give a list of common error but also to explain how to avoid them and give some good coding habits. Speaking of good habits, you should check this beauty made by Vincent ❤.
One of the major error regarding variables is the undeclared error. This error occurs all the time. She is easy to avoid but still, everyone that goes into programming will experienced it.
Let’s imagine that your program needs a value given by the user. A simple way to do so would be something like this:
There is so much wrong in this code. The variable myValue is not declared. That means that there is no memory space that is allocated to it, so when the program will run the scanf, he will have nowhere to put the value: Error.
This looks stupid but it might append during coding a long and complex function, you are in need of a temporary variable, you use it before creating it and here come the error: as simple as that.
Here is a much cleaner approach.
This code is showing a pretty good manner to have in programming. You can see that we have a variable called myTinySecurity. What the heck is this?
As its name suggests, this is an easy way to avoid improper entry, it means assuring that you get what you wanted and not something else. Improper entries are like Russian roulette. In the previous code, we are waiting that the user enters a int, but what if he does not? As you are probably thinking right now yes it will create an error if we are not capable of tracking it before.
As we are leaving in a beautiful world, C functions were designed to overcome those situations
The scanf function contains a security check. If everything went well, it returns the number of input items assigned. Reading the man of the function teaches you that it can perform conversion regarding type, but it can be dangerous. So you know what to expect if it runs well so you can prepare some code to handle the error.
Checking the output value of usual functions will often give you information regarding how went operations and can prevent a lot of bug. If you want to know more about a basic function, don’t hesitate to check its manual by the command: man scanf (for instance)
It is also highly recommended to develope the same security in your own functions with custom errors messages that will turn your awful debugging session into joy and pleasure.
This one is a bit tricker but still easy.
Let’s imagine that you want to calculate the sum of two numbers. As you’ve read the first part of this article, you’ll not forget to declare all your variable. Here is a code:
Ok, so this code will compile with no error. But once launched you might face this:
What happens? Well if we look at the line where c is declared:
We can see that we are doing two operations at the same time: Declaration and initialization.
The issue is that we are initializing a value as the sum of two uninitialized variables a and b. So we are adding two things that we don’t know the value. So there is no chance that it will give us the good result.
Here is the proper way.
All this error seems maybe absurdly easy to some, but a lot of programmers fall into at least once. Avoiding them is easy, however they must be one of the first verifications to do when debugging.
It’s over you can return skating with Paulette, see you next time! :-)