Ahmad

Ahmad

#014 – Why std::cout << a << b << works

Consider the following: This statement contains multiple << operators that are chained in a single instruction. It reads as if it’s a sentence. The insertion operator (<<) operators on a C-style string literal, an escape sequence, a class object and…

#013 – Avoid default initialization for variables

Consider the following: This is default-initialization. This defines the variable with a garbage value (not zero). If we print its contents, it will be whatever value is sitting at that memory address. Who knows! When you don’t initialize a variable,…

#012 – Prefer ‘\n’ over std::endl

When printing text with std::cout, we often want to end the line. C++ gives us more than one way to do that, but they are not identical. \\n inserts a newline. std::endl inserts a newline and flushes the stream The usual case: print a newline Most…

#010 – The modern way of variable initialization

Three things to take away: Definition, assignment, and initialization Defining a variable is called … definition. Giving a variable a value is called assignment. Initialization merges these two instructions into one statement. E.g. int x { 11 }; This is…

#009 – How to use comments

I’m a big advocate of comments. It conveys the programmer’s intent, which is very useful if you’re interpreting someone else’s code. Comments should explain intent Code shows what the program does. Comments should explain the intent behind the code. For…

#008 – Choosing your language standard

C++ has multiple language standards: C++98, C++03, C++11, C++14, C++17, C++20, C++23, and newer standards in progress. When you configure a compiler, you are not just choosing “C++.” You are choosing which version of the language the compiler should accept.…

#005 – How do we develop programs? Part 2

Here’s the flowchart from the previous nibble, we let off from Step 4. This Nibble continues from compilation and follows the program through linking, building, testing, and debugging. Three things to take away: Step 4 – Compile the program The…