The History of ‘Hello, World!’ program

If you ever stumbled on any programming language tutorial regardless of your background, you certainly have already seen the “Hello, World!” written somewhere in the tutorials. It is considered a standard program for introducing a programming language to the students, wherein it simply prints “Hello, World!” to the console screen. It was first introduced in the introductory book on C: The C Programming Language which was written by Brian Kernighan and Dennis Ritchie. It was written as “hello, world” (without capital letters or exclamation mark) in the book. As the book was considered the best introduction to the programming language (C; which was hugely popular in the era), since then it is used as the first program to introduce the basic details of a programming language whenever a programming language is being taught to students.

The above program was written for the original version of C, which was translated from a program written in A Tutorial Introduction to the Language B By Kernighan for illustrating external variables:

In modern C, the program can be written as follows:

Due to the rise in popularity of the “Hello, World!” program, many modern programming languages incorporate the program in a similar fashion, but with the addition of the sophistication or the style of the language. For instance, in Python you can write the program in a single line:

Overall, the “Hello, World!” program introduced a topic of interest to the programming enthusiasts who like to figure “everything” out.

How to sleep the CPU through cross-platform sleep function in C/C++

If you need your CPU to sleep for a while; that is, wait for some time without any activity, then you need to call system functions in your program. For instance, if you want to show some text for 2 seconds, then you can sleep the CPU to simulate the effect.

Unfortunately, there is no cross-platform way to sleep the CPU; Windows provide sleep() function in <windows.h> header, which takes milliseconds in an argument, while Linux provides sleep() in <stdlib.h> function which takes seconds and usleep() which takes milliseconds in an argument.

To overcome this cross-platform problem, we will use macros to detect whether the code is compiling under Windows or Linux. Following is its implementation:

Now, you can call SLEEP with an argument of milliseconds for sleeping the CPU whether you are on Windows or Linux. Apart from that, you can also use rlutil::msleep() from rlutil library to get cross-platform sleep function.

rlutil – a cross-platform library to change console text color in C/C++

Dissecting the rlutil library to colorize the text.
Last month ago, while I was searching for cross-platform ways to change console text color in C++, something in the fashion of vim text editor, then I found a very good open-source library, called rlutil, which can be used to change console text color in console-based applications (vocabulary builders, text editors like vim, etc.) and for easing the creation of roguelike games.
It is cross-platform library, so your code can run on both Windows and Linux, and the effect will be the same; that is, your text will be colorized!

rlutil is a single header library and hosted on Github, so you can easily clone the source codes in your hard drive. I found the API of rlutil very easy to use; you do not need to compile the library; it is a single header file library, so just include the rlutil.h in your directory of source file and call simple functions to change the color of your text!

In this post, I will show you how you can change console text color through rlutil to add fun to your boring console programs. Let’s get started!

rlutil – a cross-platform library to change console text color in C/C++

Create a new folder “rlutil-test” and within the folder, create a new file “main.cpp” (the rlutil.h should be in the same directory as your main.cpp file so copy the file if you haven’t yet)

main.cpp

Assuming the file is saved as main.cpp , you can easily compile and run the program using g++ as follows:

My linux console shows the following:

rlutil example

You can see how easy it is to change the color of console text using rlutil library!

I have tested the code on both Linux and Windows and the result was the same. Note that if you are compiling through C compiler, then the library will automatically remove the namespaces, so you won’t need to write rlutil:: when using the functions.

Apart from functions for changing the color of console text, the library also has many utility functions such as rlutil::cls() for clearing the console screen, rlutil::anykey() to check if a user presses a key, rlutil::hidecursor() and rlutil::showcursor() for hiding and showing the cursor respectively, rlutil::msleep() for sleeping the CPU in a cross-platform way, rlutil::getkey() for reading the pressed keys, etc.

What do you think of this library? Does it provide a better API to change the console text color for console-based programs than the libraries you use?

Range-based For Loops in C++11 – Basics and its usage on STL containers

Trying to understand the power of range-based for loops in C++11.
Range-based for loops is arguably one of the most powerful features of C++11. It introduces a shorter syntax for iterating through the elements of a container (C-arrays, std::vector, std::list, std::map, etc.). This feature was already included for many years in other similar-to-C++ static typed languages, such as, Java and C#, etc. and now it has found its place in C++…. which is great, because C++ programmers were tired of using the (very) old syntax for iterating through the elements of a container.
In this post, I’m going to show you its usage and how it makes the syntax easier to read and makes your code look modern.

Range-based For Loops in C++11 – Basics and its usage on STL containers

Below is its usage on STL containers:

You can compile it with g++ and run the program as follows (assuming the file is saved as main.cpp):

Running the program prints following to the console:

It executes a loop over a range. Here, we are inferring the type of elements in our “number” container (int in this case) using “auto“. Its equivalent in the previous versions of C++ (i.e., C++03) would look like the following:

Or using iterators, it would look like the following:

It can be used to iterate through the elements in C-arrays as well.

The same syntax can be used for similar-to-vector containers like std::list, std::deque, etc.

Changing the values in a container

You can use the reference to change the values in a container:

Using range-based for loop with std::map

You can use range-based for loops with std::map as well.

It is a good practice to make the parameter in the loop a reference for efficiency. You could also consider making it const if you want a read-only view of the values.

How to use lambda expression in std::find_if (C++)

Lambda function / expression is a nice feature of C++11. It enables a programmer — which means you — to define and use a function inline; that is, you don’t need to define your function in outer scope.

This feature greatly reduces the need to redundantly define functions; you can define a function in a place where you want to, in the same scope!

In this post, I will tell you how you can use its amazing power to find elements in an STL container (std::vector, std::list, etc.) according to some rules which would be set through lambda functions. Below is the example of “IsOdd” logic, which is originally found here:

Here, you can see that I’ve used a normal function to define a logic or criteria for finding elements in a container. We will now use lambda expression instead:

Both of the above programs do the same job, however, in the second program, we didn’t have to define a function of such a simple logic outside the main scope, thus shortening our code.

Conclusion

In this tutorial, I have showed you the ways to use lambda expression with std::find_if function for manipulating STL containers.
Do you have any questions regarding this tutorial? Tell me in the comments section! Also like our page on Facebook.