There are 2 types of programming errors, which are:
- compilation errors
- errors of execution
About the Compilation Errors
They occur when the program we write does not obey the language rules. This type of error is usually easy to detect. The most frequent ones are:
- Forget a semicolon
- Forget to close a key
- Forget to close the quotation marks on a
printf
or scanf
- Forget to close a comment
- Forget declaration of variables
- Misspelling a word (e.g.:
studio.h
instead of stdio.h
)
About the Errors of Execution
They are more delicate, imperceptible. The program compiles normally without errors, but when running it does not do what we expect it to do. In computer jargon, we call this bug type by bugs.
Many programming beginners are frustrated when they fail to detect the bugs. With experience, you will see that you start to detect them quickly. The important thing is not to panic.
To detect a bug, should try to isolate the error. This can be done using printf
in the middle of the code to see what the program is doing step by step. Alternatively, there are special programs called debuggers that allow you to run the program step by step and see the contents of the variables of the program.
Below are some of the bugs classics occurring in C
. They are classic because there is no programmer for C
who have never made them. I am sure that sooner or later all of you will also make (or have already made) these mistakes, which are:
- Forget to initialize variables
- Forget the
&
(commercial) in scanf
- Do not put the
flags
correct (%d
,%c
,%f
,...) in the printf
or scanf
- Utilise
=
instead of ==
- Infinite cycle
- Semicolon in the wrong place
- Do not group instructions when a composite instruction is desired
Your code has some of these problems, so let’s review the same now to make the corrections...
First Problem (Forgot to initialize variables):
int filial;
float v1, v2, v3, totalf;
Correction:
int filial = 0;
float v1 = 0.0, v2 = 0.0, v3 = 0.0, totalf = 0.0;
Second Problem (Do not group instructions when you want a compound instruction and do not insert logical conditions in the else
):
if(totalf != 0)
printf("\no valor total desta filial sera de: %f", totalf);
else(total == 0)
printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");
Correction (Correctly identify the printf
and remove the expression (total == 0)
of else
:
if(totalf != 0)
printf("\no valor total desta filial sera de: %f", totalf);
else
printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");
Or (Close Code Block with Keys):
if(totalf != 0) {
printf("\no valor total desta filial sera de: %f", totalf);
} else {
printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");
}
Or (Use Ternary Conditional Operator):
(totalf != 0) ? printf("\no valor total desta filial sera de: %f", totalf) : printf("\nO valor nao podera ser exibido, favor digitar um valor diferente de 0");
I suggest you read the community guidelines to make a good question.
– user148754
I will recommend abandoning Dev-C++. This IDE has not been updated for many years and contains numerous problems. There are better alternatives. Right now, I’m recommending Visual Studio Community Edition for having a good support and numerous tutorials for using the IDE. It also works on Linux.
– José
@Jose, I suggest Douglas use the Code::Blocks Ides.
– user148754
I believe that the question should not be edited to correct the code posted by the author. The way it turned out some answers were totally meaningless. If you want to correct the code do in your reply.
– anonimo
@THIAGODEBONIS I recommend that he use an IDE like Atom or Sublime Text. For the build use a compiler like GCC or Mingw (if it’s Windows).
– user137493
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero