What is C Tokens: Identifiers, Constants, Keywords, Data types in C

0
827
what is C tokens

Before you learn the C programming language, you should understand its fundamentals. C tokens are the basic blocks that create the fundamentals of the C programming language. Read on to understand more.

What are C Tokens?

Let us understand tokens in c by taking an example. Whenever a new building is constructed, basic things like bricks, cement, and iron rods are bought. These basic things are called building blocks, and without them, a building can not be constructed.

Similarly, C tokens are the building blocks in the C programming language. They are used to construct a program.

Also Read:How Bitwise Operators Works?

To understand it better, let us take a sample program.

Program 1.1

#include<stdio.h>

int main()

{

int a=10, b=5, sum;

sum = a + b;

printf(“Addition is = %d \n”, sum);

return 0;

}

Before compiling a program, a compiler keeps breaking down the program into small units. The smallest possible unit is known as a C token. In Program 1.1, these units are –

  1. int
  2. main
  3. a, b, sum
  4. {, }, (, )

All these tokens are combined in specific ways to construct a program.

What is the Difference Between Strcpy and Strncpy in C?

Now let us talk about the types of C tokens.

Types of C Tokens

The various types of tokens in C are listed below:

Types of C tokens

Constants in C

The variables that cannot change their values after definition are called constants. Their value remains fixed for the rest of the program.
There are various types of constants in C.

Integers: Those numbers that do not contain any fractional or exponential part. In this programming, they are represented as int. Integers are further classified into three types – Decimal, Octal, and Hexadecimal.

How Many Types of Preprocessor Directives in C Language?

Floats: These are the numbers that have a fractional or an exponential part in them. In programming languages, they are represented as a float. Example – 1.05, -0.075 etc.

