Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Tokens in C

Tokens are some of the most important elements used in the C language for creating a program. One can define tokens in C as the smallest individual elements in a program that is meaningful to the functioning of a compiler.

A token is the smallest unit used in a C program. Each and every punctuation and word that you come across in a C program is token. A compiler breaks a C program into tokens and then proceeds ahead to the next stages used in the compilation process.

In this article, we will take a closer look at Tokens in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Use of the Tokens in C

For instance, without words, you cannot create any sentence- similarly, you cannot create any program without using tokens in C language. Thus, we can also say that tokens are the building blocks or the very basic components used in creating any program in the C language.

Classification and Types of Tokens in C

Here are the categories in which we can divide the token in C language:

  • Identifiers in C
  • Keywords in C
  • Operators in C
  • Strings in C
  • Special Characters in C
  • Constant in C

Let’s look at each of these- one by one in detail:

Identifiers in C

These are used to name the arrays, functions, structures, variables, etc. The identifiers are user-defined words in the C language. These can consist of lowercase letters, uppercase letters, digits, or underscores, but the starting letter should always be either an alphabet or an underscore. We cannot make use of identifiers in the form of keywords. Here are the rules that we must follow when constructing the identifiers:

  • The identifiers must not begin with a numerical digit.
  • The first character used in an identifier should be either an underscore or an alphabet. After that, any of the characters, underscores, or digits can follow it.
  • Both- the lowercase and uppercase letters are distinct in an identifier. Thus, we can safely say that an identifier is case-sensitive.
  • We cannot use an identifier for representing the keywords.
  • An identifier does not specify blank spaces or commas.
  • The maximum length of an identifier is 31 characters.
  • We must write identifiers in such a way that it is not only meaningful- but also easy to read and short.

Keywords in C

We can define the keywords as the reserved or pre-defined words that hold their own importance. It means that every keyword has a functionality of its own. Since the keywords are basically predefined words that the compilers use, thus we cannot use them as the names of variables. If we use the keywords in the form of variable names, it would mean that we assign a different meaning to it- something that isn’t allowed. The C language provides a support for 32 keywords, as mentioned below:

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

Operators in C

The operators in C are the special symbols that we use for performing various functions. Operands are those data items on which we apply the operators. We apply the operators in between various operands. On the basis of the total number of operands, here is how we classify the operators:

  • Unary Operator
  • Binary Operator
  • Ternary Operator

Unary Operator

The unary operator in c is a type of operator that gets applied to one single operand, for example: (–) decrement operator, (++) increment operator, (type)*, sizeof, etc.

Binary Operator

Binary operators are the types of operators that we apply between two of the operands. Here is a list of all the binary operators that we have in the C language:

  • Relational Operators
  • Arithmetic Operators
  • Logical Operators
  • Shift Operators
  • Conditional Operators
  • Bitwise Operators
  • Misc Operator
  • Assignment Operator

Ternary Operator

Using this operator would require a total of three operands. For instance, we can use the ?: in place of the if-else conditions.

Strings in C

The strings in C always get represented in the form of an array of characters. We have a ‘\0′ null character at the end of any string- thus, this null character represents the end of that string. In C language, double quotes enclose the strings, while the characters get enclosed typically within various single characters. The number of characters in a string decides the size of that string.

Now, there are different ways in which we can describe a string:

char x[9] = “chocolate’; // Here, the compiler allocates a total of 9 bytes to the ‘x’ array.

char x[] = ‘chocolate’; // Here, the compiler performs allocation of memory during the run time.

char x[9] = {‘c’,’h’,’o’,’c’,’o’,’l’,’a’,’t’,’e’,’\0′}; // Here, we are representing the string in the form of the individual characters that it has.

Special Characters in C

We also use some of the special characters in the C language, and all of them hold a special meaning that we cannot use for any other purpose.

  • () Simple brackets – We use these during function calling as well as during function declaration. For instance, the function printf() is pre-defined.
  • [ ] Square brackets – The closing and opening brackets represent the multidimensional and single subscripts.
  • (,) Comma – We use the comma for separating more than one statement, separating the function parameters used in a function call, and for separating various variables when we print the value of multiple variables using only one printf statement.
  • { } Curly braces – We use it during the closing as well as opening of any code. We also use the curly braces during the closing and opening of the loops.
  • (*) Asterisk – We use this symbol for representing the pointers and we also use this symbol as a type of operator for multiplication.
  • (#) Hash/preprocessor – We use it for the preprocessor directive. This processor basically denotes that the user is utilizing the header file.
  • (.) Period – We use the period symbol for accessing a member of a union or a structure.
  • (~) Tilde – We use this special character in the form of a destructor for free memory.

Constant in C

Constant is basically a value of a variable that does not change throughout a program. The constants remain the same, and we cannot change their value whatsoever. Here are two of the ways in which we can declare a constant:

  • By using a #define pre-processor
  • By using a const keyword

Here is a list of the types of constants that we use in the C language:

Type of Constant Example
Floating-point constant 25.7, 87.4, 13.9, etc.
Integer constant 20, 41, 94, etc.
Hexadecimal constant 0x5x, 0x3y, 0x8z, etc.
Octal constant 033, 099, 077, 011, etc.
String constant “c++”, “.net”, “java”, etc.
Character constant ‘p’, ‘q’, ‘r’, etc.

Practice Problems on Tokens in C

1. Which of these is a keyword in the C language?

1. signed

2. register

3. goto

4. enum

A. 1, 2, 4

B. 2, 3

C. 1, 3, 4

D. All of the above

Answer – D. All of the above

2. Identify the rules that must be followed in the case of identifiers:

1. The identifiers must begin with a numerical digit.

2. The first character used in an identifier should be either an underscore or an alphabet.

3. The lowercase and uppercase letters are not distinct in an identifier.

4. An identifier does not specify blank spaces or commas.

5. The maximum length of an identifier is 31 characters.

A. 1, 3, and 5

B. 2, 4, and 5

C. 1, 2, and 4

D. 2, 3, and 5

Answer – B. 2, 4, and 5

3. Which of these is not a constant used in the C language?

A. Octal Constants

B. String Constants

C. Integral Constants

D. Floating Constants

Answer – C. Integral Constants


FAQs

Q1

Is there a difference between constants and literals?

No. Both of these are the same. The syntax of a constant goes as follows:
const data_type variable_name = value;

Q2

Do keywords have functions pre-defined for them, and can we use them for variables?

Yes, keywords have functions pre-defined for them, but we cannot use these to assign names to the variable. If we do so, it would connote a very different meaning, and we will experience a lot of errors.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*