Lecture 2 c progamming.pdf.. helpful for beginners

jenajijnasha 10 views 39 slides Sep 16, 2025
Slide 1
Slide 1 of 39
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39

About This Presentation

This PDF lecture material has been specially designed as a complete beginner’s guide to learning the C programming language. It introduces the subject in a simple, clear, and well-structured way so that students with little or no prior programming background can start their journey in computer sci...


Slide Content

C
PROGRAMMING

1.Importance Of C
2.Constants And Variables

Importance Of C
•It is one of the most popular
programming languages in the world.
•C is a robust language whose rich set of
built-in functions and operators can be
used to write any complex
program.
•C language is efficient and fast.
•C language is well suited for
structured programming.
Add a footer 3

FR
•C is very versatile. It can be used in
both applications and technologies.
•Most of libraries of today’s operating
system are written in c language.
•There are only 32 keywords in C and
its strength lies in its built-in functions.
•C is highly portable i.e the programs
written for one computer can be run
on another computer easily.
•Ability to extends itself i.e we can
add our own function to c library.
Add a footer 4

FR
Add a footer 5

FR
CONSTANTS AND VARIABLES
CONSTANTS
A constant is a value or variable in c
programming which cannot be changed
once they are defined in the program.
There can be any types of constants like
integer,float,hexadecimal,character
constants etc.
Now there are various ranges that differ
from unsigned to signed bits.Under the
signed bit,the range of an int varies from -
128 to +127 and under the unsigned
bit,int varies from 0 to 255.


Add a footer 6

FR
Add a footer 7
EXAMPLE

FR
Add a footer 8
OUTPUT

FR
Add a footer 9
VARIABLES
•A variable is a name of the memory location. It is used to
store data. Its value can be changed and it can be reused
many times.
•Different types of variables require different amounts of
memory and have some specific set of operations which can
be applied to them.
•Rules for Defining Variables
•A variable can have alphabets, digits and underscore.
•A variable name can start with the alphabet and underscore
only. It can’t start with a digit.
•It should not contain white spaces.
•A variable name should not be a reserved word or keyword.

FR
Add a footer 10
Syntax
•type variableName = value;
•where type is one of C types(such as int)and variableName is the name of
the variable(such as x or myName).The equal sign is used to assign a value
to the variable.
•EXAMPLE
•int myNum = 15;

FR
Add a footer 11
EXAMPLE

FR
Add a footer 12
OUTPUT

FR
Add a footer 13

FR
Add a footer 14
VARIABLE DECLARATION
•type variable_name;
•For Example,
•int a,b;
•float c;
•double d;
•Here a,b,c,d are variables.The int,float,double are data tpes.

VARIABLE ASSIGNMENT
•A variable assignment is a process of assigning a value to a variable.
•For Example
•int height = 40;
•Int base = 31;

FR
CONSTANTS VS VARIABLES
CONSTANTS
It is similar to a variable and cannot be
changed during Program execution.
It is a fixed variable that cannot be
changed after defining the variable in
a program.
In constants, the value cannot be
changed.
It can be express in two ways:#define
pre-processor and the const keyword.
Example: const int Len=5;
#define PI 3.14
VARIABLES
It is a variable that stores data type value
in a program
It is a variable that can be changed after
defining the variable in a program.
The value of a variable can change
depending on the conditions.
Typically it uses int,foat,string,double,etc
data types in a program.
Example: int a=5;float radius=5.2;char ‘A’;
Add a footer 15

FR
Add a footer 16

FR
Add a footer 17
SYMBOLIC CONSTANTS
•A symbolic constant is a name given to a some numeric constant or a
character constant or string constant,or any other constants.
•Symbolic constant names are also known as constant identifiers.Pre-
processor directive #define is used for defining symbolic constants.

SYNTAX

#define PI 3.141592
#define GOLDENRATIO 1.6
#define MAX 500
Here PI,GOLDENRATIO,SIZE are symbolic constants.

When we attempt to store a value
that cannot be represented correctly
by a data type,an Integer Overflow
(or) Underflow occurs.If the value is
more than the maximum
representable value, the phenomenon
is called Integer Overflow. The
phenomenon is called Integer
Underflow if the value is less than the
least representable value of the
datatype.
OVERFLOW AND UNDERFLOW
Add a footer 18

