Curriculum
Course: Complete C Programming Course
Login
Text lesson

Introduction to Tokens in C

 In this lesson, you will learn,

  • Motivation
  • C Character Set
  • Tokens in C

 

Motivation

Like English or any other language is used for communication, the C language is one of the computer programming languages used to communicate with a computer.

Learning the C language is similar to learning any other language, such as English. Like English, it involves learning starting with the alphabet, words, sentences, and paragraphs, while C focuses on understanding similar steps as shown in the figure below.

 

What is the C Character Set

In C programming, the character set refers to the set of valid characters that are recognized and allowed while writing C programs.

These characters are used to form tokens like keywords, identifiers, constants, operators, and expressions.

CategoryCharactersExamples / Usage
1. Letters(Alphabets)A–Z, a–zint, main, printf, char, sum
2. Digits0–910, 99, value1, number2
3. Special SymbolsVarious symbols used in syntax~ ‘ ! @ # % ^ & * ( ) _ – + = | \ { }
[ ] : ; ” ‘ < > , . ? /

 

Tokens in C Language

A token is a basic building blocks and known as the smallest meaningful unit in a C program.

It is created from the C character set. The C character set provides letters, digits, symbols, and whitespace, which are combined to form tokens.

  • For example:

    • int is a keyword token, formed using letters from the character set.

    • 123 is a constant token, formed from digits.

    • +, = are operator tokens, formed from special characters.

So, the character set provides the raw material, and tokens are the meaningful building blocks.

 

Types of Tokens in C

There are 5 primary types of tokens in the C language:

 

Identifiers

Identifiers are user-defined names for variables, functions, arrays, structure etc. and it is created by the programmer.


Rules:

  • Must begin with a letter (A–Z or a–z) or underscore _.

  • Can contain letters, digits, and underscores.

  • Cannot use keywords as identifiers.

  • Case-sensitive

Examples: Valid Identifiers

IdentifierReason
countStarts with a letter, no special chars
num1Contains digits after letters
_valueStarts with underscore
AverageMarksFollows all rules
sum_2025Letters, digits, underscore

 

Examples: Invalid Identifiers

IdentifierReason
1dataStarts with a digit
floatReserved keyword in C
@countContains invalid character @
total-valueContains hyphen - (not allowed)
marks#Contains #, which is not a valid symbol

 

Keywords in C Language

Keywords are reserved words that have predefined meanings.

These words are part of the C language syntax and cannot be used as identifiers (variable names, function names, etc.).

📌 Characteristics of Keywords:

PropertyDescription
ReservedYou cannot use them as variable names.
LowercaseAll keywords are written in lowercase.
Part of syntaxUsed for data types, control flow, etc.
Compiler-recognizedThe compiler knows the meaning and function.

 

✅ List of All 32 Keywords in C (ANSI C)

autobreakcasechar
constcontinuedefaultdo
doubleelseenumextern
floatforgotoif
intlongregisterreturn
shortsignedsizeofstatic
structswitchtypedefunion
unsignedvoidvolatilewhile

 

📄 Keywords with Their Meaning

KeywordMeaning
intDeclares an integer variable
floatDeclares a floating-point variable
charDeclares a character variable
ifConditional statement
elseAlternate path of execution when if fails
whileLooping construct
forLooping construct
doExecutes loop body first, then checks condition
returnReturns a value from a function
voidSpecifies that a function does not return a value
breakExits a loop or switch statement
continueSkips the current iteration of a loop
switchMulti-way branching statement
caseDefines individual cases in a switch
defaultExecutes if no case matches in switch
sizeofReturns the size (in bytes) of a data type
structDeclares a structure (user-defined type)
unionDeclares a union (shared memory for multiple variables)
typedefDefines new data type names
constDeclares a constant (read-only) variable
volatileTells the compiler the variable may change unexpectedly
enumDeclares enumerated constants
gotoJumps to a labeled statement
staticPreserves the value of a variable between function calls
externDeclares a variable defined elsewhere
registerHints to store variable in CPU register for fast access
signedDeclares signed variables (can hold negative values)
unsignedDeclares unsigned variables (only positive values)
longDeclares long integer data type
shortDeclares short integer data type
autoAutomatically defines storage class (default for local variables)

 

Constants

A constant is a fixed value that does not change during the execution of a program. Constants are used to store values that must remain the same throughout the program.

📌 Types of Constants in C

TypeDescription
1. Integer ConstantsWhole numbers (positive or negative)
2. Floating-point ConstantsNumbers with decimal points
3. Character ConstantsSingle character enclosed in single quotes
4. String ConstantsSequence of characters in double quotes
5. Enumeration ConstantsNamed set of integer constants
6. Symbolic ConstantsNamed constant defined using #define

 

1. Integer Constants

  • Represent whole numbers (with no decimal).

  • Can be in decimal, octal, or hexadecimal format.

int a = 25;         // Decimal
int b = 032;        // Octal (starts with 0)
int c = 0x2A;       // Hexadecimal (starts with 0x)

2. Floating-point Constants

  • Represent numbers with a decimal point or in exponential form.

float pi = 3.14;
double gravity = 9.8;
float sci = 1.23e3;   // 1.23 &times; 10&sup3; = 1230

3. Character Constants

  • A single character enclosed in single quotes.

char ch = 'A';
char newline = '\n';  // Special character constant

4. String Constants

  • A sequence of characters enclosed in double quotes.

printf("Hello, World!");
char name[] = "Amit";

5. Enumeration Constants

  • Used to define a set of named integer constants using enum.

enum days { SUN, MON, TUE, WED };
enum days today = MON; // today = 1

6. Symbolic Constants

  • Defined using the #define preprocessor directive.

#define PI 3.1415
#define MAX 100

printf("Value of PI is: %f", PI);

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review