Character: These can be single numbers, alphabets, or symbols that are written by enclosing them in single quotation marks. Example – ‘e’, ‘E’, ‘6’, ‘{‘ etc.

How Dangling Pointer affects your Programming?

Escape Sequences: Escape sequences are characters having a special meaning in the programming language. When they are used, they perform a specific function like switching to a new line or using a backspace. The escape sequences in C are listed below:

\n NewLine
\r Return
\t Horizontal Tab
\v Vertical Tab
\b Backspace
\f Form feed
\\ Backslash
\0 Null Character
\? Question Mark
\’ Single Quotation Mark
\” Double Quotation Mark

Keywords in C

Keywords in programming languages are words that have a pre-defined meaning. They perform some specific functions. There are a total of 32 keywords that are supported by the C language. Examples – auto, enum, static, return, else, etc.

How Double Pointer works?

Keywords cannot be used as variable names.

Keywords in C tokens
Keywords in C Tokens

Sample Code Snippet
int main()
{

int a=0;
printf(“This is value of a\n”, a);
return 0;
}
In the above code snippet, int and return are keywords.

Identifiers in C

Identifiers are the names that are given to any element in the program. Example – int a (here a is a variable name)
The rules to write identifiers are:

  1. The name should start with an alphabet or underscore(_)
  2. Special characters are not allowed in the name. The only underscores can be used.
  3. Keywords cannot be used as identifiers.

Sample Code Snippet 

int main()
{
int a, _var;
float b;
}
In the above code snippet, a _var and b are identifiers, as they are the names of variables of data types int and float.

Operator in C

Operators are the symbols that instruct the compiler to do a specific mathematical function. Operators are further classified into the following types.

Arithmetic Operators: Here is the list of arithmetic operators and their functions.

OPERATORS FUNCTION
+ Used to add two operands
Used to subtract the second operand from the first one
* Used to obtain the product of two operands
/ Used to divide the first operand by second
% To obtain the remainder of a division
++ Increase value by one
– – To decrease the value by one

Relational Operators: Here is the list of relational operators and their functions.

OPERATORS FUNCTION
== Used to check if two operands are equal
!= Used to check if two operands are not equal
> To check if the left operand is greater
< To check if the right operand is greater
>= To check if the left operand is greater or equal to the right one
<= To check if the left operand is lesser or equal to the right one

Logical Operators: Here is the list of logical operators and their functions.

OPERATORS FUNCTION
&& Logical AND
|| Logical OR
! Logical NOT

BITWISE Operators: These operators perform operations at the bit level. Here is the list of BITWISE operators and their functions.

OPERATORS FUNCTIONS
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< To shift left
>> To shift right

Conditional Operator: Conditional operator is used to getting the desired outcome if a given condition is true.
Example – statement 1? statement 2: statement 3
Explanation: if statement 1 is true, statement 2 will be executed. If statement 1 is false, statement 3 will be executed.

Assignment Operator: Here is the list of assignment operators and their functions

OPERATORS FUNCTIONS
= To assign the value from operand on the right to one on the left
+= To add the operands and assign the result to the left
-= To subtract the right operand from the left one and assign the result to the left
*= To obtain the product of the operands and assign it to the left
/= To divide the left operand with the right and assign the result to the left
%= To obtain the modulus and assign it to the left

Special Symbols

As the name suggests, they are special symbols that are reserved to be used for a specific purpose. For example, Parentheses() are used for function calls while Braces{} is to start and end a block of code. Similarly, a semicolon; is used to end a statement. The special symbols in C are listed below along with their purpose.

SYMBOL PURPOSE
Brackets [] These are used while creating arrays
Braces {} These are used to start and end a block of code.
Parentheses () These are used to make function calls.
Comma, It separates different statements. This is also used to separate parameters during function calls.
Preprocessor # The compiler to do some specific operation before the compilation starts. Example: #include<math.h> is used to include the header file that contains mathematical symbols and operations.
Asterisk * This is used to create pointers.
Semicolon ; Used to terminate the statement.
Assignment operator = This operator assign values.

Click here to practice program online

Various Data Types in C Programming Language

Let us talk about the various data types that are supported by C.

Data Type Memory Required in Bytes Range of the Data Type Format Specifier Used
int

4

-2^31 to (2^31 -1)

%d

float

4

3.4E -38 to 3.4E +38

%f

Signed char

1

-128 to 127

%c

Unsigned char

1

0 to 255

%c

double

8

 

%lf

Different modifiers like long, short, etc. can be used to extend or reduce the range of a data type.

Data Type Memory Required in Bytes Range of The Data Type Format Specifier Used
Short int 2 -32,768 to 32,767 %hd
Unsigned short int 2 0 to 65,535 %hu
Unsigned int 4 0 to 4,294,967,295 %u
Long int 4 -2,147,483,648 to 2,147,483,647 &ld
Unsigned long int 4 0 to 4,294,967,295 %lu
Long long int 8 -2^63 to (2^63 – 1) %lld
Unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu
Long double 12   &Lf

You can also know the size of a variable by using the sizeof () operator. The below example will explain to you how to use the sizeof () operator.

#include<stdio.h>
int main()
{
int x; //Declare Integer
printf(“Enter the integer: \n”);
scanf(“%d”, &x);
printf(“Integer size is: %lu bytes \n”, sizeof(x));
return 0;
}

Output

Enter the integer

3

The size of the integer is 4 bytes.

Derived Data Types in C

Users can use the type definition feature of C to represent existing data types by defining their identifiers.

  1. Structure: ‘struct’ keyword is used to define a structure in C. Structure is used as a package of numerous variables and is used to handle large data. This can be explained by taking an example. Suppose you need to write a code that tells about the details of an employee. You will need to create different variables as the employee’s attributes like age, name, etc. If you are trying to include the details of the entire organization, then you will have to create numerous variables for each employee. To simplify this problem, a structure is used. A structure can be created which will consist of these attributes and then they can be applied to different employees.
  2. Union: Using Union, different data types can be stored in the same memory location.
  3. Enum: We define an Enumerated data type using the keyword ‘enum’. Enumeration is a data type consisting of integral constants.

Implementation of structure in a program

#include <stdio.h>
struct emp_details
{
char *name;
int emp_id;
int age;
};

int main()
{
struct emp_details employee;
employee.name = “xyz”;
employee.emp_id = 598001;
employee.age = 21;
printf(“Name of employee is: %s \n”, employee.name);
printf(“Id of employee is: %d \n”, employee.emp_id);
printf(“Age of the employee is: %d \n”, employee.age);
return 0;
}

Output

Name of employee is: xyz

Id of employee is: 598001

Age of the employee is: 21

Implementation of Union in a program

#include <stdio.h>
#include <string.h>
union Random
{
int x;
float y;
char z[50];
};

int main( )
{
union Random random;
printf( “Size of memory occupied by random is %d\n”, sizeof(random));
return 0;
}

Implementation of enum in a program:

#include<stdio.h>
enum year{January, February, March, April, May, June, July,August, September, October, November, December};
int main()
{
int x;
for (x=January; x<=December;x++)
printf (“%d\n”,x);
return 0;
}

Output

0

1

2

3

4

5

6

7

8

9

10

11

Typedef  in C

A keyword called ‘typedef’ can be used in C to assign a new name to a data type. When you use the typedef to give a new name to a data type, the new name can be used as an identifier for future variable declarations.

Implementation of Typedef in a program

#include<stdio.h>
typedef int NUM;
int main()
{
NUM x=10;
printf(“This is an integer: %d \n”, x);
return 0;
}

NOTE: To remember that the given name is an identifier, we use uppercase letters by convention.

Output

This is an integer:10

Derived Data Types in C Continued 

There are two more types of derived data types in the C programming language. These are – Array and Pointers.

  1. Array: An array is a data type which is capable of storing multiple values. It’s syntax is – DataType ArrayName[Size of array]
  2. Pointer: All the variables are stored in a memory location with a definite address. So pointers are those variables which contain the addresses of other variables. Pointers are declared by – dataType *name

Implementation of Pointers in a program

#include <stdio.h>
int main ()
{
int x = 10; //Initialize variable x=10
int *y; //Declare pointer variable y
y=&x; //Initialize variable y
printf (“The address of variable x is: %x\n”, &x);
printf(“The address stored in the variable y is: %x \n”, y );
printf(“Value of *y: %d \n”, *y );
return 0;
}

Output

The address of variable x is: 28ff18

The address stored in the variable y is:28ff18

Value of *y:10

Implementation of Array in a program

#include <stdio.h>
void main()
{
int arr[5];
int i;
printf(“Enter the array elements: \n”);
for (i=0; i<5; i++)
{
scanf(“%d”, &arr[i]);
}
return 0;
}

Output

Enter the array elements

1 2 3 4 5

LEAVE A REPLY

Please enter your comment!
Please enter your name here