You are on page 1of 2

Aw, Snap …

So yesterday I got home from college and was stupendously tired. I ended up watching
a documentary on the Babri Mosque Issue (more on that later). Thereby, I was unable to
study and upload yesterday so yeah. Anyhow let’s get started:

Tokens
Tokens are the smallest individual units of a program. C++ has the following tokens
which can be remembered by the mnemonic KICSO which as we will see, means: -

• Keywords
• Identifiers
• Constants
• Strings
• Operators

First, we shall go through a few of these tokens: -

• Keyword: These are reserved words which have special meaning to the compiler.
I’ll lay down a few examples: - auto, break, case, switch, continue etc.
• Identifiers and Constants: These are the names given by the programmer to
variables, arrays, functions etc. Constants refer to fixed values whose values
don’t change during the execution of a program. Also, Fun Fact: The limit for
variable names in C was 32 however ANSI C++ has placed absolutely no limits on the
length of variable names.
• Strings: Since, it is trivial to know about strings, I’ll like to point it out that in C++
strings used can be of two types – i) it can be of the C type i.e. char array or ii)
string class in C++. Both of these have their special uses and are hence utilised
by need.
• Basic Data Types: Both C and ANSI C++ allow the usage of basic data types with
modifiers (except void). Modifiers can be long, short, signed, unsigned etc and can
be applied to all basic integer and character data types.
• User Defined Datatypes: Sometimes in programming, there arises a need of
composite datatype which is comprised of many different datatypes. Such needs
can be met with by the usage of these user defined datatypes. Struct and union
are two keywords used for creating a user defined datatype. Let’s begin with
struct:
struct book
{
char title[50]; //Holds the name of the book
int pages; //holds number of pages
double price; //holds price of the book
char author[50]; //holds the name of the author
};
struct book book1,book2,book3;
The above example creates three instances of the datatype book named book1,
book2 and book3. The dot(.) operator can be used to access each of their
information. E.g.-
book1.pages; //accesses the no. of the pages in book1

Union is also used for creating datatypes. Let us see the use of union and then
we will see their basic differences.
union result
{
int marks;
char grade;
float percent;
};

Both struct and union serve similar purposes, however, both have different pros
and cons. In struct, the size is the total size of the constituent datatypes.
However, if we use union, the total size of the datatype is the size of the largest
datatype. E.g.- result will have the size of float.

• Enumerated Datatype: It is another datatype which allows for associating


names to numbers, thus increasing the comprehensibility of the code. The enum
keyword from C automatically enumerates a list off words by assigning them 0,1
2 and so on. E.g. –
enum shapes {circle, square, triangle};
enum colour {red, blue, green, yellow};
enum position {on, off};

These can be used as follows: -


colour background= blue;

You might also like