Tokens in C++
In C++, a token is the smallest unit of a program that has a meaningful representation. Tokens are fundamental building blocks of a C++ program and are classified into several types. Understanding tokens is crucial for parsing, compiling, and interpreting C++ code. The main types of tokens in C++ are keywords, identifiers, and constants.
1. Keywords
Keywords are reserved words in C++ that have special meanings and cannot be used for any other purpose. They define the syntax and structure of the language. Each keyword represents a specific feature or function of the language.
Examples of Keywords:
- `int`: Specifies an integer type.
- `float`: Specifies a floating-point type.
- `if`: Used for conditional statements.
- `while`: Used for loop statements.
- `class`: Defines a class.
Example in Code:
#include <iostream>
using namespace std;
int main() {
int number = 10; // 'int' is a keyword
if (number > 0) { // 'if' is a keyword
cout << "Number is positive." << endl;
}
return 0;
}
In this example:
- `int` and `if` are keywords with specific functions in C++.
2. Identifiers
Identifiers are names given to various program elements such as variables, functions, classes, and objects. They are used to identify and access these elements in the code. Identifiers must follow specific rules:
- They must start with a letter (A-Z or a-z) or an underscore (_).
- They can be followed by letters, digits (0-9), or underscores.
- They cannot be the same as C++ keywords.
Examples of Identifiers:
- `number`: A variable name.
- `main`: The name of the main function.
- `Person`: A class name.
Example in Code:
#include <iostream>
using namespace std;
class Person { // 'Person' is an identifier
public:
string name; // 'name' is an identifier
void greet() { // 'greet' is an identifier
cout << "Hello, my name is " << name << endl;
}
};
int main() {
Person p; // 'Person' is an identifier, 'p' is an identifier
p.name = "Alice"; // 'name' is an identifier
p.greet(); // 'greet' is an identifier
return 0;
}
In this example:
- `Person`, `name`, and `greet` are identifiers representing a class, a member variable, and a member function, respectively.
3. Constants
Constants are literal values that do not change during the execution of the program. They represent fixed values used directly in the code. Constants can be of various types, such as integer constants, floating-point constants, character constants, and string constants.
Examples of Constants:
- Integer Constants: `10`, `-5`, `1000`
- Floating-Point Constants: `3.14`, `-0.001`, `2.71`
- Character Constants: `'a'`, `'1'`, `'$'`
- String Constants: `"Hello, World!"`, `"C++ Programming"`
Example in Code:
#include <iostream>
using namespace std;
int main() {
const int DAYS_IN_WEEK = 7; // 'DAYS_IN_WEEK' is a constant integer
const double PI = 3.14159; // 'PI' is a constant floating-point number
const char GRADE = 'A'; // 'GRADE' is a constant character
const string MESSAGE = "Hello, World!"; // 'MESSAGE' is a constant string
cout << "Days in a week: " << DAYS_IN_WEEK << endl;
cout << "Value of PI: " << PI << endl;
cout << "Grade: " << GRADE << endl;
cout << "Message: " << MESSAGE << endl;
return 0;
}
In this example:
- `DAYS_IN_WEEK`, `PI`, `GRADE`, and `MESSAGE` are constants with specific values.
Summary
- Keywords: Reserved words that define the syntax and structure of C++.
- Identifiers: Names used for variables, functions, classes, and other elements in a program.
- Constants: Fixed values used directly in the code, representing literals that do not change during execution.
Understanding these tokens is fundamental for writing and interpreting C++ code, as they define the basic elements and structure of the language.
No comments:
Post a Comment