C Complete Notes
C Complete Notes
EDG3
C Programming Noks
by Code With Harry
What is
Programming?
Computer Programming is a medium for
us to communicate with computers. Just
like we use "Hindi"' or 'English' '
to Communicate with each other,
programming is a way for us to deliver
our instructions to the Computer... What
is Co
C is a programming
language.
C is one of the oldest and
finest programming languages.
C was developed by Dennis
Ritchie at AT&T's Bell Labs, USA in
1972.
Uses of C
of
C Language is used to program a
wide variety of System's. Some of
the uses of C are as follows: "
Care
EDG3
Chapter 1: Variables,
Constants & Keywords
Variables
A variable is a Container which stores
a Valsse In Kitchen, we have
containers storing Rice, Dal,
Sugar etc. Similar to that
Variables in C stores. Value of a
constant. Example:
a = 3;
b = 4.7;
C = 'A';
// a is
assigned "3""
11 b is assigned "4.7": 11. C is
assigned 'A' indi
Rules for naming
variables in C
1. First character must be an alphabet
or underscore (-)
27 No Commas, blanks
allowed.
EDG3
Types of
constants
Primarily, there are three
types of constants
ހ
- 1967
12 Integer Constant ! 6, 7, 91.
627
→
Key
words
-322·1 2·57·0%
'a' '$' '@' (Must be enclosed within
~ Single inverted
Commas)
Const
Short
Continue
signed
float Unsigned dis
Void
Static
if
while
fox
default Size of goto il
volatile
Our First C
Program
#include
<stdio.h>
int main () {
printf ("Hello, I am learning
( with Harry");
return 0;
子
File: first. C
EDG3
Basic Structure of a C
Program
All C programs have to follow a
basic structure AC program starts
with a main function and executes
instructions present inside it.
Each instruction is terminated with a
semicolon (;)
EDG3
first.C
in VS Code
C
Compiler
first
exe
cxc]
500 gcc
дас
A compiler is a computer
program which converts a C
program in to machine language so
that it can be easily understood
by the computer
A C program is written in
plain text·
а
This plain text is combination of
Instructions in ia particular
sequence. The compiler performs
Some basic checks and finally
converts the
program into an
executable.
Library
functions
C language has a lot of valuable
library functions which is used to
carry out Certain tasks. for instance
printf function is used to
Values on the screen
printf ("This
is %d", i);
%d for integers If for
real values % C
for characters
prin
t
EDG3
Types of
Variables
12 Integer variable → int
a = 3; 22 Real variables
32 Character
Variably
7 Wrong as 7-
7 is real
→ int a = 7·7; float a =
7·7;
Chai a = 'B';
Receiving input
from the User
In order
to take input from the user
and assign it to a variable, we use
scanf function
Syntax for using
scanf :
Scanf("%d",
&i):
1 This & is
important!
& is the "address of" operator and it
means that the supplied value should be
copied to the address which is
"indicated by variable i
EDG3
EDG3
Type declaration
Instruction
int Ai
float bi
Other
Variations:
int a, b, c, d;
a = b = 6 = d = 30;
Mala
teying to
use a before.
defining it.
→ Value of 9 b, c &d
will
be 30 each.]
Arithmetic Instructions I
operators
int
i
=3
32
* 2 +1
2)+
✓
opera
nds
URBAN
EDG3
Operands can be int
/flat ek..
+ - * / are arithmetic
operators
intz, Z =
int b = 2, C= 3;
b*C; ✓ legal
int Z;
%
-
%
%
%
bac = 2; x Illegal (Not
allowed)
→ Modular division
operator
%
5% 2 = 1
Note -
Returns the remainder
-5% 2 = -1
Ni
17 No operator is assumed
to be present
int i= ab
→ Invalid
int i= a*b → Valid
27 There is no
operator to perform
exponentiation in C However we
can use pow (2,y) from
<math.h>(More
Jatu
Type
Conversion
An Arithmetic
operation between
Int and Int Int
and float Float
and float
Int names
→
Float
Float
5·0/22·5
URBAN
EDG3
5/2 → 20
Important!
!
2/5
2·0/5 → 0.4
Note =
crot wisdo
Q
5/11
float
a=
8;
demoted to 3(int) because a is
not able to store
floats.
a will Store 8.0 winded
18→ 8.0 (promotion to
float
XY
Quick
Quiz: a
int k = 3.0/9
3.0/9 -
༠
Value of K? and
why?
0.333. but since k is an
int
it cannot store feats
& value 0:33 is demoted
to 0.
EDG3
Operator
precedence In C
3 #2 -
8y is (3x) - (8y) or
3(2-8 4)?
In ( language simple
mathematical rules like
BODMAS,
no longer
applies.
The answer to the above question
is provided
by operator precedence &
associativity. Operator precedence:
The following table lists
the operator priority
in C Priority Operators
* /%
21
nd
2
3rd
+
Operators of higher
priority are evaluated
first
absence
of
parenthesis
in the
Operator
Associativity ÷
When operators
of equal
priscity are present in an
expression, the tie is taken care
of by associativity.
x* 4/z => (x * y)
/z
x/ y * z
= (x/y) * Z
EDG3
Control
Instructions
wolded.
Determines the flow of Control in
a program Four types of
control Instructions in Care:
17 Sequence Control
Instruction
27 Decision Control
Instruction od 32 Loop
Control Instruction
42
ut
dah tod
tone
PP
Scanned with CamScanner
Q1
URBAN
EDGE
Chapter 2 -
Practice Set
Practice Set India
Q2 What data
type will
3.0/8-2 return.
Q3 Write a program to check
whether a number
is divisible by 97 or
not.
Q4
Explain
95
ހ
EDGE
Chapter 3 - Conditional
Instructions
Sometimes we want to watch
comedy videos on You Tube if the
day is Sunday
Same times we order junk food if it is
our friend's
birthday in the
hostel.
You might want to buy an
Umbrella if its raining
and
you
have the money.
You order the meal if dal or your
favorite bhindi is listed on the menu.
Decision Making
Instructions in C. if-
else statement
→ Switch
Statement.
If-else
Statement
bo
au
if (condition to
be checked) {
Statements -if-
Condition-true;
else {
Statements - if -
condition - false;
Insipe
Scanned with CamScanner
URBAN
EDG3
Vi
Code example:
int a = 23;
if (a
718) {
printf (" You can
drive \n");
equals greater
than equal to
not equal
to
to als
Logical
Operators
&&
EDG3
Usage of Logical
Operators:
(ii) 8
88
→>
AND
1 and 0
→ is true when both the
conditions
are true
is evaluated as
false.
false 10
"
!
(3==3
(3730) →
→ evaluates to
false
evaluates to
true.
else if
&
}
else {
}
EDG3
of
else
if
fail
.
Last else is executed
only if all conditions.
Operator
precedenc
e
Priori
ty
1st
2nd
3rd
Operat
or
+-
4th
th
5'
6
7
8°
th
th
==, |=
&&
Conditional
Operators
A short hand" "if - else" can be
Conditional or
written using the
ternary operators
Condition ? expression-if-tume;
expression-it-false
Ternary
operators
Scanned with CamScanner
URBAN
EDGE
Switch (integer-
expression)
{
Case C1:
Code:
Case C2:
Code;
of
C1, Cz & Ca →
Constants.
Code → Any
valid C code
Case C3:
Code:
default
:
Code
;
A
B
→ 70 - 80 c
→ 60.70 → D.
->
→ <70 → F
Scanned with CamScanner
URBAN
EDG3
Important
Notes polo
tan
t
17 We can use switch-case
statements even by
writing cases in any order of our
choice. (not necessarily
ascending)'
2>
37
hail
EDG3
it' Chapter 3 - Practice
Setod ik w
!!! 3.-.
1 What will be the output of
this program
in lint a = 10:
if (a =
11)!
else
ud
printf (" I
am 11");
printf (" I am
not 1|
11"
);
2 Write a program to find out
whether a student is pass or fail; if it
requires total 40% and at least 33% in
each subject to pass. Assume 3 Subjects
and take marks as an input from the
user. 3 Calculate income tax paid by an
employee to the
government as per the slabs
mentioned below.
Income Slab
2.5L5OL
5.01
1
10.0L
Tax
5%
20%
30%
4
J/
N
Above
10.0L
EDG3
5 Write a program to determine
whether a
6
Character
or not
entered by the user is
lowercase
notit
-X08
hiad not
1012
102
10:01
20
WA
37
for
loop
We will look into these one
While
loop
While (condition is
true) {
// Code it has 11 Code
}
-
han
by
one
An example :
loan ton
int i=0
par latgal
While (i
<10){
L
URBAN
EDGE
+
URBAN
EDG3
त्रे
⇒
Importan
t
+++ operator does not exist.
+= is compound assign
ment operator just like == *
= /= 8 % =
do-While
Loop
=> Also
Important
// Code;
} while
(Condition)
-
Input
Out
put
:4
:
1
Du w
EDG3
for
Loop
The syntaxe of for loop looks
like this:
fox (initialize; test;
increment)
E
// Code; toledook
WW
11 Code;
// Code:
Initializ
e
or decrement
rait An example:
Updating the loop
Counter lekua ahl
Outpu
t
0
1
EDG3
17
A Case of
Decrementing for
loop
of
i
for (i=5; ; ;
; -- )
printf("%d\n",
i);
03
The
loop
loop runs
in
runs in
following
following
steps:
i is
initialized to 5
2. The condition "i" (o or nono)
is tested
27
3- The code is
executed.
42 i is
decremented
[
الب جاد
URBAN
EDG3
output =
0
2
Anal 3
4
PALLY
45
of
the
loop
The control is taken to the next
itration thus Skipping everything
below "continue" inside the bop
for that iteration
Let
Ms
look at an
example
int skip
=53
int i=0;al
while (i
<10){
if (i!=
skip)!!!
Continue;
else
}
printf("%d",
i);
quito? davrd
Anand
Outful
=>
5
EDG3
3
Notes:
tod vitam!
1. Sometimes, the name of the
variable might not indicate
the
behaviour
of
the!
progra
m
2. break statement completely
exits the loop.
Continue Statement Skips the
particular iteration of
the
loop.
Scanned with CamScanner
7
51
Chapter 4-
Practice Set
Love Maica
URBAN
EDG3
A do while loop is
executed:
12 at least once
2. at least twice
37 at most once
What can be done using one type
of loop can
done using the other two
types of
True or
False?
4 What
also be
Loop
s -
9
Repeat 8 using while
bops or!
URBAN
EDG3
1118
will
fun!
ou!
URBAN
EDGE
Game
and
Hint: Use
loops
Use a random
number generator
numb
er
the
Scanned with CamScanner
URBAN
EDG3
Void display
() {
١٤
= function
definition
}
printf(" Hi I
am display ");
function prototype is a
way to tell the compiler
function prototype & Nina
going to
define about the function we are
going
in
the
Function definition it to motor?
han aldona
This part contains the exact set of
instructions which are executed during
the function call. When va function
is called from main() main
function falls asleep and gets
temporarily Suspended. During
this time the control gols to the
function being called. When
the function body is
done executing main
() resumes.
7
Quick
Quiz
Quiz → 1. Good morning
function which prints "Good Morning"
27 Good afternoon function which
prints "Good Afternoon 37 Good
night function which prints"
Good night"
Write a program
with three
functio
1/
EDG3
Important
Points
+ Execution of a C program
starts from main () → AC
program can have more than
one function.
Every function gets called
directly or indirectly from
main()
→>
->
Commonly required
functions grouped
disks
mutton
17 Library
functions.
together in a libray
file on disk
→
agai
n.
1. To avoid rewriting the
same logic again and
2. To keep track of what we
are doing in a program 3. To test
and check logic independently
Scanned with CamScanner
URBAN
EDG3
Passing values
to functions.
We can pass values to a
function and can Value in
return from a function. wewn
int
intb.) w
maspore
sum (int a,
int b
gete
EDG3
1 13
a = 17 7 317
01
return 0;
change is a
funct
=> Misnomer
←a
vil Arca?)
a
EDG3
Recursi
on
A function defined in C can call
itself. This is called recursion.DOI.
A function calling itself is also
called recursive function.
Example of
Recursion
to
factorial
(n)
1 × 2 × 3 ..! n-
1~xn
Linny
factorial
(2)
factorial (n-1)
xn
(d) sanad]
int factorial
(int x) {
int fi
if ( x == 0 ||
x == 1) ...||
return 1;
else
f = x + factoral
(x-1);
return fi
wh drick
ли
= A program
to
calculate factorial using recursion
Scanned with CamScanner
URBAN
EDG3
5
X
How does it
work?
factorial
(5)
x factorial (4)
+
x factorial (3)
4
5
X
4
X
3.
X
factorial (2)
5
X
4
X
3
X
2x
factorial
(1)
↓
5
X
4
x 3 x
2 x
Important
Notes:
1. Recursion is sometimes
the most direct way
27
Code an
algorithen
to
called as
Chapter 5 -
Practice Set
URBAN
EDG3
4
=
5
=
** *
*
** * *
EDG3
Chapter 6 -
Pointers
A pointer is a variable which stores the
address
Another variable
of
し
72
87994
address 87994
address+
87998
Lis a
pointer j
points to i
i
#
EDG3
How to declare a
Pointer ?
the
following
syntax
→ declare a variable 1 of
type int-pointer
Store address of i
in j
1.
int *j;
J=&i
⇒
EDGE
Output:
Add i =
87994 Add i =
87994 Add 1 =
87998 Value =
8 Value = 8
i
Value = 8
i
72
J
R
87994
87998
87994
int
29487998
int "
88004
*
int **
EDGE
Types of
function calls
Based on the way we pass
arguments to the function,
function calls are of two
types.
17
17
Call by Value
call by
reference
->
Call by Value
Here the value of the
arguments are passed to the
function. Consider this
example:
int c
= sum (3,4); => assume
1=3 and y=4
x, y unitaind
if sum is defined as
sum (int a, int b)
the
Values
3 and 4 are copied to a and 5.
Now ever we change a and b,
nothing happens to the variables I
and y.
and
it
This is call by
value.
In 6 we usually make a
call by value.
Call by
reference
is
passed
Here the address of the variables
is to the function as
arguments
Now since the Addresses are
passed to the function, the
function Can à Variable in
Calling function using * and
now modify the
value of operators. Example:
弓
?
int temp;
temp = * x;
* 1 = xy;
temp;
¥ y==
URBAN
EDG3
EDG3
Ha!
6
8/11
Write a program having a
variable i. Print the address of i!
Pass this variable to a
of
Pass this variable to wa
function and print its address. Are
these addresses
Arca these wa
Same? why?
Chapter 7 -
Arrays Atwo
An array is
a
URBAN
EDGE
is a collection of similar
elements.
1 imp
TH.
=> Integer array →
Character array or
String
float percentile
[90]; → flat array
HARPA
Marks →→
7 6 21 391 3
888893AI
م
234 5
00
88 89
01622
Total = 90 elements
wit
Scanned with CamScanner
Accessing
elements
188A - I
wouldad
URBAN
EDGE
Scanf("%d", &
marks [o]);
=>
Input first
value
printf ("%d", marks [o]); =>
output first value
Quick
Quiz
→>>
of the
array
Write a program to
of five students
accept marks
in an array and print
them to the screen.
Initialization of an
Array
Hop]
I
Arrays in
memoryped an
Consider this
array:
initialized
while
declaratie
EDG3
Pointer
Arithmeticatio
A pointer can be incremented to
point to the
next
memory
Location
Consider this
example.
int i = 32;
of
that
type
.
i
->>
address 87994
4+ vdc => Now a = 87998al
Anied
char a =
'A';
NO
char * b = &a;
=> a = 87994
b++: s
float i = 1·7;
=> Now b = 87995
wind
Yay! we
understood
pointer
arithmetic
Scanned with CamScanner
Accessing Arrays
using pointers
Consider this array dow
off
7928
index
2 34
Ptr
will
point
to
If ply points to index o,
ptr ++ will
index i & so on ...
1
Multidimension
al Arrays
URBAN
EDG3
An Array can be of 2
dimension / 3 dimension / n
dimensio
ns
A 2 dimensional array can be
defined as:
int arr [3][2] =
{
{1,4}
27, 93
{1, 22 } } ;
= 1
Value = 1
⇓
Value=4
2-D arrays in
Memory
A 2d arlay like a 1-6 array
is stored in
contiguous memory blocks
like this:
arr[o][o] arr[0][1]
14
87224 87228
7
91122
Quick Quiz: Create a 2-d array
by taking input from the user.
Write a display function to print
the content of this 2-d Array in
the screen.
=
URBAN
EDG3
5
اما
7
(1) True
(1) False
(ii)
Depends
.
Write a program to create an
array of 10 integers and store
multiplication table of 5
in itinata
D
&
g
URBAN
EDG3
Fotona modland Ai
VSSAN
Scanned with CamScanner