Analytics Vidhya
Published in

Analytics Vidhya

Dicey Issues in C/C++

C/C++ problems

If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends. The following displays some of the issues:

  1. Using multiple variables in the print function
  2. Comparing Signed integer with unsigned integer
  3. Putting a semicolon at the end of the loop statement
  4. C preprocessor doesn’t need a semicolon
  5. Size of the string matters
  6. Macros and equations aren’t good friends
  7. Never compare Floating data type with double data type
  8. Arrays have a boundary
  9. Character constants are different from string literals
  10. Difference between single(=) and double(==) equal signs.

The below code generates no error since a print function can take any number of inputs but creates a mismatch with the variables. The print function is used to display characters, strings, integers, float, octal, and hexadecimal values onto the output screen. The format specifier is used to display the value of a variable.

  1. %d indicates Integer Format Specifier
  2. %f indicates Float Format Specifier
  3. %c indicates Character Format Specifier
  4. %s indicates String Format Specifier
  5. %u indicates Unsigned Integer Format Specifier
  6. %ld indicates Long Int Format Specifier

A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a non-negative integer in the range [0 to 4294967295]. The signed integer is represented in twos-complement notation. In the below code the signed integer will be converted to the maximum unsigned integer then compared with the unsigned integer.

A for loop is a control structure that allows you to execute a block of code several times. The syntax of a for loop in C programming language is -

for ( init; condition; increment ) {

Semicolons indicate the termination of a statement. A semicolon at the end of the loop statement will iterate until the condition is met and then proceed to the statement inside.

The C preprocessor is a program that processes your source program before is passed to the compiler. Preprocessor commands (known as directives) form what can almost be considered a language within C language. Preprocessor are used in the following :

  1. Macro expansion
  2. File inclusion
  3. Conditional Compilation
  4. Miscellaneous directives

Have a look at the following program.

In this program, the semicolon added to macro generates an error which is why you should avoid using semicolons in the macros.

A string is a sequence of characters terminated with a null character. You can use a scan function to read a string. The scan function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc). User input could be more than the size of the array. A clever programmer uses this technique to avoid potential hackers from exploiting the string buffer.

A macro is a piece of code that has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros — object-like macros resemble data objects when used and function-like macros resemble function calls. An object-like macro is a simple identifier that will be replaced by a code fragment. A function-like macro is only expanded if its name appears with a pair of parentheses after it.

In the below code the programmer desired the output of 9 but got 5. This was likely due to the sequence of precedence, which is :

  1. Exponentiation and root extraction
  2. Multiplication and division
  3. Addition and subtraction

In the below code n1 gets initialized and is rounded up to single precision, resulting in value 0.10000000149011612. Then n1 is converted back to double precision to be compared with 0.1 literal (which equals to 0.10000000000000001)which creates a mismatch.

Arrays are a data structure that stores elements in a sequential manner. The elements inside an array are stored linearly in the memory. A programmer should avoid directly using the values for the array size in a loop statement. Garbage values are displayed when the program oversteps the array boundary.

In C programming, character constants are different than string literals. A character surrounded by single quotes is a character constant. A character constant is an integer whose value is the character code that stands for the character. Zero or more characters surrounded by double quotes is a string literal. A string literal is an immutable array whose elements are of type char.

The equal (=) sign is used to store the value in a variable. The equal to (==) sign indicates a comparison between two values or variables. The below code depicts the beauty of using the equal (=) sign at the wrong place.

Hope you understand issues in c/c++ a bit more, thank you for reading.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store