Your first C++ program

Now you have installed the compiler based on your OS, it’s time to write your first C++ program.

“Hello World!”

Your first C++ program will be a “Hello World!” program.

You might have noticed “Hello World!” being the first program while starting out with any programming language. This is because:

  • It is a standard check to see whether everything is working fine or not.
  • There will be very less code to start with.
  • The less code makes it intuitive for the beginners to get acquainted with the language.
  • The code is enough to learn the basic syntax and semantics of the language.

So, let’s get coding.

#include<iostream>

usingnamespacestd;

int main()

{

cout<<“Hello World!”;

return0;

}

The program prints Hello World! in the output screen.

How the program works?

Now, let’s dissect the above code. The code is divided into six major parts:

  • #include <iostream>
  • using namespace std
  • ;
  • int main() { }
  • cout<< “Hello World!”;
  • return 0;

1.      What is #include <iostream>?

If you’ve already written code in C language before, you might seen this line of code before. If you haven’t, don’t worry we’ll cover it now.

This statement includes the header file into the application so that you are able to use the operations included in them. Also, you can create your own header files and include them in your program using the #include.

What is iostream?

iostream is what you call the header file. It is a standard C++ input/output library file.
It comes packaged with the compiler/IDE and contain mechanisms to get the information from the user and print same or added information to a file, screen or any other media.

What is #include?

The #include iostream file, into the program. This ensures that now you’re able to use the operations, iostream operations (like: taking input from user, displaying output on the screen), in the program.

2.      What is using namespace std;”?

The statement is intuitive in itself, you are “using” the “namespace” “std” in your file.
We use the namespace std to make it easier to reference operations included in that namespace.
If we hadn’t used the namespace, we’d have written std::cout instead of cout. This tells the compiler that every cout is actually std::cout.

What’s a namespace?

It’s a region where your code resides. It limits or expands the scope of your code to one or more files.

Why do you use namespace?

Like two persons can have the same name, variables and functions in C++ can have same names as well. The use of namespace is to avoid the confusion of which variables/functions you are referencing to.

What is std?

std is a standard namespace used in C++.

3.      Semicolon ”;”

Ask any C++ programmer and they will tell you at least one horror story related to the semicolon ; .

The semicolon is a terminal. It terminates a statement. When missed or incorrectly used, it will cause a lot of issues.

4.      int main() { }

As the name suggests, it is the main function of the program. The code inside { } is called the body and is executed first when you run your C++ program.

It is one code that is mandatory in a C++ program. If you just have this line of code alone, your program will be valid.

5.      cout<< “Hello World!”;

This statement prints “Hello World!” onto the output screen.

The cout is an object of standard output stream. What this means is, it outputs/prints the data after << , i.e. Hello World! into a stream (in this case, the output screen).

What is a stream?

Stream is basically a sequence of objects, usually bytes. It can describe files, input/output terminal, sockets, etc.
What is <<?

<< is the insertion operator used to write formatted data into the stream.

6.      What is return 0;?

This statement returns 0 ‘zero’.

This is called a return statement. It isn’t mandatory to return anything from the main() function but is rather a convention. If not return, the compiler returns a status automatically.

Why zero in return statement?

It denotes Exit status of the application that basically the tells system “The program worked fine.”

Related Posts

© 2024 Software Engineering - Theme by WPEnjoy · Powered by WordPress