C++ Tokens
C++ is a language the computer understands, now what is the smallest fundamental unit of any language? Let's consider an example, English consists of words, punctuations, and grammar. Now words are further divided into nouns, pronouns, verbs, and so on.
Similarly, a token is a fundamental unit of C++ programming language, think of tokens as words.
In C++ there are 5 main tokens:-
Keywords
Keywords are pre-defined or reserved words in a programming language. Each Keyword has a reserved meaning. This means each keyword is like an instruction to the computer to perform a specific task. Consider the following example.
C++ automatically highlights the keywords you use in your code. In the above example include,using, namespace, int and return are all keywords.
All primitive data types are keywords. Loops, like for, while,, do while, and selection statements like if and else if are also keywords.
Identifiers
Identifiers are user-defined names which consist of a sequence of letter, digits and the underscore character.
Certain rules are to be followed while naming identifiers in C++
- The first character should always be a letter or an underscore.
- They must consist of only letters, digits, or underscore. No other special character is allowed.
- It should not be a keyword.
- The length of the identifier should be less than 31 characters. If an identifier-name exceeds this length, then only the first 31 significant characters are considered.
Example of valid identifiers:
Examples of invalid identifiers:
Constants
Constants refer to fixed values that are not altered throughout the entire program. These are declared using the keyword const .
Special Symbols
Special symbols like [] () {}, ; * = # have special meaning and they cannot be used for other purposes. Let's find out what they mean.
- Brackets [] : These are used as array element reference.
- Parenthesis () : These are used to indicate function calls and function parameters.
- Braces {}: The opening and ending curly braces mark the start and end of a block of code containing more than one executable statement.
- Comma (,): It is used to separate more than one statement like for separating parameters in function calls or elements in an array.
- Colon (:):It is an operator that essentially invokes something called an initialization list.
- Semicolon (;): It is known as a statement terminator. It indicates the end of one logical entity.
- Asterisk () : It is used to create a pointer variable.*
- Assignment operator (=) :It is used to assign values.
- Pre-processor (#): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before the actual compilation.
Consider this line of code
Operators
Operators are symbols that when applied to C++ variables trigger an operation. The variables on which operators act upon are called operands. Operators can be divided into two broad categories based on the number of Binary and Unary operators.
Unary operators
Operators that act on a single operand are called Unary operators. These include increment and decrement operators.
Binary operators
Operators that require two operands to act upon are called binary operators.
Binary operators are classified into :
Arithmetic operators:
- + addition
- - subtraction
- * multiplication
- / division
- % modulus
Relational Operators
- expression 1>expression 2: this means to check if expression 1 is greater than expression 2
- expression 1<expression 2: check if expression 1 is lesser than expression 2
- expression 1>=expression 2: check if expression 1 is greater than or equal to expression 2
- expression 1<=expression 2: expression 1 is lesser than or equal to expression 2,
- expression 1==expression 2 :check if expression 1 equals expression 2.
Logical Operators
- AND && When all expression are true, only then the statement is true.
- OR || When any one expression, is true the entire statement is true.
- NOT**!** Simply reverses the value which means if the statement is true it turns it into false.
Assignment Operators: used to assign value to variables and constants.
Conditional Operators: expression ? condition 1 : condition 2 ; if expression 1 is true perform condition 1 or else perform condition 2.