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

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

15 Web Design Trends to Watch in 2018

The modern world is full of extraordinary things that influence our imagination and mood. Our soul needs a perfect atmosphere and impressive spots. To apply such things in practice, we have submitted the list of the web trends that deserve your attention. Robert frost design analysis will meet all your wishes and expectations. Image Source Web Design Trends to Watch in 2018 1. More Organic Shapes Until this year, web design, as well as mobile design, were based on the right-angled and sharp-edged shapes. However, it seems that this year will bring some significant changes in the field of web design. The recent trends will offer the absolute rounded corners. In addition, the web design of 2018 will make the real things look like the cartoonish ones. 2.   Bold Minimalism Although some of you may think that this web design trend will not attract the Internet users. Indeed, the notion of minimalism is often associated with boredom and dullness. However, in this case, bold minimalis

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and