C++ is a powerful programming language that supports both structured and object-oriented programming. It is a superset of C, which means that most C constructs are valid in C++, though there are some differences and additions.
Tokens in C++
When a C++ program is compiled, the source code is broken down into tokens. Tokens are the smallest units in a program, and a C++ program is written using these tokens. There are several types of tokens in C++:
- Keywords
- Identifiers
- Constants
- Strings
- Operators
Keywords in C++
Keywords, also known as reserved words, have special meanings in C++ and are always written in lowercase. These words cannot be used as variable names, function names, or any other identifiers. There are 95 reserved keywords in C++. Here are some commonly used keywords:
Keyword | Description |
---|---|
asm | Declares a block of code to be passed to the assembler |
auto | Specifies the storage class of a variable |
break | Terminates a loop or switch statement |
case | Specifies a match in a switch statement |
catch | Handles exceptions in a try block |
char | Defines a character data type |
class | Declares a class |
const | Defines a constant variable |
continue | Skips the current iteration of a loop |
default | Handles unmatched cases in a switch statement |
delete | Deallocates memory |
do | Starts a do-while loop |
double | Defines a double-precision floating-point number |
else | Specifies an alternative condition in an if statement |
enum | Declares an enumeration type |
extern | Specifies external linkage for a variable or function |
float | Defines a floating-point number |
for | Starts a for loop |
friend | Allows access to private members of a class |
goto | Transfers control to a labeled statement |
if | Starts an if statement |
inline | Suggests inlining a function |
int | Defines an integer data type |
long | Modifies the size of an integer |
new | Allocates memory |
operator | Overloads operators |
private | Specifies private access in a class |
protected | Specifies protected access in a class |
public | Specifies public access in a class |
register | Specifies register storage for a variable |
return | Returns a value from a function |
short | Modifies the size of an integer |
signed | Specifies a signed data type |
sizeof | Returns the size of a data type |
static | Specifies static storage duration |
struct | Declares a structure |
switch | Starts a switch statement |
template | Declares a template |
this | Refers to the current object |
throw | Throws an exception |
try | Starts a try block for exception handling |
typedef | Creates a type alias |
union | Declares a union |
unsigned | Specifies an unsigned data type |
virtual | Declares a virtual function in a class |
void | Specifies an empty data type |
volatile | Prevents compiler optimization of a variable |
while | Starts a while loop |
Identifiers in C++
Identifiers are names given to variables, functions, arrays, classes, etc. They are essential in any programming language. Here are the rules for naming identifiers in C++:
- An identifier cannot start with a digit or special character.
- Keywords cannot be used as identifiers.
- Identifiers can only contain alphabetic characters, digits, and underscores.
- Uppercase and lowercase letters are distinct (e.g., A and a are different).
Examples of valid identifiers: GFG
, gfg
,
Learning_of_Cpp
.
Examples of Good and Bad Identifiers
Invalid Identifier | Bad Identifier | Good Identifier |
---|---|---|
Cash prize | C_prize | cashprize |
catch | catch_1 | catch1 |
1list | list_1 | list1 |
Example Code
Here's an example of a simple C++ program using identifiers:
#include <iostream> using namespace std; int main() { int Learning_of_Cpp = 1; cout << "Identifier result is: " << Learning_of_Cpp;
return 0; }
Output:
Identifier result is: 1
How Keywords Differ from Identifiers
Keywords and identifiers have distinct properties:
Keywords | Identifiers |
---|---|
Predefined or reserved words | Names defined by the programmer |
Always lowercase | Can start with uppercase or lowercase |
Define the type of entity | Define the name of the entity |
Only contain alphabetical characters | Can contain letters, digits, and underscores |
Cannot be used as variable names | Can be both uppercase and lowercase |
Example Code with Keywords
#include <iostream> using namespace std; int main() { int n = 2; switch (n) { case 1: cout << "Computer Network" << endl; break; case 2: cout << "C++" << endl; break; case 3: cout << "DBMS" << endl; break; case 4: cout << "Data Structure" << endl; break; case 5: cout << "Operating System" << endl; break; default: cout << "Enter Valid number" << endl; } return 0; }
Output:
C++
Tips for Using Keywords and Identifiers
- Avoid using reserved keywords as variable or function names.
- Use meaningful identifiers to make your code easier to read and understand.
- Follow naming conventions like using camelCase or underscores to separate words in identifiers.
- Keep identifiers short but descriptive to balance readability and conciseness.
By understanding and properly using keywords and identifiers, you can write clear and efficient C++ programs.