Getting Started with C++

C++ is a compiled language hence it requires a compiler. This can be achieved in 2 ways:-

  • Online Integrated Development Environment (IDE):- Online IDE can be accessed from a web browser, such websites allow us to write codes in various language and to compile it, shows the errors in the code (if any), runs the code, and gives the output for the respective input. Some of the popular IDE’s are:- Ideone, codepad, C++ Shell, etc.

  • C++ Compiler:- A Compiler converts the high level language into Machine language which can be understood by the Computer. C++ compilers can be downloaded on most Operating Systems including Windows, macOS, Linux, etc. Code::Blocks, Dev C++, and Borland are some of the most common C++ compilers.

Below is a basic C++ code that prints Hello World.

Source Code:-

Output:- Hello World

The code contains:-

1. Header Files:- Header files are predefined functions in C++. These functions are called by containing them in #include< > and they’ve a .h extension. We need this line in order to use std::cout. Excluding this file would result in a compile error, as the compiler wouldn’t otherwise know what std::cout is. There are 2 types of header files:-

      a) Pre defined header files:- These files are already in the C++ library and are just needed to be called.

      b) User defined header files:- These header files are created by users themselves and can be called by any other file.

2. Main function:- It is the first function that is executed by the compiler.

3. Print Function:- The std is the buffer stream, hence it is used for input buffer by std::cin and output buffer by std::cout. std::cout (which stands for “character output”) and the << operator allow us to send letters or numbers to the console to be output. In this case, we’re sending it the text “Hello world!”, which will be output to the console.

4. Semicolon:- Every line of code is to be terminated by a semicolon else the line won’t terminate and it'll be considered as one single line of code until a semicolon is reached.

5. Return:- Every function has a return value. If its an int function then it should return an integer, if it's void then it doesn't have a return type, and so on.when the program finishes running, the program needs to send a value back to the operating system in order to indicate that the program has executed. In this case, the main function will return 0 to the operating system which means “everything went okay!”

Another way of calling an output stream is by declaring a file using namespace std:-

Using namespace std:- Namespace tells us a scope and the particular function followed by namespace is called. std function tells us the input-output stream, hence we don’t have to call std before cin or cout.

Lets look at another example


Enter Your Name :- Raj
Hello Raj

std::cin is another predefined variable that is defined in the iostream library. Whereas std::cout prints data to the console using the insertion operator (<<), std::cin (which stands for “character input”) reads input from keyboard using the extraction operator (>>). The input must be stored in a variable to be used.
We use cin function followed by 2 left operators to input a particular variable. Such variables are generally mutable, which means they can be changed or overridden after being assigned once.

Now we have looked at several ways to input and output in C++. Try some yourself for better understanding.

Happy Coding 😊

By Programmers Army

Contributed by: Tanmay Garg