Skip to main content

Dicey Issues in C/C++

Dicey Issues in C/C++

C/C++ problems

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 ) {
 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 :

  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.

Comments

Popular posts from this blog

How to use Chess com API using Python

  How to use Chess com API using Python Chess is an amazing strategy-based game and if you have been following the recent boom of online chess then welcome to the chess club. Online chess is amazing since it allows you to play with a random stranger at your level or stockfish (computer). There are many popular online chess websites like lichess.org, chess.com, playchess.com, and newly created kasparovchess.com. Today we will be seeing how to use chess.com API for getting players' stats. You can create software and get affiliates from them (check out the link below), so share it with them if you are planning to create something. Before you start make sure you have the following things: Pre-requirements Postman Anaconda or mini conda or Python idle Any text editor of your choice Pretty good? Now let’s download the JSON file that chess com developers have already made for us from here and then you import it to the Postman. This just helps you with prewritten get methods so that ...

Create your own YouTube video and playlist downloader using QT designer in Python

Create your own YouTube video and playlist downloader using QT designer in Python The code for this software is available on my GitHub, the link is given below. Create your own YouTube video and playlist downloader using QT designer in Python This is the thing we are trying to make today for this article The code for this software is available on my GitHub, the link is given below. If you haven’t checked out my previous article on the same topic but not the GUI version, please check it out. 3 ways to download YouTube playlists at once using Python Have you guys ever wanted to download your entire youtube playlist? rahulbhatt1899.medium.com So before starting to code let's check if you have all the requirements beforehand: Pre-requirements : Qt designer Anaconda or mini conda or python 3.7 compiler Any text editor of your choice Libraries to download: Pytube Ffmpy PyQt5 This would be sectioned into the following structure: Creating the UI in Qt designer Converting UI file int...

Authentication Using Passport JS

Authentication is big and most complicated part of an application, but it is also the  preponderance  part of any web app/software in general. Passport JS Passport JS is a widely popular authentication module for  Node js.  The sole purpose is to authenticate requests and is based on the idea of pluggable authentication strategies ( including local strategy, there are more than 500 strategies currently  available  ). When using third party application Your app never receives your password  thus freeing the developer from the burden of security related to handling and storing passwords. The Passport authentication and its strategy will include protection against attacks like “man in the middle” and other vectors an attacker might exploit. We are going to use Facebook strategy, for now, to install Passport and Facebook strategy type in the following command: Next, we are going to write the authentication code, and we’ll be creating a different module cal...