FR EXAMPLE OF OVERFLOW
Add a footer 19

OUTPUT
Add a footer 20

FR
EXAMPLE OF UNDERFLOW
Add a footer 21

OUTPUT
Add a footer 22

FR
Add a footer 23

FR
Add a footer 24
ARRAYS IN C
•An array is defined as the collection of similar type of
data items stored at contiguous memory locations.
Arrays are the derived data type in C programming
language which can store the primitive type of data such
as int, char, double, float, etc. It also has the capability
to store the collection of derived data types, such as
pointers, structure, etc. The array is the simplest data
structure where each data element can be randomly
accessed by using its index number.

•Each element of an array is of same data type and
carries the same size, i.e., int = 4 bytes.
•Elements of the array are stored at contiguous
memory locations where the first element is stored at
the smallest memory location.
•Elements of the array can be randomly accessed
since we can calculate the address of each element
of the array with the given base address and the
size of the data element.

PROPERTIES
OF ARRAYS
Add a footer 25

Why do we need arrays?
We can use normal variables (v1, v2, v3,..) when
we have a small number of objects, but if we
want to store a large number of instances, it
becomes difficult to manage them with normal
variables. The idea of an array is to represent
many instances in one variable.

FR
ADVANTAGE OF ARRAY IN C
Add a footer 28
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the
elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a
few lines of code only.
4) Random Access: We can access any element randomly using the
array.


DISADVANTAGE OF ARRAY IN C

1) Fixed Size: Whatever size, we define at the time of
declaration of the array, we can't exceed the limit. So, it
doesn't grow the size dynamically like LinkedList which we
will learn later.

One Dimensional Arrays
We can visualize a one -dimensional
array in C as a single row to store the
elements. All the elements are stored at
contiguous memory locations. Now, we
will see how to declare, initialize and
access array elements:
You can declare an array of any data type (i.e. int,
float, double, char) in C.

Array Declaration

While declaring a one-dimensional array in
C, the data type can be of any type, and
also, we can give any name to the array, just
like naming a random variable.
Syntax:
int arr[5]; //arr is the array name of type integer, and 5 is the size of the array
Array Initialization

In static uninitialized arrays, all the elements
initially contain garbage values, but we can
explicitly initialize them at their declaration.
SYNTAX:
<data_type> <arr_name> [arr_size]={value1, value2, value3,…};

FR
Add a footer 31

Array Accessing

In one-dimensional arrays in C, elements are accessed by specifying the
array name and the index value within the square brackets. Array
indexing starts from 0 and ends with size-1. If we try to access array
elements out of the range, the compiler will not show any error message;
rather, it will return some garbage value.

Syntax:
<arr_name>[index];

FR
Add a footer 33
EXAMPLE
#include < stdio.h >
int main() {
//declaring and initializing one -dimensional array in C
int arr[3] = {10, 20, 30};
// After declaration, we can also initialize the array as:
// arr[0] = 10; arr[1] = 20; arr[2] = 30;
for (int i = 0; i < 3; i++) {
// accessing elements of array
printf(" Value of arr[%d]: %d\n", i, arr[i]);
}
}
OUTPUT
Value of arr[0]: 10
Value of arr[1]: 20
Value of arr[2]: 30

FR
Add a footer 35
TWO-DIMENSIONAL ARRAYS
The two-dimensional array can be defined as an array of arrays. The 2D
array is organized as matrices which can be represented as the collection of
rows and columns. However, 2D arrays are created to implement a
relational database lookalike data structure. It provides ease of holding the
bulk of data at once which can be passed to any number of functions
wherever required.

FR
Add a footer 36
Declaration of two dimensional Array in C

The syntax to declare the 2D array is given below.

data_type array_name[rows][columns];
Consider the following example.

int twodimen[4][3];
Here, 4 is the number of rows, and 3 is the number of columns.
Initialization of 2D Array in C
The two-dimensional array can be initialized and defined in the following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};

DENNIS RITCHIE
Father of C and UNIX
(1941-2011)

AGAIN IT’S GAME TIME

FR
Add a footer 39
Tags