In C++, a program is made up of small building blocks known as tokens. These are the smallest individual units that the compiler understands while processing a program.
Every statement in a C++ program is formed using different types of tokens. Understanding tokens is essential because they form the foundation of programming syntax.
C++ tokens are mainly divided into the following categories:
In C++, keywords are a set of reserved words that have predefined meanings assigned by the language. These words are an essential part of the syntax and are used by the compiler to understand the structure and behavior of a program.
Since keywords are already defined by the C++ language, they cannot be used as identifiers such as variable names, function names, or class names. Attempting to use a keyword for any other purpose will result in a compilation error.
Here are some frequently used keywords in C++:
int float double char if else switch case for while do break continue return void const class public private protected
C++ keywords can be grouped based on their functionality:
#include <iostream>
using namespace std;
int main() {
int number = 10;
if (number > 0) {
cout << "Positive number";
} else {
cout << "Negative number";
}
return 0;
}
In the above program, words like int, if, else, return are keywords. Each one has a fixed role that helps define how the program behaves.
int is different from INT).Overall, keywords act as the building blocks of C++ syntax, allowing programmers to write meaningful and structured programs efficiently.
In C++, identifiers are the names assigned by the programmer to different elements of a program. These elements may include variables, functions, arrays, classes, objects, or any other user-defined items. Identifiers help make a program readable and meaningful by giving recognizable names to data and operations.
Instead of using memory addresses or complex representations, programmers use identifiers to refer to data easily. For example, a variable storing marks can be named marks, making the code more understandable.
C++ follows strict rules for defining identifiers. These rules must be followed to avoid errors:
value and Value are different).int marks; float totalScore; double average_value; char grade;
int 1marks; // Starts with a number float total-score; // Contains special character (-) int if; // Keyword used as identifier char student name; // Contains space
Although not enforced by the compiler, following good naming practices is highly recommended:
totalMarks instead of tm).MAX_SIZE).
#include <iostream>
using namespace std;
int main() {
int marks = 85;
float totalScore = 100.0;
float percentage = (marks / totalScore) * 100;
cout << "Percentage: " << percentage;
return 0;
}
In this example, marks, totalScore, and percentage are identifiers that store values and make the program easy to understand.
In summary, identifiers act as labels for different components in a C++ program, making it easier for programmers to write, read, and manage code effectively.
In C++, constants are values that remain fixed throughout the execution of a program. Unlike variables, once a constant is defined, its value cannot be modified or reassigned. Constants are useful when you need to store values that should stay unchanged, such as mathematical values, fixed limits, or configuration data.
C++ supports different types of constants based on the kind of data they represent:
Constants are commonly defined using the const keyword. Once declared, their value cannot be changed later in the program.
const int x = 10; const float PI = 3.14; const char grade = 'A';
#include <iostream>
using namespace std;
int main() {
const float PI = 3.14;
float radius = 5;
float area = PI * radius * radius;
cout << "Area of circle: " << area;
return 0;
}
In this example, PI is a constant whose value remains unchanged throughout the program.
MAX_VALUE).In summary, constants provide a safe way to store fixed values in a program, ensuring that important data remains unchanged and the code stays reliable.
In C++, a string is a sequence of characters enclosed within double quotes (" "). Strings are mainly used to store and display text such as names, messages, and sentences in a program.
Each character in a string is stored in a continuous memory location, making it easy to handle and manipulate text data in programs.
"Hello World" "Welcome to C++" "Programming is fun"
Strings can be used directly in output statements or stored in variables using the string data type (from the standard library).
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string message = "Welcome to C++";
cout << message;
return 0;
}
" ").<string> library allows advanced string operations.In short, strings make it easy to work with text in C++ programs, whether it's displaying messages or handling user input.
In C++, operators are special symbols used to perform operations on variables and values. They allow programmers to manipulate data, perform calculations, and make logical decisions within a program.
Operators work with operands (values or variables) to produce a result. For example, in the expression a + b, + is the operator, while a and b are operands.
C++ provides different types of operators based on the kind of operations they perform:
+, -, *, /, %==, !=, >, <, >=, <=&& (AND), || (OR), ! (NOT)=, +=, -=, *=, /=
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
int sum = a + b; // Arithmetic operator
bool result = (a > b); // Relational operator
cout << "Sum: " << sum << endl;
cout << "Is a greater than b? " << result;
return 0;
}
In summary, operators are the building blocks of expressions in C++, allowing you to perform everything from simple calculations to complex logical operations.
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int sum = a + b;
cout << "Sum = " << sum;
return 0;
}
In this program:
Tokens are the fundamental elements of a C++ program. Every line of code is built using different types of tokens such as keywords, identifiers, constants, strings, and operators. A clear understanding of tokens helps beginners write correct and efficient programs.