C++ Identifiers
Identifiers in C++ are used to identify variables, functions, classes,
objects, arrays, etc. An identifier in C++ must follow certain rules:
Rules for Naming Identifiers
1. Alphanumeric Characters: Identifiers can consist of letters
(both uppercase and lowercase), digits, and underscores (_).
However, it must start with either a letter or an underscore. It
cannot start with a digit.
2. First Character: The first character must be a letter or an
underscore. Identifiers cannot start with a digit.
3. Case Sensitivity: Identifiers are case-sensitive,
meaning Variable and variable are considered different identifiers.
4. Reserved Keywords: Identifiers cannot be keywords or reserved
words in C++. (such as int, float, if, else, while, etc.).
5. Length: Identifiers can be of any length, but it is a good practice to
keep them reasonably short and meaningful.
Examples of Valid Identifiers
Examples of Invalid Identifiers
Best Practices for Naming Identifiers
1. Descriptive Names: Use meaningful and descriptive names that
convey the purpose of the identifier.
2. Camel Case: For variables and functions, use camel case
(e.g., myVariable, calculateSum).
3. Uppercase for Constants: Use uppercase letters for constants
(e.g., MAX_VALUE, PI).
4. Avoid Single Letters: Avoid using single letters except for loop
counters (e.g., i, j)
C++ Operators
Arithmetic Operators
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
% : Modulus
Relational Operators
== : Equal to
!= : Not equal to
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Less than or equal to
Logical Operators
&& : Logical AND
|| : Logical OR
! : Logical NOT
Bitwise Operators
& : Bitwise AND
| : Bitwise OR
^ : Bitwise XOR
~ : Bitwise NOT
<< : Left shift
>> : Right shift
Assignment Operators
= : Assign
+= : Add and assign
-= : Subtract and assign
*= : Multiply and assign
/= : Divide and assign
%= : Modulus and assign
&= : Bitwise AND and assign
|= : Bitwise OR and assign
^= : Bitwise XOR and assign
<<= : Left shift and assign
>>= : Right shift and assign
Unary Operators
+ : Unary plus
- : Unary minus
++ : Increment
-- : Decrement
! : Logical NOT
~ : Bitwise NOT
Ternary Operator
?: : Ternary conditional
Other Operators
sizeof : Returns the size of a variable or type
& : Address of operator
* : Pointer dereference
-> : Member access through pointer
. : Member access
, : Comma operator
:: : Scope resolution
new : Dynamic memory allocation
delete : Dynamic memory deallocation
typeid : Returns the type information
decltype : Inspects the declared type of an entity
static_cast : Compile-time cast
dynamic_cast : Runtime cast (for polymorphic types)
const_cast : Casts away constness
reinterpret_cast : Casts between unrelated types