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:
- Using multiple variables in the print function
- Comparing Signed integer with unsigned integer
- Putting a semicolon at the end of the loop statement
- C preprocessor doesn’t need a semicolon
- Size of the string matters
- Macros and equations aren’t good friends
- Never compare Floating data type with double data type
- Arrays have a boundary
- Character constants are different from string literals
- 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.
- %d indicates Integer Format Specifier
- %f indicates Float Format Specifier
- %c indicates Character Format Specifier
- %s indicates String Format Specifier
- %u indicates Unsigned Integer Format Specifier
- %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 ) {
statement(s);
}
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 :
- Macro expansion
- File inclusion
- Conditional Compilation
- 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 :
- [Exponentiation](en.wikipedia.org/wiki/Exponentiation "Exponentiation") and [root extraction](en.wikipedia.org/wiki/Root_extraction "Root extraction")
- [Multiplication](en.wikipedia.org/wiki/Multiplication "Multiplication") and [division](en.wikipedia.org/wiki/Division_%28mathemati.. "Division (mathematics)")
- [Addition](en.wikipedia.org/wiki/Addition "Addition") and [subtraction](en.wikipedia.org/wiki/Subtraction "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.