Marvellously, a large group of programmers believe in this golden rule of compilation: "Errors - Oh, No! Warnings - I Don’t care!"
Please! THIS IS NOT TRUE. WARNINGS ARE INDICATORS OF LURKING PROBLEMS, THAT CAN (AND USUALLY WILL) LEAD TO REAL PROBLEMS LATER!!!
A program that compiles without errors, but gives a few warnings need not necessarily run successfully. Runtime errors (like, segmentation fault (SIGSEGV)), wrong answers or “unexplainable behaviours” can all happen, because of some reason mentioned in a warning!
The warnings are there for a reason. Please read and understand them properly, and try to remove them from your code (of course, understand, why the warning came in the first palce, and then make necessary changes). I would encourage you to enable all warnings during compilation, when you are compiling your program locally. In C/C++, this can be done by adding the compiler option
-Wall during the compilation step.
gcc prog.c -o prog -Wall
This will display all warnings, which might have been hidden by default. Armed with this, make sure that your program has no compile time warnings as well, before executing your program.
PS: Of course, this does not guarantee, that a program that compiles without errors and warnings, will not throw a runtime error. Only, the number of instances of that happening will be reduced.
In C/C++, the simplest and most innocent warning message that anyone can come across is
``` format '%d' expects type 'int *', but argument 2 has type 'int' ```This comes when you forget to put an `&` in your `scanf`. This will almost surely throw a SIGSEGV.
scanf("%d", t); into
To share an experience, I have a friend, who almost failed his lab exams, because of forgetting a single
&. He got through with very low marks simply based on the design he submitted. You can imagine the frustration, when he identified who/what the culprit was.
Many a times, trying to remove warnings from someone’s code have helped me to recover their code from runtime errors. In a few occasions, I could also correct some wrong answers as well.
I am posting this here, in the hope that at least a few of you shall also benefit from my experiences.