Introduction
Every c++ program is made up of small and individual units and they are known as tokens in c++. In this post, you will learn all the tokens in the c++ and they are:
- Keywords
- Identifiers
- Constants
- Strings
- Operators
A complete c++ program contain tokens, white spaces and all the syntaxes. Most of the tokens are same as the c programming. But c++ has some additional tokens. Now, let us study these one by one.
Keywords
These are some special words whose meaning is already known to the compiler. We can call these keywords as reserved words. Whenever we will use these keywords, compiler will automatically understand what it has to do.
You can not use them as the general identifier in your program. There are some common keywords between c and c++. The following table shows the c++ keywords.
asm | auto | break | case |
catch | char | class | const |
continue | default | delete | do |
double | else | enum | extern |
float | for | friend | goto |
if | inline | int | long |
new | operator | private | protected |
public | register | return | short |
signed | sizeof | static | struct |
switch | template | this | throw |
try | typedef | union | unsigned |
virtual | void | volatile | while |
There are also some keywords which are added by ANSI C++ and they are
bool | export | reinterpret_cast | typename |
const_cast | false | static_cast | using |
dynamic_cast | mutable | true | wchar_t |
explicit | namespace | typeid |
Identifiers
Identifiers are nothing but a name of a variable or variables, functions, arrays, classes, etc. Programmer gives these names but there are certain rules and they are
- Identifiers can have only alphabets, digits and underscore. You can not use other than this for declaring an identifiers. For example, vijay@123 is an invalid identifier because we have used ‘@’ here.
- The name of an identifier will start with alphabet or underscore. You can not use digits at the beginning of the identifier. For example, 123_vijay is an invalid identifier because there is digit at the beginning.
- Like c programming, c++ is also a case sensitive language. That means uppercase letters and lowercase letters are not the same. For example, Vijay and vijay are different for c++ compiler.
- We can not use keyword as an identifier. We know, keyword has fixed meaning which is already known to the compiler.
Before declaring and using an identifier, we must remember these rules.
Constants
Constants values do not change throughout the c++ program. In other words, we can say constant is an entity whose value will not change. For example, 876 is a constant value. It will remain same. So basically we have integer constant, floating point constant, character constant, string constant in c++ and so on.
In the c++ programming, we can declare a constant using the qualifier const. See the following example.
const int num = 50;
After this statement, you can not modify the value of num because now it becomes constant and hence, we will not be able to change it.
Strings
Strings are nothing but a collection of characters. We can also define a string as a character array. Whatever we will write in the double quotation, it becomes string constant.
There is also another way to declare a string. See the following program.
#include <iostream>
using namespace std;
int main()
{
string name;
cout<<"Enter your name\n";
cin>>name;
cout<<"Hello, "<<name;
return 0;
}
Here, instead of using character array, we have declared the variable name of type string. We can not do this in c programming. See the following output.
Enter your name
Vijay
Hello, Vijay
Operators in C++
All the operators in c programming can be used in c++. But there are some operators in c++ which you can not use in c programming. So, I have already written the post on operators in c programming. You can read that post. These operators you can use in c++. The following table is showing operators in c++ which are newly introduced.
<< | Insertion operator |
>> | Extraction operator |
: : | Scope resolution operator |
: : * | Pointer to member declarator |
->* or .* | Pointer to member operator |
new | It allocates memory |
delete | Release memory |
endl | Line feed operator works just like \n |
setw | Field width operator |
So, these are tokens in c++. This is just a very basic article. I hope, you have understood these tokens in c++. If you have any problems then please feel free to contact me.
Thank you.