KEMBAR78
Lecture 2 c progamming.pdf.. helpful for beginners | PDF
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
W e c a n v i s ual i ze a o n e - di men si o n al
a r r ay i n C a s a s i n g le r o w t o s t o re t h e
e l e men t s . A l l t h e e l e men t s a r e s t o red a t
c o n t i guo us m e m o ry l o c at i o ns . N o w, w e
w i l l s e e h o w t o d e c l are , i n i t i al i ze a n d
a c c es s a r ra y e l e m en t s:
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]: %dn", 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

Lecture 2 c progamming.pdf.. helpful for beginners

  • 1.
  • 2.
  • 3.
    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
  • 4.
    FR • C isvery 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
  • 5.
  • 6.
    FR CONSTANTS AND VARIABLES CONSTANTS Aconstant 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
  • 7.
    FR Add a footer7 EXAMPLE
  • 8.
  • 9.
    FR Add a footer9 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.
  • 10.
    FR Add a footer10 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;
  • 11.
    FR Add a footer11 EXAMPLE
  • 12.
    FR Add a footer12 OUTPUT
  • 13.
  • 14.
    FR Add a footer14 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;
  • 15.
    FR CONSTANTS VS VARIABLES CONSTANTS Itis 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
  • 16.
  • 17.
    FR Add a footer17 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.
  • 18.
    When we attemptto 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
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
    FR Add a footer24 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.
  • 25.
    • Each elementof 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
  • 26.
    Why do weneed 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.
  • 28.
    FR ADVANTAGE OF ARRAYIN 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.
  • 29.
    One Dimensional Arrays We c a n v i s ual i ze a o n e - di men si o n al a r r ay i n C a s a s i n g le r o w t o s t o re t h e e l e men t s . A l l t h e e l e men t s a r e s t o red a t c o n t i guo us m e m o ry l o c at i o ns . N o w, w e w i l l s e e h o w t o d e c l are , i n i t i al i ze a n d a c c es s a r ra y e l e m en t s: You can declare an array of any data type (i.e. int, float, double, char) in C.
  • 30.
    Array Declaration While declaringa 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,…};
  • 31.
  • 32.
    Array Accessing In one-dimensionalarrays 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];
  • 33.
    FR Add a footer33 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]: %dn", i, arr[i]); } } OUTPUT Value of arr[0]: 10 Value of arr[1]: 20 Value of arr[2]: 30
  • 35.
    FR Add a footer35 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.
  • 36.
    FR Add a footer36 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}};
  • 37.
    DENNIS RITCHIE Father ofC and UNIX (1941-2011)
  • 38.
  • 39.