Skip to main content

C++ Tokens – Keywords, Constants, Identifiers, Strings, Operators & Special Symbols

Here you will learn about C++ tokens, keywords, constants, identifiers, strings, operators and special symbols.

What are Tokens?

In simple words, we can say that tokens are the smallest component pertaining to any program that make sense to the compiler, i.e. compiler can very easily understand that. We can classify the tokens into six different sub-categories as follows:

  • Keywords
  • Constants
  • Strings
  • Identifiers
  • Operators
  • Special Symbols

Now we are going to illustrate these one by one and will see what exactly they mean.

Keyword

The keywords are the reserved terms in any programming language. Every keyword in the language is expected to provide an intended functionality to the program. We cannot use the keywords  as variable names because this practice tries to assign a new meaning to the keyword which is not appreciated nor allowed in any programming language. However, the C/C++ pre-processor directives (so-called header files) can be used to specify text to be exchanged for keywords before compilation. C language preferably supports for 32 keywords that we have mentioned in the tabular form below:

do for float continue
void else if switch
struct enum register char
typedef static return default
const int short volatile
while double break signed
union long sizeof unsigned
extern goto auto case

Apart from supporting these 32 keywords, C++ has 31 more keywords which are:

typeid false catch new
this delete try explicit
export typename using inline
asm namespace class throw
private protected public true
static_cast template const_cast operator
reinterpret_cast dynamic_cast mutable bool
friend virtual wchar_t

Constants

Constants are normally the variables. The only thing that differentiates Constants from Variables is the fact that it is not allowed to modify the value of a constant by the program after the constants have already been defined.

Constants refer to fixed values.

Constants are also sometimes referred as Literals.

They may belong to any of the data types.

Constant types

  • Integer constants: For example: 0, 5, 957, 12376 etc.
  • Floating Point / Real constants: For example: 0.7, 8.65, 4587.05 etc.
  • Octal and Hexadecimal constants: For example:
    • Octal: (15)8  = (13)10
    • Hexadecimal: (015)16 = (21)10
  • Character constants: For example: ‘a’, ‘A’, ‘x’, ‘Z’ etc.
  • String constants:  For example: “TheCrazyProgrammer”

Strings

Strings are regarded as an array of characters, ending with a null character “\0”. The null character is used to point the ending of the specified string.

Strings, in practical use are expected to be enclosed in double quotes (“ ”), whereas the character within single quotes (‘ ’).

C++ Tokens 1

The above declarations can be illustrated as:

  1. When char is declared as “flag[25]”, that means 25 bytes of space in the memory is provided for accommodating the value of the string.
  2. But when we’ve declared char as “flag[]”, the space in the memory will be provided by the CPU strictly according to the requirement at runtime.

Identifiers

Identifiers are saviors. We can use them as the general terminology for the naming purpose of variables, arrays, and functions. Identifiers are the user-defined names which may consist of a long sequence of letters and digits with either a letter or the underscore (_) as a first character.

Keywords can’t be used as identifiers, as they are reserved for special purpose. To use the identifiers in program statements we need to declare it in the program itself.

Rules for naming C++ identifiers

  • Identifiers must begin with a letter or underscore( _ ).
  • No special characters are allowed in identifier naming, only letters, digits or underscores can be used.
  • A keyword can’t be an identifier.
  • White space is not allowed within identifier
  • It should only be up to 31 characters long, as only first 31 characters are significant.

C identifiers examples

C++ Tokens 2

Operators

The operators are the symbols that are commonly used to trigger some action when applied to variables or other objects. The operator needs some data items to act upon, those data items are referred to as Operands. For example in (a + b), ‘+’ sign is the operator whereas ‘a’ & ‘b’ are the operands.

Types of Operators

  • Unary Operators: The operators that work upon a single operand only are called Unary Operators. Example:- Increment & decrement operators.
  • Binary Operators: As clear from its name itself, Binary Operators are those which require two different operands to work upon. They can be classified into:
    1. Arithmetic Operators
    2. Relational Operators
    3. Logical Operators
    4. Assignment Operators
    5. Conditional Operators
    6. Bitwise Operators
  • Ternary Operators: The operators that require three different operands to work upon are known as Ternary Operators. Conditional Operator (?:) is an example of ternary operator.

Special Symbols

The symbols that are used in C/C++ with some special meaning and for some specific function are called as Special Symbols.

The special symbols being used with context to programming language are illustrated below as:

  • Brackets []: These opening and closing brackets are used as array element reference. These are used to indicate single & multidimensional subscripts.
  • Braces {}: Opening and closing curly braces are used to mark start and end of a block of code containing more than one statement.
  • Comma ( , ): To separate more than one statement, Comma is used for example in for loop comma separates initialization, condition & increment.
  • Semicolon ( ; ): Used at the end of statements for termination.
  • Parenthesis () : Are used to indicate function parameters & function calls.
  • Asterick ( * ): This special symbol is used to create a pointer variable.
  • Assignment Operator ( = ): For assigning values, this special symbol is used.
  • Preprocessor ( # ): This you must have seen attached with the header files. This is automatically used by the compiler to transform your program before actual compilation.

 Reference: http://www.geeksforgeeks.org/cc-tokens/

Comment below if you have any queries related to above C++ tokens tutorial.

The post C++ Tokens – Keywords, Constants, Identifiers, Strings, Operators & Special Symbols appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2017/11/c-tokens.html

Comments

Popular posts from this blog

Rail Fence Cipher Program in C and C++[Encryption & Decryption]

Here you will get rail fence cipher program in C and C++ for encryption and decryption. It is a kind of transposition cipher which is also known as zigzag cipher. Below is an example. Here Key = 3. For encryption we write the message diagonally in zigzag form in a matrix having total rows = key and total columns = message length. Then read the matrix row wise horizontally to get encrypted message. Rail Fence Cipher Program in C #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) f...

Data Encryption Standard (DES) Algorithm

Data Encryption Standard is a symmetric-key algorithm for the encrypting the data. It comes under block cipher algorithm which follows Feistel structure. Here is the block diagram of Data Encryption Standard. Fig1: DES Algorithm Block Diagram [Image Source: Cryptography and Network Security Principles and Practices 4 th Ed by William Stallings] Explanation for above diagram: Each character of plain text converted into binary format. Every time we take 64 bits from that and give as input to DES algorithm, then it processed through 16 rounds and then converted to cipher text. Initial Permutation: 64 bit plain text goes under initial permutation and then given to round 1. Since initial permutation step receiving 64 bits, it contains an 1×64 matrix which contains numbers from 1 to 64 but in shuffled order. After that, we arrange our original 64 bit text in the order mentioned in that matrix. [You can see the matrix in below code] After initial permutation, 64 bit text passed throug...

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan...