Tokens in C: Uses & Types

Jasmine Grover logo

Jasmine Grover Content Strategy Manager

Content Strategy Manager

Token is the smallest unit utilised in a C program. In a C program, every single word and punctuation mark is a token. 

  • A compiler divides a C program into tokens before moving on to the subsequent compilation phases. 
  • Some of the most crucial components needed while writing programs in the C language are tokens. 
  • Tokens in C are the smallest discrete elements of a program that have any significance to a computer's operation.

Read More: Determinant Formula

Key Terms: Constants, Strings, Keywords, Operators, C Language, Tokens, Identifiers, Special Characters


Uses of Tokens in C

[Click Here for Sample Questions]

Tokens are used in the ways listed below:

  • In C, tokens are the equivalent of building blocks, therefore a program cannot be written without them.
  • Tokens are divided into a number of subcategories, each of which has a crucial role to play.
  • For example, identifiers are used to give variables a unique identity, keywords can be used to predefine concepts, operators can be used to carry out actions, and so on.
  • Since strings are needed to define things and special symbols are used in the syntax and many other places, they both play a significant role in issue-solving.

Also Read:


Types of Tokens in C

[Click Here for Sample Questions]

Based on the tasks they are intended to carry out, the C language's tokens can be divided into six categories. These are the several C token types:

  • Keywords
  • Identifiers
  • Constants
  • Strings
  • Special Characters
  • Operators

Keywords

In the C programming language, keywords are a group of pre-defined or reserved words. 

  • These are written in lowercase and are case-sensitive. 
  • The compiler already understands what they mean and how they work.
  • Because doing so would attempt to give the term a new meaning, which is forbidden in the C language
  • The C language supports 32 keywords, including:

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

Identifiers

These are used to name the variables, structures, functions, and so on. The user-defined words in the C language are the identifiers. These may start with an alphabetic letter or an underscore, but they may also be made up of lowercase letters, capital letters, numerals, or underscores. 

The guidelines for creating the identifiers are as follows:

  • The identifiers cannot start with a digit in the numerical range.
  • An underscore or an alphabet should be used as the first character in an identifier. Any of the letters underscores, or numerals may come after that.
  • In an identifier, the capital and lowercase characters are separate from one another. It is safe to say that an identifier is case-sensitive as a result.
  • The keywords cannot be represented by an identifier.
  • An identification does not specify commas or blank spaces.
  • An identifier may have up to 31 characters.
  • We need to write identifiers in a way that is not only clear and concise but also easy to read.

Constants

A constant is just a variable's value that stays the same throughout a program. The constants do not change; we are unable to alter their values in any way. Two methods for declaring a constant are as follows:

  • Using the pre- processor #define
  • Using the keyword const

The various constant types used in the C programming language are listed below:

Constant Example
Integer constant 10, 20, 30 etc.
Floating-point constant 10.2, 20.5, 30.6 etc.
Octal constant 011, 022, 088 etc.
Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.
Character constant x', 'y', 'z' etc.
String constant "Java", "C++", "Python" etc.

Strings

In C, strings are denoted by an array of characters that ends with the null character "0". The string's ending point is indicated by the null character "0." 

  • Additionally, double quotes are always used to encapsulate these strings. 
  • The string's size is essentially determined by the number of characters it contains plus an additional 1 byte of memory space that is always set aside for null character values.

There are several methods to characterise a string right now:

  • char x[9] = “chocolate’; // The 'x' array is given a total of 9 bytes by the compiler in this case.
  • char x[9] = ‘chocolate’; // In this case, memory allocation is handled during runtime by the compiler.
  • char x[9] = {‘c’,’h’,’o’,’c’,’o’,’l’,’a’,’t’,’e’,’\0′}; // In this case, the string is being represented by its individual characters.

Special Characters

The following special symbols are only used in C because they have a specific meaning and can not be used in other contexts. Following is a list of a few of these:

  • Brackets [ ]: Opening and closing brackets serve as references to array elements. These denote subscripts that are single- and multidimensional.
  • Parentheses( ): These unique symbols, known as parentheses, are used to denote function calls and function parameters.
  • Braces { }: A block of code with more than one executable statement has these opening and closing curly braces to indicate where it begins and ends.
  • Comma (,): It is used to divide statements into separate lines, such as when calling functions with different parameters.
  • Colon (:): The operator colon essentially calls a structure known as an initialization list.
  • Semicolon (;): It is referred to as a statement terminator, the semicolon. It denotes the conclusion of a single logical object. Because of this, a semicolon must be used to terminate each separate statement.
  • Asterisk (*): It is used to multiply variables and to construct pointer variables.
  • Assignment operator (=): It is used to assign values and to check the validity of logical operations.
  • Preprocessor (#): The compiler uses the preprocessor, a macro processor, to automatically change your program before it is really compiled.

Operators

In C, operators are specialized symbols that are used to carry out particular tasks; the data items to which they are applied are referred to as operands.

  • The following categories of operators exist for C depending on the number of operands:
  • Unary operators are those that only need one operand to perform an action. 
  • For instance, sizeof, the increment operator (++), and the decrement operator (--).
  • The sorts of operators used between two operands are called binary operators.

Relational Operators

Operators Description
== Is equal to operator
!= Is not equal to operator
> Greater than operator
< Less than operator
>= Greater than equal to operator
<= Less than equal to operator

Arithmetic Operators

Operators Description
+ It is used to perform addition operations
- It is used to perform subtraction operations
* It is used to perform multiplication operations
/ It is used to perform division operations
% It is used to get the remainder value upon division of two numbers

Logical Operators

Operators Description
&& It is called AND operator and is used to perform logical conjunction of two expressions (Result: True if both the expressions evaluate to true else the result remains False
Logical OR It is used to perform logical disjunction on two expressions (Result: True if either or both the expressions evaluate to true)
! It is known as not operator and is used to perform logical negation on an expression

Bitwise Operators

Operators Description
<< Binary Left Shift Operator
>> Binary Right Shift Operator
~ Binary One's Complement Operator
& Binary AND Operator
^ Binary XOR Operator
!= Is not equal to

Assignment Operator

Operators Description
= Equals to assignment operator
+= This operator increments the value and then assign it to the variable
-= This operator decrements the value and then assign it to the variable
*= This operator performs multiplication firstly and then assign it to the variable
/= This operator performs division firstly and then assign it to the variable
%= This operator finds remainder with the operand and then assign it to the variable
<<= This operator left shifts the value and then assign it to the variable
>>= This operator right-shifts the value and then assign it to the variable
&= This operator performs bitwise AND operation firstly and then assign it to the variable
^= This operator performs bitwise XOR operation firstly and then assign it to the variable

Increment and Decrement Operator

Operators Description
++ It is known as an increment operator and is usually used to increment the variable values. Example: int x = 1; x++; printf("Result: %d",x); //Result: 2
-- It is known as decrement operator and is usually used to decrement the variable values. Example: int x = 1; x--; printf("Result: %d",x); //Result: 0

Also Read:


Things to Remember

  • Tokens are important in the C programming language because they serve as the foundation for creating programs.
  • Keywords, Identifiers, Constants, Special Characters, Strings, and Operators are the different categories of tokens in C.
  • In C, a keyword is a group of predefined values or a reserved word with a specific meaning predetermined by the compiler.
  • In C, identifiers are succinct names with descriptive content that specifically identify variables or function names.
  • Variables with fixed values that cannot be changed while a program is running are known as constants in the C programming language.
  • Assignment operator (=): It is used to assign values and to check the validity of logical operations.
  • Preprocessor (#): The compiler uses the preprocessor, a macro processor, to automatically change your program before it is really compiled.

Read More: Tree Topology


Sample Questions 

Ques. Which of the following is a keyword in C? (1 mark)
(A) int
(B) float
(C) char
(D) while

Ans. (D)

Explanation: While is a keyword in C. It is used to create a while loop. The other options are not keywords in C.

Ques. Which of the following is an identifier in C? (1 mark)
(A) 123abc
(B) _abc123
(C) abc$123
(D) int

Ans. (B)

Explanation: _abc123 is an identifier in C. It is a valid identifier because it starts with a letter or an underscore and contains only letters, numbers, and the underscore character. The other options are not identifiers in C.

Ques. Which of the following is a constant in C? (1 mark)
(A) int x = 10;
(B) float y = 3.14159;
(C) char c = 'a';
(D) while (x < y)

Ans. (B)

Explanation: float y = 3.14159; is a constant in C. It is a floating-point constant because it has a decimal point. The other options are not constants in C.

Ques. Which of the following is a special character in C? (1 mark)
(A) +
(B) -
(C) *
(D) ;

Ans. (D)

Explanation: ; is a special character in C. It is used to terminate a statement. The other options are not special characters in C.

Ques. Which of the following is an operator in C? (1 mark)
(A) int
(B) float
(C) char
(D) +

Ans. (D)

Explanation: + is an operator in C. It is an arithmetic operator that is used to add two operands. The other options are not operators in C.

Ques. What is the difference between a keyword and an identifier? (1 mark)

Ans. The main difference between a keyword and an identifier is that a keyword has a special meaning to the compiler, while an identifier does not. Keywords cannot be used as identifiers.

Ques. What is the difference between a constant and a variable? (1 mark)

Ans. The main difference between a constant and a variable is that the value of a constant cannot be changed, while the value of a variable can be changed.

Ques. What is the difference between a string and an array? (1 mark)

Ans. A string is a sequence of characters that is enclosed in double quotes ("). An array is a collection of data items of the same type.

Ques. What are the rules for naming identifiers in C? (1 mark)

Ans. Identifiers must start with a letter or underscore (_). They can then contain any combination of letters, digits, and underscores. Identifiers cannot be keywords, and they cannot contain spaces.

Ques. What are constants in C? (2 marks)

Ans. Constants are values that do not change during the execution of a C program. They can be of the following types:

* Integer constants

* Floating-point constants

* Character constants

* String constants

For Latest Updates on Upcoming Board Exams, Click Here: https://t.me/class_10_12_board_updates


Check-Out: 

Comments


No Comments To Show