Tokens in C Programming

Tokens in C Programming

5 mins read13.9K Views Comment
Atul
Atul Harsha
Senior Manager Content
Updated on Aug 14, 2023 16:19 IST

Tokens are the smallest unit in the C program. Every keyword or character or sequence of characters that you come across in C is a token. It is the smallest element identified by the C compiler. 

2022_07_Untitled-design.jpg

Think of tokens in C Programming as puzzle pieces. Each piece, or token, has a role. They come together to create a complete picture, or in this case, a program. Tokens can be words, numbers, or symbols. They make the code work.

Must Explore: C Programming Online Courses and Certifications

Let’s dive deep into the tokens to understand C programming clearly.

Table of Contents for Tokens in C

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4.24 K
6 weeks
475
6 weeks
– / –
45 hours
4 K
80 hours
– / –
1 month
– / –
40 hours
– / –
2 months
– / –
1 year
4 K
80 hours

What are Tokens in C programming?

Tokens in C are like Lego blocks. They are the smallest units in a C program. Here are the six types of tokens:

  1. Identifiers: These are names given to variables, functions, and arrays.
  2. Keywords: Reserved words in C. They must be written in lowercase.
  3. Operators: Symbols that operate on one or more operands to give an output.
  4. Strings: Sequence of characters enclosed within single or double quotation mark.
  5. Special Characters: These include parentheses, braces, brackets, semicolons, etc.
  6. Constants: Fixed values that don’t change during execution.

NOTE: C program is a collection of different types of token. Every single value you

Use of Tokens in C

Tokens are the building blocks of C. Just like you cannot write words without letters or sentences without words, similarly, you need a token to write codes in C. 

Also Read: What is C Programming?

Types of Tokens in C

Tokens in C are categorized into the following 6 types. Let’s understand them one by one:

Identifiers in C

As the name suggests identifiers are used to identify something in C. An identifier is the user-defined name of a variable, method, or class. 

Let’s understand the rules for naming an identifier in C:

  • Identifiers must not start with a number.
  • First character should be either an underscore or an alphabet followed by any sequence of alpha-numeric combination or underscore.
  • Identifiers are case sensitive. ie. ‘Name’ and ‘name’ would be considered as two different identifiers.
  • Keywords cannot be used as identifiers.
  • Blank spaces or commas are not allowed in the identifier. i.e name var is wrong and name_var is correct.
  • At max you can have 31 characters in an identifier.
  • Not a rule but having a meaningful short identifier name is ideal.

Example of Identifiers

 

Valid Identifiers Bad Identifiers
totalScore 123abc (Starts with a number)
playerName total score (Contains a space)
_temporaryValue void (Is a keyword)
MAX_LIMIT a (Not descriptive)
calculateAverage p_n (Unclear abbreviation)

Keywords in C

Keywords are special reserved words in C. These special words hold a specific predefined meaning and functionality of its own. Using them as identifiers would result in error. C has in total 32 such predefined words known as keywords. 

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

Operators are special symbols that are used to perform specific operations between two identifiers. The identifiers on which the operations are performed on operators. In C we classify the operators as below:

  • Unary Operator
  • Binary Operator
  • Ternary Operator

Unary Operator

Unary operators in C work on one operand. Here are some examples:

Operator Description
++ Increment operator. Increases value by 1.
– – Decrement operator. Decreases value by 1.
Negation operator. Changes sign of value.
! Logical NOT operator. Reverses logical state.
& Address operator. Gives memory address of value.
* Dereference operator. Gives value at a memory address.

Binary Operator 

Binary operators in C work on two operands. Here are some examples:

Type of Operator Symbol Description
Arithmetic + Addition
Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
Relational == Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Logical && Logical AND
|| Logical OR
! Logical NOT (though unary)
Bitwise & Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Left shift
>> Right shift
Assignment = Assign
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign

Ternary Operator

Ternary means three. As the name suggests, this type of operator (?:) requires three operands. The ternary operator in C is a shorthand for an “if-else” statement. It’s used to assign a value to a variable based on a condition.

The ternary operator in C requires three operands. Here’s why:

  1. Condition: The first operand is a condition that evaluates to true or false.
  2. Value if True: The second operand is the value that will be used if the condition is true.
  3. Value if False: The third operand is the value that will be used if the condition is false.
  4. It consists of three parts: a condition, a value if the condition is true, and a value if the condition is false.
  5. The syntax is: condition ? value_if_true : value_if_false.

Example:


 
int a = 10, b = 20;
int max = (a > b) ? a : b; // max will be 20, as the condition is false
Copy code

String in C

Strings are sequences of characters enclosed within the single or double quotes. Each string is followed by a null character (‘/0’) which represents the end of the string. 

Example:


 
char s[7] = 'shiksha'; //Compiler allocates 9 bytes to the ‘x’ array.
char s[] = 'shiksha'; // Memory allocation is done during the run time.
char s[7] = {'s','h','i','k','s','h','a',\0}; // Represented as individual characters.
Copy code

Special Character in C

Special characters in C are reserved characters which hold predefined meaning to it. Let’s see some of the special characters defined in C:

  • ( ) Small Brackets: Used with function declaration or function calling. Eg: scanf()
  • [ ] Square Brackets: Used with array declaration
  • { } Curly brackets: Denotes the opening or closing of a block
  • (,) Comma: Used to separate two or more variables or parameters in function
  • (.) Dot: Used to access members of union or a structure
  • (*) Asterisks: Used while declaring pointers in C
  • (#) Hash: Used as a preprocessor directive while including the header files. Eg: #include<conio.h>

Constant in C

Constant refers to a value of a variable which does not change throughout the program.

List of the types of constants used in C language:

Type of Constant Example
Floating-point constant 3.14, 23.33, 52.23, etc
Integer constant 10, 53, 26, etc.
Hexadecimal constant 0x9x, 0x3, 0X8, etc.
Octal constant 022, 019, 07, 0121, etc.
String constant “Python”, “Program”, “C”,etc.
Character constant ‘a’, ‘b’, ‘c’, etc.

FAQs

What is a token in C language?

Tokens are the smallest building block of the C language. Each character or sequence of characters is a token.

What are the 6 tokens in C?

There are 6 tokens in C: Identifiers, Keywords, Operators, Strings, Special Characters, Constant.

Is printf a token?

In short YES. printf is a keyword and all the keywords are a token so printf is a token.

What are keywords in C?

Keywords are special reserved words with special pre-defined functionality. For example: if, else, while, etc.

About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio