Browse Tutorials
First C++ Program and Its Syntax

First C++ Program and Its Syntax

10 Feb 2024
Beginner
801 Views
9 min read
Learn via Video Course & by Doing Hands-on Labs

C++ Programming For Beginners

First C++ Program and its Syntax: An Overview

Are you excited to begin coding in C++? But, don't know where and how to start? Don't worry. All your questions will be answered in this article today. You will understand how to write a C++ program in a step-by-step manner. If you want to deepen your understanding of C++ programming skills, consider enrolling in our C++ Certification. In this C++ Tutorial, we'll learn the syntax and the meaning of each word or symbol in the C++ program.

Begin your 1st C++ Program

  • Open any text editor or IDE and create a new file with any name with a .cpp extension. e.g. helloworld.cpp
  • Open the file and enter the below code:
    
    #include <iostream>
    using namespace std;
    int main() {
     cout << "Hello, World!" << endl;
     return 0;
    } 
  • Compile and run the code

Output

Hello, World!

Read More - C++ Interview Interview Questions for Experienced

Syntax of C++

In the above section, you tried the very basic program of “Hello World” in C++. Did you understand the terms like iostream, cout written in the code? Don’t worry let us understand it one by one:
#include <iostream>
using namespace std;
int main() {
 cout << "Hello, World!" << endl;
 return 0;
} 
  • In the very first line, #include signifies the header file. This header file is a library of C++ that helps the programmer with standard input-output objects.
  • In the next line, there is using namespace std; statement. It denotes that we can use names for objects and variables from the standard library. The cout object is defined inside the std namespace.
  • Then comes the main() function. It is the function due to which the program executes. Any C++ program without a main() function cannot run. Only the code written within {} executes during run time.
  • cout is an object that prints the string inside quotation marks " ". It is followed by the insertion operator, <<.
  • The using namespace std; line can be omitted and replaced with the std keyword, followed by the : : operator for some objects as shown below
    
    #include <iostream>
    int main() {
     std::cout << "Hello, World!" << std::endl;
     return 0;
    } 
    

    This is more preferred than using the statement, using namespace std; because it can create potential problems.

  • With the help of cout, our program printed the sentence “hello world” on the screen. There can be as many cout statements as many you want in your program.
    
    #include <iostream>
    using namespace std;
    int main() {
     cout << "Hello, World!" << endl;
     cout << "This is my first program" << endl;
     return 0;
    } 

    Output

    Hello, World!
    This is my first program
    
  • The endl manipulator is used to insert a new line. That's why each output is displayed in a new line.
  • The << operator can be used more than once if we want to print different variables, strings, and so on in a single statement.
  • return 0 indicates the end of the main() function.
  • The closing curly bracket }is used to end the main() function.
  • Have you noticed ; after each line of code in the main() function? It is necessary to put it after each line of code. In C++ every line ends with a ;. If you forget to put a ;, it will show you a compile-time error.
  • C++ ignores white spaces but we use it to make the code more readable.

User Input in C++

In C++, cin takes formatted input from standard input devices such as the keyboard. The cin object is used along with the extraction operator, >> for taking input.

Example of User Input in C++


#include <iostream>
using namespace std;

int main() {
 int a;
 cout << "Enter a number: ";
 cin >> a; // Taking input
 cout << "The number is: " << a;
 return 0;
}

The above C++ code uses cin to take an integer input from the user. The input is stored in the variable a.

You will see variables and data types in the upcoming tutorials, Variables in C++ Programming and Data Types in C++

Output

Enter a number: 85
The number is: 85
  • You can even use std::cin instead of cin if you don't want to use the statement, using namespace std;.
    
    #include <iostream>
    int main() {
     int a;
     std::cout << "Enter a number: ";
     std::cin >> a; // Taking input
     std::cout << "The number is: " << a;
     return 0;
    } 
  • You can even take multiple inputs using a single cin statement
    
    #include <iostream>
    int main() {
     int a, b;
     std::cout << "Enter two numbers: ";
     std::cin >> a >> b; // Taking inputs
     std::cout << "The numbers are: " << a << "," << b;
     return 0;
    }
    
  • Output

    Enter two numbers: 10 20
    The numbers are: 10, 20
    
Summary
After getting through this article you must have at least got an idea to write a basic C++ program. You'll learn all the concepts of coding in C++ in the upcoming tutorials. Just follow the tutorial in a sequence. To become a certified C++ programmer, consider enrolling in our C++ Certification Program.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this