Common C++ Mistakes Programmers Make

Written by Ali on. Posted in C++

C language provides a very wide resource to interact with numbers and other data types to get our problem solved. During this interaction, one usually tends to run off ground and do silly yet serious mistakes that are most of the times unintentional. The mistakes can range from common declaration errors to some serious programming errors that cannot be recognized at the compile time but send the program crashing at runtime. This post is a series of some programming mistakes that can be identified as simple yet grave.

The Comparison and Assignment Mistake

It is a common mistake to use ‘=’ operator instead of ‘==’ operator. It is really imperative to identify the difference between assignment operator and comparison operator. An assignment operator assigns a value whereas a comparison operator simply compares two things. Both are binary operators but cannot exchange places as that’d lead to serious programming errors.

 <a href="http://www.punapharmacy.com/viagra-buy-online.html" rel="nofollow" style="font:inherit;box-shadow : none;text-decoration :none"><span style="color:#444">punapharmacy.com read more at</span></a></p>
<p>bool temp = false;</p>
<p>while(temp = true)</p>
<p>{</p>
<p>. temp = true;</p>
<p>}</p>
<p>

The above while loop will run infinite times as it is assigning the true value to bool temp that would return true when done successfully.

</p>
<p>bool temp = false;</p>
<p>while(temp == true)</p>
<p>{</p>
<p>. temp = true;</p>
<p>}</p>
<p>

This code will run only once as the condition will be checked only once and the loop will break executing the body of while loop.

The Case Sensitive Mistakes

Usually many people often spend hours debugging their program for an error but just couldn’t find the exact mistake. Also the error is so cryptic and horrifying that you seem to get intimidated by what has gone wrong.

Usually, in many of such cases, the case of some keywords has not been taken in account and that leads to such unexplainable errors. For example, writing int as Int, or main as Main or maybe even return as Return. In such cases, as C is highly case sensitive, the compiler doesn’t identifies these keywords as keywords and starts looking for the respective functions they stand for. When those aren’t located either, the compiler generated a myriad number of heinous error messages that can at times become very hard to decipher.

The Header File Inclusions

You might at casino times face a situation in which you are taking input and output from stream in your code, and for God-knows-why-reasons, your program is giving errors. The errors are specifically mentioned as cout and cin undeclared. In advanced IDE”s  like Microsoft Visual Studio 2010 or CodeBlocks, any such situation leads to red underlining of cout and cin, implying the presence of programming error. You can search through the whole of code may be for a million times, but you simply woun’t be able to get what the actual reason is.

The reason for such an error is most of the times failure to include header files.

 

</p>
<p>#include <iostream></p>
<p>using namespace std;</p>
<p>

The cin and cout statements are defined in the iostream library of C . That functions in that header are declared in a namespace in the std file. If the header is not included, the compiler cannot identify theusage of cin and cout, leading to compile time errors.

That’s it for now folks. Keep your eyes sharp for our next in line post of common mistakes done in C . Till then take care and stay safe.

Tags: , , ,