Introduction to C
1. Tokens in C
Why do we need Tokens?
Because the computer doesn’t understand sentences like us. It only understands programs broken into small pieces (tokens).
Without tokens, the compiler cannot read and execute your program.
What are Tokens in C?
1st understand in a simple example, Imagine you are writing a letter. A letter is made of sentences, and sentences are made of words.
➡ A C program is made of statements,
➡ and each statement is made of tokens (the smallest parts).
So, tokens = words of the C language.
Tokens in C Language: –
In C, the smallest unit of a program is called a token. Just like a sentence is made of words, a C program is made of tokens.
Types of Tokens in C: –
There are 6 main types of tokens in C:
- Keywords
- Identifiers
- Constants
- Operators
- Special Symbols
- Strings
Explain on by one with simple example,
a) Keywords
These are reserved words with special meaning.
You cannot use them as variable names.
Example:
int,float,return,if,else,while,for.
🍭Example
code<>
int age = 20;
return 0;
b) Identifiers
Names given to variables, functions, arrays, etc.
Example:
age,sum,main,printf.
🍭Example
code<>
int marks;
C) Constants
Fixed values that do not change during program execution.
Types: Integer (
10), Float (3.14), Character ('A'), String ("Hello").
🍭Example
code<>
const int roll = 101;
d) Operators
Symbols used to perform operations.
Example:
+,-,*,/,%,==,&&,||.
🍭Example
code<>
int sum = 5 + 3;
e) Special Symbols
Characters with special meaning in C.
Examples:
{ }→ block of code( )→ function call[ ]→ array;→ statement terminator,→ separator
🍭Example
code<>
printf("Hello");
f) Strings
A sequence of characters enclosed in double quotes (” “).
Example:
"Hello","C Language".
🍭Example
code<>
printf("C is awesome");
**Tokens are the building blocks of a C program. Without tokens, no code can exist.**
2. Data types (fundamental, derived, user-defined)
Data types in C: –
In C, data types tell the computer what kind of data you are storing.
Think of it like different containers in your kitchen:
A bottle is for water (and water need for… Do all of you know what I mean 😉)
A jar is for cookies (and cookies need for…?)
A box is for clothes
In the same way, in C, you need to choose the right container (data type) before storing data.
There are 3 types of Data types,
one is Fundamental (Basic) Data Types, second is Derived Data Types and third is User-Defined Data Types
Now Explain on by one with example :
a) Fundamental (Basic) Data Types
These are the basic containers provided by C.
int→ stores whole numbers (10, -5, 1000)float→ stores decimal numbers (3.14, -2.5)double→ stores bigger decimal numbers (3.14159265)char→ stores single characters (‘A’, ‘b’, ‘9’)void→ means nothing (used for functions that don’t return anything)
Example:
code<>
int age = 18; // integer
float pi = 3.14; // decimal
char grade = 'A'; // character
Here, see a very simple analogy:
int= counting apples (1,2,3)float= measuring milk (2.5 liters)char= writing your grade (‘A’, ‘B’)
b) Derived Data Types
These are created using fundamental data types.
Array → Collection of same type values. Like an egg tray (holds multiple eggs).
🍭Example
code<>
int marks[5] = {80, 90, 85, 70, 95};
Pointer → Stores memory address of another variable.
Like having a house key (it doesn’t hold the house, just points to it).
🍭Example
code<>
int x = 10;
int *p = &x; // p stores address of x
Function → Block of code that performs a task.
Like a washing machine (you give clothes → it does work → gives result).
c) User-Defined Data Types
These are created by the programmer to store custom data.
struct (Structure) → Group of different data types under one name.
Like a student ID card (name, roll, marks all in one place).
🍭Example
code<>
struct Student {
char name[20];
int roll;
float marks;
};
union → Similar to struct but uses shared memory.
Like a single locker used for money or documents (but only one at a time).enum (Enumeration) → Used to store related constants.
Like days of week (Mon=0, Tue=1, …).
🍭Example
code<>
enum Week {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
In one word,
Fundamental → Basic containers (apple, milk, grade).
Derived → Combination containers (egg tray, key, machine).
User-defined → Your own custom containers (student card, locker, weekdays).
3. C syntax rules
Just like English has grammar rules (like subject + verb + object), C has its own syntax rules. If you don’t follow them, the compiler gives an error.
Syntax in C means the set of rules that tells the compiler how the program should be written.
If you break these rules → the compiler shows errors.
“Think of it like learning a new language — you need to know its grammar first.”
🔗Now coming to the rules.
1️⃣ Every C Program Must Have a main() Function
The main() function is the starting point of every C program. Without it, the program cannot run.
🍭Example:
// code<>
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Explanation:
#include <stdio.h>→ includes standard input/output library (for printf).int main()→ starting point of execution.return 0;→ tells the operating system the program ran successfully.
Analogy: Like a movie always starts from the first scene → in C, the program always starts from main().
2️⃣ Each Statement Must End with a Semicolon (;)
C uses semicolon to mark the end of a statement.
🍭Example:
✅Correct:
// code<>
int a = 5;
printf("%d", a);
❌ Wrong:
// code<>
int a = 5
printf("%d", a) // no semicolon here
Compiler won’t understand where the statement ends.
Analogy: Just like a sentence in English ends with a full stop (.), in C a statement ends with a semicolon (;).
3️⃣ C is Case-Sensitive
C treats uppercase and lowercase letters differently.
🍭Example:
// code<>
int age = 20;
int Age = 30;
printf("%d %d", age, Age); // Output: 20 30
Here, age and Age are different variables.
Analogy: Writing “ram” (a boy’s name) and “RAM” (computer memory) → two completely different meanings.
4️⃣ Curly Braces { } Define a Block of Code
When you group multiple statements together, put them inside { }.
🍭Example:
// code<>
if (1) {
printf("Hello\n");
printf("World\n");
}
- Without braces, only the first line after
ifwill be executed.
Analogy: Braces { } are like a bag that holds multiple items together.
5️⃣ Comments are Allowed for Explanation
Comments are ignored by the compiler, but help programmers understand the code.
Single-line comment →
// comment hereMulti-line comment →
/* comment here */
🍭Example:
// This is a single line comment
/* This is a
multi-line
comment */
Analogy: Comments are like sticky notes on your textbook — they don’t change the content but help you remember.
6️⃣Header Files Must be Included
To use functions like printf or scanf, you must include the required header file.
🍭Example:
#include <stdio.h> // for input/output functions
#include <math.h> // for mathematical functions
If you don’t include the correct header file, the compiler won’t recognize the function.
Analogy: Think of header files as toolboxes — if you don’t bring the right toolbox, you can’t do the work.
7️⃣ Whitespace & Indentation are Ignored by Compiler (But Important)
C doesn’t care about spaces or line breaks, but programmers use them for readability.
🍭Both are valid:
printf("Hello");printf("World");printf("Hello");
printf("World");Second one is easier for humans to read.
Analogy: Like handwriting — messy writing may still be correct, but neat writing is easier to read.
8️⃣ Keywords Cannot Be Used as Variable Names
C has reserved words (keywords) like int, if, while, return.
You cannot use them as variable names.
❌ Wrong:
int if = 10; // Error✅ Correct:
int marks = 10; // ValidAnalogy: In your city, some places are reserved (like hospital, school) → you cannot build your house there.
⭐At a glance: –
main()→ Entry point of program.;→ End every statement with semicolon.Case-sensitive →
Age≠age.{ }→ Group statements together.Comments → For explanations (ignored by compiler).
Header files → Toolboxes for extra functions.
Whitespace → Ignored by compiler but helps humans.
Keywords → Reserved, cannot be used as variable names.
Analogy: C syntax is like grammar rules of a language.
If you follow them, the program works smoothly. If you break them, the compiler will complain (errors).
4. Variables & Data Types
If you are starting C programming, two of the most important concepts are Variables and Data Types.
Without them, you cannot store, process, or display data in a program.
🔗What is a Variable in C?
A variable is like a named box in memory that stores data.
You can put something inside it, read it, and change it later.
Syntax:
data_type variable_name = value;
🍭Example:
int age = 20;
float price = 99.50;
char grade = 'A';
Example of Variable:
Think of school lockers:
Each locker has a name/number → variable name.
Each locker can hold only one type of thing → data type.
You can open it, put something, or replace it → change value.
🍭Example:
int apples = 5; // locker for whole numbers
apples = 10; // replace old value with new
Rules for Naming Variables
Must start with letter or underscore.
Cannot have spaces or symbols (
@,#,$).Cannot be a keyword (like
int,if,return).Case-sensitive →
Ageandageare different.
❌ Invalid: 1number, float@value, return
✔️ Valid: number1, _value, studentAge
What is a Data Type in C?
A data type tells the compiler:
What kind of data is stored.
How much memory it needs.
What operations can be performed on it.
💡 Think of it as the label on your box that says what it can contain.
Types of Data Types in C
C data types are divided into 3 main categories:
1️⃣ Fundamental (or Primitive) Data Types
These are the basic building blocks.
| Data Type | Size (in bytes)* | Example Values | Description |
|---|---|---|---|
int | 2 or 4 | 10, -25, 1000 | Whole numbers |
float | 4 | 3.14, -2.5 | Decimal numbers (single) |
double | 8 | 123.4567 | Decimal numbers (double) |
char | 1 | 'A', 'z', '5' | Single character |
(Size depends on compiler/system)
2️⃣ Derived Data Types
Built using fundamental types.
Array → Stores multiple values of the same type.
Pointer → Stores memory address.
Function → Group of reusable code.
Structure → Collection of different types.
🍭Example:
int marks[3] = {80, 90, 100}; // array of integers
3️⃣ User-Defined Data Types
Created by the programmer.
typedef → gives a new name to existing data type.
enum → stores a set of named constants.
struct → groups multiple variables.
union → stores different variables in same memory.
🍭 Example (enum):
enum Week {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
Analogy: –
Imagine your kitchen :
Variable → Jar in your kitchen.
Data Type → Label on jar (sugar jar, rice jar, oil jar).
Value → The actual content inside the jar.
Example in C:
int rice_kg = 5; // jar with label "int", contains whole number
float milk_litre = 2.5; // jar with label "float", contains decimal
char grade = 'A'; // jar with label "char", contains a letter
At a glance: –
A variable is a named storage in memory.
A data type defines the kind of data the variable can hold.
Types of data types:
Fundamental → int, float, double, char
Derived → array, pointer, function, structure
User-defined → typedef, enum, struct, union
Without variables & data types, C programs cannot process or store information.
5. How C is different from other languages?
Key Differences of C with Other Languages: –
C vs C++
C: Procedure-oriented (focus on functions).
C++: Object-oriented (focus on objects, classes, inheritance).
Example:
C is like building a house by step-by-step manual work (brick, cement, paint).
C++ is like using pre-designed modules (rooms, furniture) that can be reused.
C vs Java
C: Compiled language, directly converted to machine code → very fast.
Java: Compiled into bytecode, then executed by JVM (Java Virtual Machine) → slower but portable.
Example:
C is like writing directly in English to a local person (fast communication).
Java is like writing in English, then translating into many languages so people everywhere can understand (slower, but universal).
C vs Python
C: Low-level, requires manual memory management, more code.
Python: High-level, automatic memory management, fewer lines of code.
Example:
C is like driving a manual car – full control but needs more effort.
Python is like driving an automatic car – easier but less control.
Execution Speed
C: Very fast (close to hardware).
Java/Python: Slower (because of interpreters or virtual machines).
Example:
C is like cooking raw food at home (fast & optimized), while Python/Java is like ordering food online (convenient but takes time).
Memory Management
C: Manual memory allocation with
malloc()andfree().C++: Uses constructors/destructors, still offers manual control.
Java/Python: Automatic garbage collection.
Example:
C is like washing your own clothes by hand.
Java/Python is like using a washing machine – easy but you don’t control every detail.
Learning Curve
C: Harder for beginners but builds strong foundation.
Python: Easier for beginners (simple syntax).
Example:
Learning C is like learning mathematics from scratch → tough but rewarding.
Learning Python is like using a calculator → quick and simple, but you may miss the fundamentals.
6. Structure of a C program
Basic Structure of a C Program
- Header File
- Global Declarations (Optional)
- Main Function
- Body of the Program(Local Declarations, Statements / Logic, Return Statement)
- User-Defined Functions (Optional)
Header File: –
-
Lines starting with
#are preprocessor commands. -
Example:
#include <stdio.h>→ tells the compiler to include standard input-output library.
🍭Example: Think of it like importing ingredients before cooking.
Global Declarations (Optional): –
-
Variables or constants declared outside functions.
-
They can be used by all functions in the program.
🍭Example: Like a rulebook that everyone in the house follows.
Main Function: –
-
Every C program must have a
main()function. -
It is the entry point where execution starts.
🍭Example: Like the main door of your house – everything begins here.
Body of the Program: –
a. Local Declarations –
Variables declared inside functions (like int number = 5;).
They can only be used inside that function.
🍭Example: Like keeping your personal wallet inside your room – only you can access it.
b. Statements / Logic –
The body of the program where actual tasks happen.
Example: printf("Hello, World!"); → displays output.
🍭Example: Like cooking food in the kitchen – the real work happens here.
C. Return Statement –
Used to send a value back to the calling function.
return 0; → indicates successful execution.
🍭Example: Like telling your boss the work is completed successfully.
User-Defined Functions (Optional): –
-
Functions created by the programmer.
-
Helps break large programs into smaller parts.
🍭Example: Like assigning different household chores to different family members.
Code<>
#include <stdio.h> // Header File or Preprocessor
// Global Variable
int mangoes = 10;
// Function Declaration
void greet();
int main() {
// Local Variable
int guavas = 20;
// Logic
printf("Numbers of Mangoes: %d\n", mangoes);
printf("Numbers of Guavas: %d\n", guavas);
// Function Call
greet();
return 0; // End of Program
}
// User Defined Function
void greet() {
printf("Hello from user-defined function!\n");
}
Output: –
Numbers of mangoes: 10
Numbers of Guavas: 20
Hello from user-defined function!
7. First Program in C
Fastest C Program (Hello World)
code<>
#include <stdio.h>
int main() {
printf(“Hello, World!\n”);
return 0;
}
Output: –
Hello, World!
This is the most basic and fast program every beginner runs first.
#include <stdio.h>→ Allows usingprintf.int main()→ Entry point of program.printf("Hello, World!\n");→ Prints text on screen.return 0;→ Ends the program successfully.