C++ Basic Syntax - GeeksforGeeks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

12/4/24, 11:08 AM C++ Basic Syntax - GeeksforGeeks

C++ Basic Syntax


Last Updated : 25 Nov, 2024

Syntax refers to the rules and regulations for writing statements in a


programming language. They can also be viewed as the grammatical
rules defining the structure of a programming language.

The C++ language also has its syntax for the functionalities it provides.
Different statements have different syntax specifying their usage, but
C++ programs also have basic syntax rules that are followed
throughout all the programs.

Basic Syntax of a C++ Program


We can learn about basic C++ Syntax using the following program.

C++

1 // C++ program to demonstrate the basic syntax


2 // Header File Library
3 #include <iostream>
4
5 // Standard Namespace
6 using namespace std;
7
8 // Main Function
9 int main()
10 {
11
12 // Body of the Function
13
14 // Declaration of Variable
15 int num1 = 24;
16 int num2 = 34;
17
18 int result = num1 + num2;
19

https://www.geeksforgeeks.org/cpp-basic-syntax/ 1/10
12/4/24, 11:08 AM C++ Basic Syntax - GeeksforGeeks

// Output
21 cout << result << endl;
22
23 // Return Statement
24 return 0;
25 }

Output

58

The program above shows the basic C++ program that contains header
files, main function, namespace declaration, etc. Let’s try to understand
them one by one.

1. Header File

The header files contain the definition of the functions and macros we
are using in our program. In line #1, we used the #include <iostream>
statement to tell the compiler to include an iostream header file library
which stores the definition of the cin and cout standard input/output
streams that we have used for input and output. #include is a
preprocessor directive using which we import header files.

2. Namespace

A namespace in C++ is used to provide a scope or a region where we


define identifiers. In line #2, we have used the using namespace std
statement for specifying that we will be the standard namespace where
all the standard library functions are defined.

3. Main Function

In line #3, we defined the main function as int main(). The main
function is the most important part of any C++ program. The program
execution always starts from the main function. All the other functions
https://www.geeksforgeeks.org/cpp-basic-syntax/ 2/10
12/4/24, 11:08 AM C++ Basic Syntax - GeeksforGeeks

are called from the main function. In C++, the main function is required
to return some value indicating the execution status.

4. Blocks

Blocks are the group of statements that are enclosed within { } braces.
The body of the main function is from line #4 to line #9 enclosed within
{ }.

5. Semicolons

As you may have noticed by now, each statement in the above code is
followed by a ( ; ) semicolon symbol. It is used to terminate each line of
the statement of the program.

6. Identifiers

We use identifiers for the naming of variables, functions, and other


user-defined data types. An identifier may consist of uppercase and
lowercase alphabetical characters, underscore, and digits. The first
letter must be an underscore or an alphabet.

7. Keywords

In the C++ programming language, there are some reserved words that
are used for some special meaning in the C++ program. It can’t be used
for identifiers. For example, the words int, return, and using are some
keywords used in our program.

8. Basic Output cout

In line #7, we have used the cout stream object (declared in the
<iostream> header file) to print the sum of two numbers to the standard
output stream (stdout).

Object-Oriented Programming in C++

https://www.geeksforgeeks.org/cpp-basic-syntax/ 3/10
12/4/24, 11:08 AM C++ Basic Syntax - GeeksforGeeks

C++ programming language supports both procedural-oriented and


object-oriented programming. The above example is based on the
procedural-oriented programming paradigm. So let’s take another
example to discuss Object Oriented Programming in C++.

C++

1 #include <iostream>
2 using namespace std;
3
4 class Calculate{
5
6 // Access Modifiers
7 public:
8 // data member
9 int num1 = 50;
10 int num2 = 30;
11
12 // member function
13 int addition() {
14 int result = num1 + num2;
15 cout << result << endl;
16 }
17 };
18
19 int main() {
20
21 // object declaration
Courses @35%22Off C++Calculate
Data Types add;
C++ Input/Output C++ Arrays C++ Pointers C++ OOPs C++ ST
23 // member function calling
24 add.addition();
25
26 return 0;
27 }

Output

80

1. Class
https://www.geeksforgeeks.org/cpp-basic-syntax/ 4/10
12/4/24, 11:08 AM C++ Basic Syntax - GeeksforGeeks

A class is a user-defined data type. A class has its own attributes (data
members) and behavior (member functions). In line #3, we have
declared a class named Calculate and its body expands from line #3 to
line #7.

2. Data Members & Member Functions

The attributes or data in the class are defined by the data members &
the functions that work on these data members are called the member
functions.

In the above example, num1 and num2 are the data member &
addition() is a member function that is working on these two data
members. There is a keyword here public that is access modifiers. The
access modifier decides who has access to these data members &
member functions.

3. Object

The object is an instance of a class. The class itself is just a template


that is not allocated any memory. To use the data and methods defined
in the class, we have to create an object of that class.

BLACK FRIDAY OFFER! Subscribe for 1 Year and get 1 Extra year of
access completely FREE! Upgrade to GeeksforGeeks Premium today!

Choose GeeksforGeeks Premium and also get access to 50+ Courses


with Certifications, Unlimited Article Summarization, 100% Ad free
environment, A.I. Bot support in all coding problems, and much more.
Go Premium!

Comment More info Next Article


C++ Comments

https://www.geeksforgeeks.org/cpp-basic-syntax/ 5/10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy