Introduction to C Language (Simple English)
C language is a computer programming language. It was created by Dennis Ritchie in 1972
at Bell Labs.
Why is C language important?
• It is one of the oldest and most powerful programming languages.
• Many modern languages like C++, Java, and Python are based on C.
• It is used to build operating systems, games, software, and embedded systems.
Features of C language:
• Simple and easy to learn
• Fast in execution
• Helps understand the basics of programming
• Supports loops, decision making, and functions
Where is C used?
• To make programs in schools and colleges
• To develop parts of operating systems like Windows or Linux
• In companies for system software and hardware-level coding
• Example:
Example:
Program to Print "Hello World" in C
#include <stdio.h> // header file
void main() { // main function starts
printf("Hello World"); // output statement
stdio.h
• Full form: Standard Input Output
• Used for input and output functions
• Examples: printf(), scanf()
conio.h
• Full form: Console Input Output
• Used for keyboard and screen-related functions
• Examples: getch(), clrscr()
What is main() in C?
• main() is the starting point of every C program.
• When we run a C program, the computer starts reading from the main() function.
• It is compulsory in every C program.
Notes:
• main() is a function.
• Inside main(), we write all the code that we want the computer to run.
• Without main(), the program will not work.
printf()
• Used to show/output something on the screen.
• Example: printf("Hello"); → prints Hello
scanf()
• Used to take input from the user.
• Example: scanf("%d", &num); → takes an integer input
Comments in C
Comments are notes written by the programmer inside the code to explain it.
They are ignored by the compiler — they do not affect the program.
Why use comments?
• To explain what the code is doing
• To make the program easy to understand for others (and yourself later!)
Types of Comments in C:
1. Single-line Comment
Starts with //
Example:
// This is a single-line comment
2. Multi-line Comment
Starts with /* and ends with */
Example:
/* This is a
multi-line comment */
Special Characters in C
In C language, special characters are symbols that have a special meaning. They are used in
output, formatting, and logic.
Common Special Characters:
Symbol Name Use in C
\n New Line Moves to the next line
\t Tab Adds a horizontal tab (space)
\\ Backslash Prints a backslash \
\" Double Quote Prints a double quote " "
\0 Null Character Shows end of string
Example:
printf("Hello \n World");
Output:
Hello
World
Keywords in C
What are Keywords?
Keywords are special reserved words in the C language.
They have a fixed meaning and cannot be used as variable names.
They are already defined in the C compiler and used for writing the structure of a program.
Common Keywords and Their Use:
Keyword Meaning / Use
int Used to declare integer variable
float Used to declare decimal number
char Used to declare character variable
if Used for checking condition
else Used with if for alternate condition
while Used to create a loop
for Used to create a loop
return Used to return value from a function
void Means function does not return anything
break To stop a loop or switch
continue Skips one loop cycle and continues
switch Used for multi-way decision making
case Used inside switch
default Used when no switch case matches
do Used in do-while loop
struct Used to create structures
const Used to declare a constant value
sizeof Used to get size of a variable
There are total 32 keywords in C language.
Example (Invalid):
int for = 10; // Error: "for" is a keyword, cannot be used as variablename
Variables in C
What is a Variable?
A variable is a name given to a memory location where we store some data (value).
We can change the value of a variable anytime in the program.
You can think of a variable like a box that holds some value.
Rules for Naming Variables:
1. It must start with a letter (A–Z or a–z) or an underscore _
2. It can contain letters, numbers, and underscores
3. No spaces allowed
4. You cannot use keywords as variable names
5. Variable names are case-sensitive
Example:
int age = 16;
float height = 5.5;
char grade = 'A';
Here:
• age is a variable of type int
• height is a variable of type float
• grade is a variable of type char
ypes of Variables in C
Type of Where it is declared Scope (Where it can Life (When it exists)
Variable be used)
Local Inside a function or Only inside that Created when function
block function/block runs, ends after function
ends
Global Outside all functions Can be used in any Exists from program start
function to end
Static Inside function with Only inside that Keeps its value even after
static keyword function function ends
Constants in C
A constant is a value that does not change during the execution of the program.
It is fixed and cannot be modified once it is defined.
Why use constants?
• To store values that must stay the same (like pi = 3.14)
• To make programs safer and easier to read
What is Data Type in C?
A data type tells the type of data a variable will store in memory.
It helps the computer know how much memory to use and what kind of data (number,
character, decimal, etc.) is being used.
Example:
int age = 15; // stores a number
float pi = 3.14; // stores a decimal number
char grade = 'A'; // stores a character
Basic Data Types in C
DATA TYPE MEANING SIZE (APPROX) EXAMPLE VALUE
INT Integer (whole number) 2 or 4 bytes 5, -10, 100
FLOAT Decimal number 4 bytes 3.14, -5.5
CHAR Single character 1 byte 'A', '9', '#'
DOUBLE Big decimal number 8 bytes 10.123456
Summary:
• Use int for whole numbers
• Use float or double for decimal numbers
• Use char for single letters or symbols
Simple Examples of Basic Data Types
#include <stdio.h>
void main() {
int age = 16; // integer type
float height = 5.7; // float type
char grade = 'A'; // character type
double weight = 55.789; // double type
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Grade: %c\n", grade);
printf("Weight: %.3lf\n", weight);
}
Output:
makefile
CopyEdit
Age: 16
Height: 5.7
Grade: A
Weight: 55.789
Operators in C
Operators are special symbols used to perform operations on variables and values.
Example: +, -, *, /
Types of Operators in C
Type Example Use
Arithmetic +, -, *, /, % For basic math operations
Relational ==, !=, <, >, <=, >= To compare values
Logical &&, `
Assignment =, +=, -=, *=, /= To assign or update value
Increment/Decrement ++, -- To increase or decrease by 1
Bitwise &, ` , ^, <<, >>`
Ternary condition ? true : false Shortcut for if-else
Example: Operators in C
#include <stdio.h>
void main() {
int a = 10, b = 5;
printf("Sum = %d\n", a + b); // Arithmetic
printf("Is a > b? %d\n", a > b); // Relational
printf("Logical AND = %d\n", a && b); // Logical
printf("Before a = %d\n", a);
a++; // Increment
printf("After a++ = %d\n", a);
int max = (a > b) ? a : b; // Ternary
printf("Greater = %d\n", max);
}
Output:
mathematica
Copy
Edit
Sum = 15
Is a > b? 1
Logical AND = 1
Before a = 10
After a++ = 11
Greater = 11
Input Functions in C
Input functions are used to take values from the user.
Common Input Functions:
Function Purpose
scanf() To take input like number, character, etc.
getchar() To take a single character input
gets() To take full string (sentence) input
getch() To take character input without showing it on screen
Example using scanf():
#include <stdio.h>
void main() {
int age;
printf("Enter your age: ");
scanf("%d", &age); // %d tells that we are taking integer input
printf("You are %d years old", age);
}
Why We Use %d, %f, %s, etc.?
These are called format specifiers. They tell C what kind of value we are dealing with.
Format Code Meaning Used For Example Input
%d Integer number 10, 45, -5 scanf("%d", &a)
%f Float number 3.14, 5.0 scanf("%f", &b)
%c Character 'A', 'z' scanf("%c", &ch)
%s String (word) "hello", "rahul" scanf("%s", name)
%lf Double (big float) 15.678901 scanf("%lf", &d)
Output Functions in C
Output functions are used to show (print) result on the screen.
Common Output Functions:
Function Purpose
printf() To print output on screen
putchar() To print single character
puts() To print string and move to next line
Example using printf():
#include <stdio.h>
void main() {
float pi = 3.14;
printf("Value of Pi is: %f", pi); // %f prints float number
}
What is Decision Structure in C?
A decision structure allows the program to make a choice.
For example, if a student’s marks are more than 35, then print “Pass”, otherwise print “Fail”.
In C, we use:
• if
• if-else
• nested if
• else-if ladder
1. if Statement
Syntax:
if (condition) {
// runs if condition is true
}
Example:
int age = 18;
if (age >= 18) {
printf("You can vote");
}
Diagram:
[ Condition ]
|
True | False
-----> Run
Nothing
2. if-else Statement
Syntax:
if (condition) {
// runs if true
} else {
// runs if false
}
Example:
int marks = 30;
if (marks >= 35) {
printf("Pass");
} else {
printf("Fail");
}
3. nested if Statement
It means one if inside another if.
Syntax:
if (condition1) {
if (condition2) {
// code
}
}
Example:
int age = 25;
char gender = 'M';
if (age >= 18) {
if (gender == 'M') {
printf("Adult Male");
}
}
4. else-if ladder
Used when you have more than two conditions.
Syntax:
if (condition1) {
// code
}
else if (condition2) {
// code
}
else {
// default code
}
Example:
int num = 0;
if (num > 0) {
printf("Positive");
}
else if (num < 0) {
printf("Negative");
}
else {
printf("Zero");
}
What is switch-case in C?
• switch is used to select one block of code from many options.
• It is useful when you have many conditions based on a single variable.
Syntax:
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
...
default:
// code if no case matches
}
Example 1: Print Weekday Name
int day = 2;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid day");
}
Example 2: Grade System
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent");
break;
case 'B':
printf("Very Good");
break;
case 'C':
printf("Good");
break;
default:
printf("Try again");
}
Simple Calculator using switch
#include <stdio.h>
int main() {
int num1, num2;
char op;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op); // space before %c to avoid newline issue
printf("Enter second number: ");
scanf("%d", &num2);
switch (op) {
case '+':
printf("Result = %d", num1 + num2);
break;
case '-':
printf("Result = %d", num1 - num2);
break;
case '*':
printf("Result = %d", num1 * num2);
break;
case '/':
if (num2 != 0)
printf("Result = %d", num1 / num2);
else
printf("Cannot divide by zero");
break;
default:
printf("Invalid operator");
}
return 0;
}
What is a Loop in C?
A loop in C is used to repeat a block of code again and again until a condition is true or
false.
Why do we need loops?
• To avoid writing the same code again and again
• To save time and reduce code size
• For tasks like printing 1 to 100, checking multiple values, etc.
Types of Loops in C:
1. while loop
2. do-while loop
3. for loop
1. while Loop
Syntax:
while (condition) {
// code to run
}
Example:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
2. do-while Loop
Syntax:
do {
// code to run
} while (condition);
Example:
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
Note: do-while runs at least once, even if the condition is false.
3. for Loop
Syntax:
for (initialization; condition; update) {
// code to run
}
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
Summary Table:
LOOP TYPE USE WHEN... RUNS AT LEAST ONCE?
WHILE You don’t know how many times to repeat No
DO-WHILE You want code to run at least once Yes
FOR You know how many times to repeat No
What is an Array in C?
An array is a collection of multiple values of the same data type stored in a single
variable.
Example:
int marks[5] = {90, 80, 70, 85, 60};
• int → type of data (integers)
• marks → name of the array
• [5] → total number of elements
• {90, 80, 70, 85, 60} → values stored in the array
Why use arrays?
• To store many values in one variable
• Helps in loops, data storage, and sorting/searching
Accessing Elements:
printf("%d", marks[0]); // prints 90
Index starts from 0.
Practical
1. Print "Hello World"
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}
2. Add Two Numbers (with Input)
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d", a + b);
return 0;
}
3. Area of Rectangle
#include <stdio.h>
int main() {
int l, b, area;
printf("Enter length and breadth: ");
scanf("%d %d", &l, &b);
area = l * b;
printf("Area = %d", area);
return 0;
}
If-Else Conditions
4. Check Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even");
else
printf("Odd");
return 0;
}
5. Find Greater Between Two Numbers
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("a is greater");
else
printf("b is greater");
return 0;
}
6. Positive, Negative, or Zero
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
printf("Positive");
else if (num < 0)
printf("Negative");
else
printf("Zero");
return 0;
}
Switch Case
7. Simple Calculator
#include <stdio.h>
int main() {
int a, b, choice;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("1.Add 2.Subtract 3.Multiply
4.Divide\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Sum = %d", a + b);
break;
case 2: printf("Difference = %d", a -
b); break;
case 3: printf("Product = %d", a * b);
break;
case 4: printf("Quotient = %d", a / b);
break;
default: printf("Invalid choice");
}
return 0;
}
Loops
8. Print 1 to 10 using For Loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++)
printf("%d\n", i);
return 0;
}
9. Sum of 10 Numbers Using Loop
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
printf("Sum = %d", sum);
return 0;
}
10. Table of Any Number
#include <stdio.h>
int main() {
int n;
printf("Enter number: ");
scanf("%d", &n);
for (int i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n*i);
return 0;
}
Print Even numbers from 1 to 20
#include <stdio.h>
int main() {
printf("Even numbers from 1 to 20:\n");
for (int i = 1; i <= 20; i++) {
if (i % 2 == 0)
printf("%d ", i);
}
return 0;
}
Check Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even number");
else
printf("Odd number");
return 0;
}
Patterns
11. Triangle Pattern (Stars)
#include <stdio.h>
int main() {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++)
printf("*");
printf("\n");
}
return 0;
}
12. Triangle Pattern (Numbers)
#include <stdio.h>
int main() {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++)
printf("%d", j);
printf("\n");
}
return 0;
}
13. Reverse Triangle
#include <stdio.h>
int main() {
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= i; j++)
printf("*");
printf("\n");
}
return 0;
}