C Programming Complete Notes
C Programming Complete Notes
•Examples:
•Smartphone App: The app for a smart home system is a program that
lets you control lights or the thermostat from your phone.
•Remote Control for TV: The remote is like a program that sends
instructions to the TV to change channels, adjust volume, or turn on/off.
Example:
•Human-to-Human Communication: When we talk or text, we use
language to share information and ask each other to do things.
NO
❑ Can You Give a Program Directly to a Machine?
Translator Machine
readable output
/complier
(0101010110)
❑ Can You Give a Program Directly to a Machine?
Machine Language:
•Definition: Binary code executed by the CPU.
•Challenges:
• Complex and hard to read.
• CPU-specific (not portable).
• Difficult to debug.
Alternatives:
• Key Aspects:
[Link]: Code structure and rules.
[Link]: Meaning of the code.
[Link] Structures: Ways to store data.
4. Control Structures: Managing code flow (loops, conditionals).
5. Problem-Solving: Implementing solutions with code.
• Benefits:
•Automation: Automate tasks.
•Software Development: Build applications.
•Career Opportunities: Jobs in tech fields.
❑ Why is Learning Programming Important?
2. ATM Programmer:
•What They Do: Develop ATM software, design functions and security
features.
•Skills: Knowledge of programming languages, database management, and
technical skills.
❑ Every Programming Language to must have
Feature
Monitor
CPU
Keyboard
Platform Dependent OS
An operating system that is designed to work only on a specific type of
hardware or platform.
Examples:
Windows (x86/x64), macOS (Apple
hardware), and Android (ARM devices)
Linux.
❑ Windows
Translator .exe
/complier (runnable File)
The C program will compile to .exe file that can be run on Windows.
❑ Linux
Translator .out
/complier (runnable File)
The C program will compile to .out file that can be run on Linux.
❑ list of common file types along with their extensions
and associated software
❑ What is Algorithm:
An algorithm is a step-by-step set of instructions designed to perform a
specific task or solve a particular problem. In computer science, algorithms
are foundational for programming, guiding computers to execute tasks
efficiently and accurately.
•Start
•Take two numbers as input (A and B)
•Add the numbers: Sum = A + B
•Output the result: Sum
•End
❑ What is Syntax:
Syntax is the set of rules that defines the correct structure of code in a
programming language. It specifies how commands, keywords, and symbols
should be arranged.
01001001010
0101010010100
complier machine
readable
Kickstart Your Coding: Installation and Setup Guide
#include <stdio.h>
int main() {
printf(“Coding SeeKho");
return 0;
}
❑ .c vs .h extensions
❑ Recap:
❖ What is a program?
❖ Why do we need to learn programming languages?
❖ Why is it necessary to communicate with machines?
❖ Can we give a program directly to a machine?
❖ What are the components of learning a programming language?
❖ Why is learning programming important?
❖ What is an Operating System (OS)?
❖ What are platform-dependent operating systems?
❖ What’s an IDE, and why use one?
❖ What file extensions are generated when compiling a C program for
different operating systems?
❑ Variable
Example: Example :
y=3x
Email id:
x y=3x
Password: 1 3
2 6
3 9
❑ Variable just like container:
T =2
Sugar Tea Rice
❑ How Variables are Stored in Memory
Identifier
a =10
Store 10
4001
10
Memory Address of a
❑ Key Rules for Identifiers in C:
•Allowed Characters: Identifiers can include letters (A-Z, a-z), digits (0-9), and underscores (_))
•Starting Character: They must start with a letter or an underscore. (e.g., count, _total )
.
•Case Sensitivity: Identifiers are case-sensitive, so Value and value are considered different .
•Reserved Words: Identifiers cannot be keywords (like int, return, for, etc.).
❑ Examples of Valid and Invalid Identifiers
Choose destination
1234
❑ Data Types in c
Signed int
int
Unsigned int
Data Type Typical Size (Bytes) Range (Signed) Range (Unsigned) Example (Signed) Example (Unsigned)
unsigned short x =
short int 2 –32,768 to 32,767 0 to 65,535 short int x = -32768;
65535;
–2,147,483,648 to unsigned int x =
int 4 0 to 4,294,967,295 int x = -2147483648;
2,147,483,647 4294967295;
–2,147,483,648 to
0 to 4,294,967,295
4 (or 8 on some 2,147,483,647 (or long int x = - unsigned long x =
long int (or larger on 8-byte
systems) larger on 8-byte 2147483648L; 4294967295UL;
systems)
systems)
–
unsigned long long
9,223,372,036,854,7 0 to long long x = -
x=
long long int 8 75,808 to 18,446,744,073,709, 9223372036854775
1844674407370955
9,223,372,036,854,7 551,615 807LL;
1615ULL;
75,807
n bit ( int/short/long int/long long int)
2^n -1 2^n-1
Range=-2^(n-1) to 2^(n-1)
Float
float double
long double
Data Type Size (bytes) Approximate Range Example
double b =
double 8 ±5.0 × 10⁻³²⁴ to ±1.7 × 10³⁰⁸
3.141592653589793;
long double c =
±3.4 × 10⁻⁴⁹³² to ±1.1 × 10⁴⁹³²
long double 8 (or 16 on some systems) 3.1415926535897932384
(depends on architecture)
62643383279502884L;
Signed
char
unsigned
Data Type Size (bytes) Range Example
–128 to 127 (or 0 to
char 1 255 if unsigned by char ch = 'A';
default)
signed char sch = -
signed char 1 –128 to 127
42;
unsigned char uch =
unsigned char 1 0 to 255
250;
Format Specifier Description Example
%d Signed integer printf("%d", -42);
%u Unsigned integer printf("%u", 42);
%f Floating-point number printf("%f", 3.14);
%c Single character printf("%c", 'A');
%s String of characters printf("%s", "Hello");
Hexadecimal integer
%x printf("%x", 255);
(lowercase)
%o Octal integer printf("%o", 8);
printf("%p",
%p Pointer address
(void*)&var);
printf("100%%
%% Percent sign
done");
❑ printf – printing output
• You provide a format string, which can include text and format
specifiers (like %d for integers, %f for floats, etc.) that tell printf how to
display the data.
•You need to provide format specifiers and variable addresses where the input data should
#include <stdio.h>
int main() {
int a = 5;
printf("a = %d\n", a);
return 0;
}
- A) a = 5
- B) a = %d
- C) a = a
- D) Error
#include <stdio.h>
int main() {
int x = 10;
printf("%d %d %d\n", x, x++, ++x);
return 0;
}
- A) `10 11 11`
- B) `10 10 11`
- C) `10 11 12`
- D) Undefined behavior
5. Which of the following is a correct way to take an integer input from the user?
- A) `scanf("%d", age);`
- B) `scanf("%d", &age);`
- C) `scanf("%f", &age);`
- D) `printf("%d", &age);`
10+20=30
operands
❑ Operators:
Operators are symbols that tell the compiler to perform specific
operations, such as mathematical, logical, or bitwise operations, on
data values. They act on variables or values to produce a result.
❑ Operands:
Consider c = a + b * 2;
•Operators: =, +, *
•Operands: a, b, 2, and `c
❑ Types Of Operators:
OPERATORS
#include<stdio.h>
20
void main(){
10
int a=10;
a= 20;
}
a=b+c; valid
b=10 c=20
b+c=a;invalid
a=30
10=a
Lvalue=Rvalue
10=20
LHS
32
3+5*4
23
Precedence order:
% Modulas Operator
14%5 =4
12%2=0
2%5=2
3%7=3
int result = 20 / 5 * 2 % 3;
153
Print last digit=153%10=3;
Print last two digit=153%100=53;
153
Print 2nd last digit=(153/10) %10=5;
❑ Concept of Swapping
Rice sugar
Swapping of number with
temp variable
int main() {
int a, b, temp;
temp = a;
a = b;
b = temp;
return 0;
}
Swapping of number without
temp variable
int main() {
int a, b;
a = a + b;
b = a - b;
a = a - b;
return 0;
}
Average of marks for different subjects :
•Start
•Declare variables to store the sum of numbers and the
Enter marks for five subjects:
average. Subject 1: 85
•Input 5 numbers.
•Calculate the sum of the 5 numbers.
Subject 2: 90
•Calculate the average using the formula: Subject 3: 78
Subject 4: 88
Subject 5: 92
Total marks: 433.00
•Display the average. Average marks: 86.60
•End
#include <stdio.h>
int main() {
int a, b, c, d, e;
float avg;
avg = (a + b + c + d + e) / 5.0;
return 0;
}
Celsius to Fahrenheit :
Programming with
•Start Real-Life Example:
•Input: Get the temperature in Celsius Celsius to Fahrenheit
from the user. Conversion
•Convert: Apply the formula to convert
Celsius to Fahrenheit: •Input: Doctor measures body
temperature in Celsius (e.g.,
37°C).
•C is the input temperature in Celsius. •Convert: Use the formula
•F is the converted temperature in to convert Celsius to Fahrenheit.
Fahrenheit. •Output: 37°C = 98.6°F.
•Output: Display the converted
temperature in Fahrenheit.
•End
#include <stdio.h>
int main() {
float celsius, fahrenheit;
return 0;
}
Temperature conversion :
Power Calculation
#include <stdio.h>
#include <math.h>
int main() {
double base, exponent, result;
return 0;
}
#include <stdio.h>
int main() {
int base, exponent;
long long result = 1;
return 0;
}
Simple Interest (SI)
int main() {
float P, R, T, SI;
SI = (P * R * T) / 100;
return 0;
}
Compound Interest (CI)
#include <stdio.h>
#include <math.h>
int main() {
float P, R, T, A, CI;
return 0;
}
Statement/expressions
Statement/expressions
Relational operators(Binary Operators)
Used to compare values, resulting in a Boolean value (1 for true, 0 for false).
Relational operators
Relational operators
Relational operators
Relational operators
a b a &&b output
T T T 1
T F F 0
F T F 0
F F F 0
[Link] OR(||) (CHOICE)
a || b Non –zero(true)
a b a ||b output
T T T 1
T F T 1
F T T 1
F F F 0
[Link] NOT(!)
Laddu tabhi milenge jab aapne padhai nahi chhodi hai, warna
kuch nahi milega!
!a output
!T F
!F T
Short –circuit evaluation
a && b zero(0)
a || b 1
||
||
1. Age Verification
for Voting
•Start.
•Declare age.
•Prompt and read the user's age.
•Set canVote to 1 if age is between 18 and
100; otherwise, set it to 0.
•Print "Yes" if canVote is 1, otherwise print
"No."
•End.
#include <stdio.h>
int main() {
int age;
return 0;
}
year % 100 != 0 or year % 400 == 0
Logical operator ka use
kaha hai?
||
1. Leap year checker year % 4
== 0
•Start.
•Declare year.
•Prompt and read the user's input for the year.
•Set isLeapYear to 1 if:
year % 100 != 0 or year % 400 == 0
•year is divisible by 4, and
•(year is not divisible by 100 or is divisible by 400).
•Print "Yes" if isLeapYear is 1, otherwise print
"No."
•End.
#include <stdio.h>
int main() {
int year;
return 0;
}
year % 100 != 0 or year % 400 == 0
Modify Operator
Increment the Decrement
value of a the value of a
variable by 1 variable by 1
||
Increment(++) Decrement(--)
a=5
||
1. Pre- Increment(++a):(पहले बढ़ाओ ++, फिर print करो)
a=a+1 (a++)
a=6
year % 100 != 0 or year % 400 == 0
Post –increment(a++)
||
1. Post-increment(a++):(पहले print करो, फिर बढ़ाओ ++) a=5
a++
a=a+1
a=6 result=5
year % 100 != 0 or year % 400 == 0
Increment Operator(++)
1. Pre- Increment(++a):(पहले
|| Post-increment(a++):(पहले
बढ़ाओ ++, फिर print करो) print करो, फिर बढ़ाओ ++)
i=10
++i=i+1=11
year % 100 != 0 or year % 400 == 0
Increment Operator(++)
1. Pre- Increment(++a):(पहले
|| Post-increment(a++):(पहले
बढ़ाओ ++, फिर print करो) print करो, फिर बढ़ाओ ++)
j=i++
j=++i
j=i
i=i+1
i=i+1
j=i
year % 100 != 0 or year % 400 == 0
Pre –decrement(--a)
||
1. Pre-decrement(--a):(पहले घट़ाओ --, फिर print करो) a=5
--a
a=a-1
a=4 result=4
year % 100 != 0 or year % 400 == 0
Post –Decrement(a--)
||
1. Post-decrement(a--):(पहले print करो, फिर घट़ाओ --) a=5
a--
a=a-1
a= 4 result=5
year % 100 != 0 or year % 400 == 0
C standard rule
❑ Ek variable
||ko ek hi baar increment/decrement karo in Result
one line. Varna side effects aayenge. vary
complier
to
complier
Why use
Number System Binary
Number
system ?
[Link] Number System: 10 symbols (0,1,2,3,4,5,6,7,8,9)
10,11,12,13,14,15,16
[Link] Number System: 16 symbols(0 -9 ,A,B,C,D,E,F)
Decimal Number System
Computers use binary because it's simpler and more efficient to
represent data with only two states (0 and 1) using electrical signals.
Binary Number System
Decimal to Binary
Binary to Decimal
Octal Number System:
Decimal to Octal:
Octal to decimal:
Hex-decimal Number System:
Decimal to Hexa-decimal :
Hexa-decimal to decimal:
Bitwise Operator:
[Link] OR(|)
[Link] AND(&&)
[Link] XOR(^)
[Link] not(~) Unary Opeartor
a ~a
0 1
1 0
Bitwise OR(|):
Dono me se eke bhi one hai to or karne per output 1hi aayga
a b a|b
0 0 0
0 1 1
1 0 1
1 1 1
Bitwise OR(|):
0011 (binary of 3)
1011(binary of 11)
1011
Bitwise AND(|):
Dono me se eke bhi ZERO hai to AND karne per output 0 hi aayga
a b a&&b
0 0 0
0 1 0
1 0 0
1 1 1
Bitwise AND(&&):
0011 (binary of 3)
&&
1011(binary of 11)
&&
1011(binary of 11)
a b a^b
0 0 0
0 1 1
1 0 1
1 1 0
Bitwise X-OR(^):
0011 (binary of 3)
1011(binary of 11)
1000
Bitwise left shift(<<):binary operator
c=10<<1
c=20
C=a<<b;
C=a*2^b;
Bitwise left shift(<<):
0 0 0 0 0 0 1 0 1 0
10<<1
Popped
out
0 0 0 0 0 1 0 1 0 0
Empty
space
Bitwise right shift(>>):binary operator
c=10>>1
c=5
C=a>>b;
C=a/2^b
Bitwise right shift(>>):
Popped
out
0 0 0 0 0 0 1 0 1 0
10>>1=10/2^1
0 0 0 0 0 0 0 1 0 1
Empty
space
How to store Negative Number in computer Memory?
Signed magnitude
0,1
+7=111
1 0 0 0 0 1 1 1
-7=111
+ve=0
Signed bit 7 bit
magnitude
-ve=1
2`s complementation
1)+ve :as it is
0001
1`s complementation=1110
2)-ve :2`s complementation
+ 1
2`complementation= 1111
Programs
int main() {
int num;
return 0;
}
#include <stdio.h>
int main() {
int num;
return 0;
}
Ternary operator(? :)
True
false
c=b
c=a>b?a:b;
a>b 15>20
True
C=T?a:b
C=a
c=a>b?a:b;
Programs
num
1. Finding the Absolute Value of a Number
F
num <0
-(num)
Assigning Grades Based on Marks
A B C D
4. Finding the Smallest of Three Numbers
•Example code:
void main() {
S1;
S2;
S3;
S4;
}
•Default flow: Sequential
•Looping: Executes multiple times (e.g., S1 → S2 → S3
in a loop)
➢ Control Flow Statements
[Link] Statements:
•if
•if-else
•if-else if- else
•if-else if
•Switch Statement
[Link] Statements (Repetition):
•for
•while
•do-while
[Link] Statements:
•continue
•break
•exit
•return
JK ka concept
Agar yeh sach hai, toh yeh kaam karna zaroori hai!
➢ Selection Statement ➢ Conditional Execution:
c
void main() {
If Statement S1;
S2;
•Default Behavior: Sequential if (condition) {
S3;
execution S4;
Example: S5;
}
void main() {
S1;
S6;
S2; S7;
S3; S8;
S4; }
S5;
S6; •If condition is true:
S7;
S8; Execute S3, S4, S5
}
•Then continue with S6, S7,
S8
Syntax
Syntax of if Statement in C Single Statement Case:
if (condition/expression)
if (condition/expression) { S1;
S1;
S2; if (condition/expression) {
S3; S1;
}
}
Program: Eligibility Checker
Program: Number Checker
Program: Student Result Checker
Program: Bank ATM Simulation
If-Else Statement
structure
if (expression) { if (chai_garam_hai) {
// Chai garam hai, toh piyo.
// Code block when the expression is true } else {
} // Chai thandi hai, toh usse
else { garam karo.
// Alternate block of code }
}
JK ka Concept
If-Else Statement
S1;
S2; Flow Sequence:
[Link] the condition is True: S1 → S2 → S3 → S4
if (expression/condition) { → S7 → S8
S3;
S4; [Link] the condition is False: S1 → S2 → S5 → S6 →
S7 → S8
}
else {
S5;
S6;
}
S7;
S8;
Programs:
#include <stdio.h>
int main() {
int num;
if (num % 2 == 0)
printf("%d is Even.\n", num);
else
printf("%d is Odd.\n", num);
return 0;
}
#include <stdio.h>
2. Simple ATM Withdrawal System
int main() {
int balance = 1000;
(C Program)
int amount;
if (amount <= 0) {
printf("Invalid amount!\n");
}
else if (amount > balance) {
printf("Insufficient balance!\n");
}
else {
balance -= amount;
printf("Withdrawal successful!\n");
printf("Remaining Balance: %d\n", balance);
}
return 0;
}
Syntax
if (expression/condition)
{
// Code 1
}
else if (expression2/condition)
{
// Code 2
}
else
{
// Code 3
}
1. Grade Calculator
Switch case
Switch-Case:
•Works like a menu, where specific
tasks are performed based on your
choice.
•Each case handles a different option.
•default runs when no option matches.
Break:
•Used to exit a block once the task is completed.
•Mostly used in switch and loops.
Continue:
•Skips the current iteration in a loop and moves to the next step.
•It bypasses the remaining code of the current iteration and proceeds to the next
one.
For loop
Pattern understand kara
anni tyala loop madhe
chalwa
loop
Loop Break
Condition
Count++
count=count+1
Coding seekho
increment
initilization
Coding seekho
Print all even number upto n
print a to z
int main() {
int n;
printf("Enter value of n: ");
scanf("%d", &n);
for(int i = 2; i <= n; i += 2) {
printf("%d ", i);
}
return 0;
}
#include <stdio.h>
int main() {
return 0;
}
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
return 0;
}
#include <stdio.h>
int main() {
return 0;
}
Table Printing
6 X 1=6
6 X 2=12
6 X3=18
6X4=24
6X5=30
6X6=36
6X7=42
6X8=48
6X9=54
6X10=60
x x i
n=6(constant)
#include <stdio.h>
int main() {
int n;
printf("Enter a number to print its table: ");
scanf("%d", &n);
return 0;
}
Power of number:
n=4,power=3
4^3=4X4X4
Sum of n natural
numbers=n(n+1)/2
Sum=0;
Sum=sum+i
Sum of square of n natural number=n(n+1)(2n+1)/6
end
start
Prime numbers:
1 or n
1)"If a number n is divisible by any number
between 2 and n−1, then it is not a prime number."
fibonacci 0, 1 ,1 ,2 ,3 ,5 ,8,13,21,34
+
Logic
Last Prev Curr
0 1 1
1 2 3
2 3 5
#include <stdio.h>
int main() {
int n, a = 0, b = 1, next;
return 0;
}
Star pattern
*****
Nested for loop=>loop ke ander loop
printf(“*”);
Printf(“\n”);
***** Star pattern
***** Steps for Pattern Printing :
*****
[Link] Banao: Rows = i, Columns = j.
*****
***** 2.i=1 pe j dekho aur i=n pe bhi.
***** i=3 * * * * *
i=4 * * * * *
*****
i=5 * * * * *
i=1 j=1,2,3,4,5
Step 2
i=2 j=1,2,3,4,5
i=3 j=1,2,3,4,5
i=4 j=1,2,3,4,5
i=5 j=1,2,3,4,5
***** Step 3 i=1 j<=5
***** i=2 j<=5
***** i=3 j<=5
i=4 j<=5
*****
i=5 j<=5
*****
Step 4 j<=5
*****
*****
*****
*****
***** condition
columns
10 10 10 10 10 Step 1 j=1 j=2 j=3 j=4 j=5 Number
i=1 pattern
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 i=2 10 10 10 10 10
rows
10 10 10 10 10 i=3 10 10 10 10 10
10 10 10 10 10 i=4 10 10 10 10 10
i=5 10 10 10 10 10
i=1 j=1,2,3,4,5
Step 2
i=2 j=1,2,3,4,5
i=3 j=1,2,3,4,5
i=4 j=1,2,3,4,5
i=5 j=1,2,3,4,5
10 10 10 10 10 Step 3 i=1 j<=5
10 10 10 10 10 i=2 j<=5
10 10 10 10 10 i=3 j<=5
10 10 10 10 10 i=4 j<=5
10 10 10 10 10 i=5 j<=5
Step 4 j<=5
10 10 10 10 10
10 10 10 10 10
10 10 10 10 10
10 10 10 10 10
10 10 10 10 10
condition
columns
Step 1 Number
j=1 j=2 j=3 j=4 j=5
i=1 pattern
1 1 1 1 1
11111
i=2 2 2 2 2 2
22222 rows
33333 i=3 3 3 3 3 3
44444 4 4 4 4 4
i=4
55555
i=5 5 5 5 5 5
i=1 j=1,2,3,4,5
Step 2
i=2 j=1,2,3,4,5
i=3 j=1,2,3,4,5
i=4 j=1,2,3,4,5
i=5 j=1,2,3,4,5
Step 3 i=1 j<=5
i=2 j<=5
11111
22222 i=3 j<=5
33333 i=4 j<=5
44444
i=5 j<=5
55555
Step 4 i ko print
karo
11111
22222
33333
44444
55555
columns
Step 1
j=1 j=2 j=3 j=4 j=5
i=1 1 2 3 4 5
12345 Number
i=2 1 2 3 4 5 pattern
12345 rows
12345 i=3 1 2 3 4 5
12345 1 2 3 4 5
i=4
12345
i=5 1 2 3 4 5
i=1 j=1,2,3,4,5
Step 2
i=2 j=1,2,3,4,5
i=3 j=1,2,3,4,5
i=4 j=1,2,3,4,5
i=5 j=1,2,3,4,5
Step 3 i=1 j<=5
i=2 j<=5
11111
22222 i=3 j<=5
33333 i=4 j<=5
44444
i=5 j<=5
55555
Step 4 j ko print
karo
11111
22222
33333
44444
55555
Number
pattern
54321
54321
54321
54321
54321
columns
Step 1
j=1 j=2 j=3 j=4 j=5
i=1 1 4 9 16 25
1 4 9 16 25 i=2 1 4 9 16 25
1 4 9 16 25 rows
1 4 9 16 25 i=3 1 4 9 16 25
1 4 9 16 25 i=4 1 4 9 16 25
1 4 9 16 25
i=5 1 4 9 16 25
i=1 j=1,2,3,4,5
Step 2
i=2 j=1,2,3,4,5
i=3 j=1,2,3,4,5
i=4 j=1,2,3,4,5
i=5 j=1,2,3,4,5
Step 3 i=1 j<=5 Ascending
square pattern
i=2 j<=5
1 4 9 16 25
1 4 9 16 25 i=3 j<=5
1 4 9 16 25 i=4 j<=5
1 4 9 16 25
i=5 j<=5
1 4 9 16 25
Step 4 j *j ko
print karo
1 4 9 16 25
1 4 9 16 25
1 4 9 16 25
1 4 9 16 25
1 4 9 16 25
alphabets
pattern
columns
Step 1
j=1 j=2 j=3 j=4 j=5
i=1 a a a a a
aaaaa i=2 b b b b b
bbbbb rows
ccc c c i=3 c c c c c
ddddd i=4 d d d d d
eeeee
i=5 e e e e e
i=1 j=1,2,3,4,5
Step 2
i=2 j=1,2,3,4,5
i=3 j=1,2,3,4,5
i=4 j=1,2,3,4,5
i=5 j=1,2,3,4,5
Step 3 i=1 j<=5
i=2 j<=5
i=3 j<=5
aaaaa i=4 j<=5
bbbbb
ccc c c i=5 j<=5
Ascending
ddddd
square pattern
eeeee
i=1 a (5 times)
i=2 b(5 times)
i=3 c(5 times)
i=4 d(5 times)
i=5 e(5 times)
aaaaa
1) row = 1 bbbbb
2) row <= 5 ccc c c
3) name = 'a' + (row - 1) ddddd
= 'a' + (2 - 1) eeeee
= 'a' + 1
= 'b' (ASCII no: 97 + 1 = 98)
i=1 j=1
Step 2
i=2 j=1,2
i=3 j=1,2,3
i=4 j=1,2,3,4
i=5 j=1,2,3,4,5
Step 3 i=1 j<=i
* i=2 j<=i
** i=3 j<=i
***
i=4 j<=i
****
***** i=5 j<=i
Step 4 j<=i
1
12
123
1234
12345
Update condition
Break
condition
Initialization
While loop
syntax
i=1(initialization)
Loop break
condition
Update conditions
do while loop
While loop
do while loop
1)Initialize 1)Initialize
2)break 2)update
3)update 3)break
Syntax of do while
1)Initialize
2)Update
3)break
Sum=0 Sum of N natural numbers
i=1
Sum= sum+ i
break continue
// code block
return value; // optional
}
• return_type: function kis type ka value return karega (jaise int, float, void, etc.)
• function_name: function ka naam
• parameters: input values (arguments)
• return: result return karta hai (agar void nahi hai)
Example 1: Simple Function with No Return
Types of Function
1. Library Functions
(Inbuilt Functions) 2. User-Defined Functions
1. Library Functions (Inbuilt Functions)
Definition:
Yeh functions C ke standard library mein pehle se defined hote hain. Hum
unhe direct use kar sakte hain, unka code likhne ki zarurat nahi hoti.
Example:
Definition:
Use:
Code ko modular, reusable aur manageable banata hai.
Parameter vs Argument (Basic Difference)
Parameter Argument
Function call ke waqt use
1️⃣ Function definition mein use hota hai
hota hai
Ek variable hota hai jo value accept Ek actual value hoti hai jo
2️⃣
karta hai function ko di jati hai
3️⃣ Placeholder hota hai Real data hota hai
4️⃣ Syntax: void add(int a, int b) Syntax: add(5, 10);
• Parameter = Function ko bolte ho: "Mujhe do number chahiye."
• Argument = Jab function call hota hai: "Yeh lo 5 aur 10, add karo."
Types of User-Defined Functions in C
Explanation: Function koi value return karta hai, lekin koi argument nahi
leta.
4. With Arguments and With Return Value
Explanation: Sabse common type – values pass bhi hoti
hain aur result return bhi hota hai.
Function Declaration
(Prototype):
* = Value-at/Dereference
Operator
Syntax:
Data_type *var_name
Value at
address
operator
x
Int x=10 10
1000
1004
x
Int x=10 18
1000
Int y=&x
y
1000
*y=18
1004
x
5
1000
y
1000
10044
Z
1004
1008
1)Addition of two number using pointer
x a
1000 10 1000
2000 b
y 20 1004
1004
2004
Swapping of number with
temp variable using pointer
Before swapping
x=&a 1000 1004
a=10 b=20
y=&b
Afther swapping
Call by value
Call by reference
Concept "Jo bhi humne pass kiya, usme koi change nahi hoga."
10
1000
a++ a
10
2)call by reference
Concept:
"Jo values humne pass ki, unme bhi changes honge."
x
10
1000
a++ a
1000
storage classes
Static extern
1. Automatic (auto): "Temporary local hero – Block khatam, kaam khatam!"
•Default Class: Variables declared inside a function without any storage class
specifier are treated as auto.
•Scope: Local to the block or function in which it is declared.
•Lifetime: Exists only during the execution of the block or function.
•Keyword: auto (rarely used explicitly, as it is implicit).
•Example:
void func() {
auto int x = 10; // Same as 'int x = 10;’
printf("%d\n", x);
}
2. Register "Fastest memory racer – Turbo mode enabled!"
•Keyword: register.
•Notes:
•The compiler may ignore this request if no registers are available.
•Pointers to register variables are not allowed.
ALU(CPU)
5
5 2 x
0.1 sec +
7 2
y
processor 7
z
4 sec
RAM
3. Static: "Lifelong yaari – Value yaad rakhta hai!"
•Purpose:
•Scope:
a=1
b=1,2,3
a=1 b=1
a=1 b=2
a=1 b=3
4. External (extern)
"Global mohalla superstar – Sab jagah dikhai deta hai!"
Word 2
Word 3
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
return 0;
}
Printing JK 10 times
JK n==0
JK n-1
Sum of digits
1=>1
n>=0 n<10 2=>2
3=>3
125 1+2+5=8
n%10+sumofdigits(n/10)
Power of
number
#include <stdio.h>
void printNumbers(int n) {
if (n > 0) {
printNumbers(n - 1); // Recursive call
printf("%d ", n); // Print the number
}
}
int main() {
int N = 5;
printNumbers(N); // Output: 1 2 3 4 5
return 0;
}
Array
11 12 13 14 15
index 0 1 2 3 4
Declaring an Array
Example:
int numbers[5]; // Declares an array of 5 integers
Initializing an
Array
10 20 30 40 50
0 1 2 3 4
Common Operations
Traversal: 11 12 13 14 15
0 1 2 3 5
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
Input from User:
0 1 2 3 4
int sum = 0;
int arr[3][3];
// Declares a 3x3 array (3 rows, 3columns)
rows
// Static initialization
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Dynamic input
int arr[2][3];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &arr[i][j]);
}
}
Accessing
Elements
0 1 2
0
1 23
2
Simple Matrix Print Code in C
cols
0 1 2
0 1 2 3
1 4 5 6
rows
2 7 8 9
Enter number of rows: 3 matrix as input from the user and
Enter number of columns: 3 prints it
Enter elements of the matrix:
Enter element at position [1][1]: 1
Enter element at position [1][2]: 2
Enter element at position [1][3]: 3
Enter element at position [2][1]: 4
Enter element at position [2][2]: 5
Enter element at position [2][3]: 6
Enter element at position [3][1]: 7
Enter element at position [3][2]: 8
Enter element at position [3][3]: 9
Matrix B
C=A+B
Strings
Char str[20]={“AMIT”}
A M I T
Char a[10];
a=“AMIT” not allowed in c
Methods to Take String Input
#include <stdio.h>
int main() {
char str[100]; // Declare a string with enough space
printf("Enter a string: ");
scanf("%s", str); // Reads input until a space or newline
printf("You entered: %s\n", str);
return 0;
}
Warning: Avoid using gets in modern C programs. It has been removed in C11.
3. Using fgets (Recommended)
fgets is the safest way to take string input in C as it prevents
buffer overflows.
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Reads up to sizeof(str) - 1 characters
printf("You entered: %s", str);
return 0;
}
String Predefined function
3. Concatenating Strings
4. Comparing Strings
0 1 2 3 4 5 6 7 8
t
len = strlen(a); ‘a’ ‘b’ ‘h’ ‘i’ ‘s’ ‘h’ ‘e’ ‘k’ ‘\0’
t = a[i];
a[i] = a[len - i - 1];
a[len - i - 1] = t;
}
Palindrome
Example 1:
Input: madam
String: m a d a m
Index: 0 1 2 3 4
Step-by-Step Comparisons:
[Link] a[0] (m) with a[4] (m) → Match
[Link] a[1] (a) with a[3] (a) → Match
[Link] a[2] (d) with a[2] (d) → Match
Result: All characters match → Palindrome
Example 2: Input: hello
String: h e l l o
Index: 0 1 2 3 4
Step-by-Step Comparisons:
[Link] a[0] (h) with a[4] (o) → Mismatch
Result: Mismatch found → Not a Palindrome
Static memory allocation
int main() {
int arr[5]; // 5 integers ke liye memory compile time par allocate
printf("Example\n");
return 0;
}
Advantages:
• Fast execution (kyunki memory pehle hi allocate ho gayi hai).
• Memory management simple hota hai.
Disadvantages:
• Memory ka size fix hai, agar zyada chahiye to badal nahi sakte.
• Agar extra memory allocate kar di to waste ho sakti hai.
Dynamic memory allocation
Key Points:
2000 6
1000 11 22 44
x
ptr = (castType*) calloc(number_of_blocks, size_of_each_block);
realloc()
y
ptr = (castType*) realloc(old_ptr, new_size_in_bytes);
free()
free(pointer);
Syntax
struct StructureName {
dataType member1;
dataType member2;
// ...
};
Person
Person
A union is similar to a structure, but it uses a shared memory for all its
members. This means only one member can hold a value at any given
time.
Syntax:
union UnionName {
dataType member1;
dataType member2;
// ...
};
Union Data
LCM,HCF(GCD)
LCM ka full form hai "Lowest Common Multiple", jiska matlab hota hai:
Sabse chhota aisa number jo diye gaye sabhi numbers se poori tarah
divide ho jaye (ya jisme sabhi numbers ka multiple aa jaye).
4 6
8 12
12 18
16 24
20 30
24 36
28 42
32 48
36 54
40 60
44 66
48 72
52 78
56 84
60 90
LCM=12
(sabse chota common multiple)
Highest common multiple(HUM BATA NAHI
SAKTE)
8%2=0
8/2=4
4 6
for(int i=1;i<=100;i++){
printf(“Lcm is %d”,i);
break;
}
for(int i=1;i<=100;i++){
printf(“Lcm is %d”,i);
break;
}
X=1000 Y=2000
for(int i=1;i<=100;i++){
printf(“Lcm is %d”,i);
break;
}
Inka 1 se check karna start
karoge to time waste hoga
Case 1: Common factor hai → LCM < multiplication
Example: 4 and 6
• Multiplication = 4 × 6 = 24
• LCM = 12 (chhota kyunki 4 aur 6 me common factor 2 hai)
True
false
c=b
c=a>b?a:b;
for(int i = x>y ? x:y;i<=x*y;i++){
printf(“Lcm is %d”,i);
break;
}
Max=4>6?4:6
HCF ka full form hai Highest Common Factor, jise GCD (Greatest
Common Divisor) bhi bolte hain.
Ye wo sabse bada number hota hai jo dono (ya zyada) numbers ko
poori tarah divide karta hai bina remainder ke.
Example:
Find HCF of 12 and 18
Factors of 12: 1, 2, 3, 4, 6, 12
Factors of 18: 1, 2, 3, 6, 9, 18
Common factors: 1, 2, 3, 6
HCF = 6
• Factors of 4 → 1, 2, 4
• Factors of 6 → 1, 2, 3, 6
• Common factors → 1, 2
HCF = 2
b=1%10=1
S=5+1=6
X=1/10=0
(loop break condititon)
Armstrong number
153
a=153%10=3
y=x/10=153/10=15
b=y%10=15%10=5
c=15/10=1
x=153
t=153%10=3,5,1
s=s+(t*t*t)=0+(3*3*3)=27
=27+(5*5*5)=152
=152+(1*1*1)=153
y=y/10
y/10=153/10=15
=15/10=1
=1/10=0
x==s
b=123%10=3
Sum Of Digits
S=0+3=3
X=123/10
b=1%10=1
S=5+1=6
X=1/10=0
(loop break condititon)
b=123%10=3 Reverse of Number
S=0*10+3=3
X=123/10
b=1%10=1
S=32*10+1=321
X=1/10=0
(loop break condititon)
b=121%10=1 palindrome number
S=0*10+1=1
X=123/10
b=1%10=1
S=12*10+1=121
X=1/10=0
(loop break condititon)
File handling
File Handling in C:
File handling in C allows us to create, read, write, and manipulate files
on the system. The standard library <stdio . h> provides functions to handle
files.
Basic File Operations:
Char[]={“JK”}
fp
[Link]
buffer
100
[Link]
File Handling Functions in C