Fundamentals_of_C_Language_Lecture_4.pptx

PSanjay8 0 views 35 slides Oct 28, 2025
Slide 1
Slide 1 of 35
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

About This Presentation

This presentation covers the concepts of functions and arrays in C programming. It explains how functions promote modularity and code reuse through declaration, definition, and calling mechanisms. The lecture also explores arrays, their declaration, initialization, and usage for storing multiple dat...


Slide Content

C Functions Types of Functions There are two types of functions in C programming: Library Functions:  are the functions which are declared in the C header files such as scanf (), printf(), gets(), puts(), ceil(), floor() etc. User-defined functions:  are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.

C Functions Different aspects of function calling A function may or may not accept any argument. It may or may not return any value. Based on these facts, There are four different aspects of function calls. function without arguments and without return value function without arguments and with return value function with arguments and without return value function with arguments and with return value

//Example for Function without argument and return value #include<stdio.h> void printName (); void main () { printf("Hello "); printName (); } void printName () { printf(" Javatpoint "); } Output: C Functions

//Example for Function without argument and with return value #include<stdio.h> int sum(); void main() { int result; printf("\ nGoing to calculate the sum of two numbers:"); result = sum(); printf("% d",result ); } int sum() { int a,b ; printf("\ nEnter two numbers"); scanf ("%d % d",&a,&b ); return a+b ; } Output: C Functions

//Example for Function with argument and without return value #include<stdio.h> void sum(int, int); void main() { int a,b,result ; printf("\ nGoing to calculate the sum of two numbers:"); printf("\ nEnter two numbers:"); scanf ("%d % d",&a,&b ); sum( a,b ); } void sum(int a, int b) { printf("\ nThe sum is %d", a+b ); } Output: C Functions

//Example for Function with argument and with return value #include<stdio.h> int sum(int, int); void main() { int a,b,result ; printf("\ nGoing to calculate the sum of two numbers:"); printf("\ nEnter two numbers:"); scanf ("%d % d",&a,&b ); result = sum( a,b ); printf("\ nThe sum is : % d",result ); } int sum(int a, int b) { return a+b ; } Output: C Functions

C Functions Recursive Function In C, a function that calls itself is called Recursive Function. The recursive functions contain a call to themselves somewhere in the function body. Moreover, such functions can contain multiple recursive calls.

#include < stdio.h > int fact (int); int main() { int n,f ; printf("Enter the number whose factorial you want to calculate?"); scanf ("% d",&n ); f = fact(n); printf("factorial = % d",f ); } int fact(int n) { if (n==0) { return 0; } else if ( n == 1) { return 1; } else { return n*fact(n-1); } } Output: Control flow statements

Global Variable Local Variable Global variables are declared outside all the function blocks. Local Variables are declared within a function block. The scope remains throughout the program. The scope is limited and remains within the function only in which they are declared. Any change in global variable affects the whole program, wherever it is being used. Any change in the local variable does not affect other functions of the program. A global variable exists in the program for the entire time the program is executed. A local variable is created when the function is executed, and once the execution is finished, the variable is destroyed. It can be accessed throughout the program by all the functions present in the program. It can only be accessed by the function statements in which it is declared and not by the other functions. If the global variable is not initialized, it takes zero by default. If the local variable is not initialized, it takes the garbage value by default. Global variables are stored in the data segment of memory. Local variables are stored in a stack in memory. We cannot declare many variables with the same name. We can declare various variables with the same name but in other functions.

Storage Classes in C Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a variable. There are four types of storage classes in C Automatic External Static Register

Storage Classes in C Storage Classes Storage Place Default Value Scope Lifetime auto RAM Garbage Value Local Within function extern RAM Zero Global Till the end of the main program Maybe declared anywhere in the program static RAM Zero Local Till the end of the main program, Retains value between multiple functions call register Register Garbage Value Local Within the function

Storage Classes in C Automatic Automatic variables are allocated memory automatically at runtime. The visibility of the automatic variables is limited to the block in which they are defined. The scope of the automatic variables is limited to the block in which they are defined. The automatic variables are initialized to garbage by default. The memory assigned to automatic variables gets freed upon exiting from the block. The keyword used for defining automatic variables is auto. Every local variable is automatic in C by default.

#include < stdio.h > int main() { auto int a; //auto auto char b; auto float c; printf("%d %c %f", a,b,c ); // printing initial default value of automatic variables a, b, and c. return 0; } Output: Storage Classes in C

Storage Classes in C Static The variables defined as static specifier can hold their value between the multiple function calls. Static local variables are visible only to the function or the block in which they are defined. A same static variable can be declared many times but can be assigned at only one time. Default initial value of the static integral variable is 0 otherwise null. The visibility of the static global variable is limited to the file in which it has declared. The keyword used to define static variable is static.

#include<stdio.h> static char c; static int i ; static float f; static char s[100]; void main () { printf("%d %d %f %s", c,i,f ); // the initial default value of c, i , and f will be printed. } Output: Storage Classes in C

Storage Classes in C Register The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU. We can not dereference the register variables, i.e., we can not use &operator for the register variable. The access time of the register variables is faster than the automatic variables. The initial default value of the register local variables is 0. The register keyword is used for the variable which should be stored in the CPU register. However, it is compiler?s choice whether or not; the variables can be stored in the register. We can store pointers into the register, i.e., a register can store the address of a variable. Static variables can not be stored into the register since we can not use more than one storage specifier for the same variable.

#include < stdio.h > int main() { register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0. printf("% d",a ); } Output: Storage Classes in C

Storage Classes in C External The external storage class is used to tell the compiler that the variable defined as extern is declared with an external linkage elsewhere in the program. The variables declared as extern are not allocated any memory. It is only declaration and intended to specify that the variable is declared elsewhere in the program. The default initial value of external integral type is 0 otherwise null. We can only initialize the extern variable globally, i.e., we can not initialize the external variable within any block or method. An external variable can be declared many times but can be initialized at only once. If a variable is declared as external then the compiler searches for that variable to be initialized somewhere in the program which may be extern or static. If it is not, then the compiler will show an error.

#include < stdio.h > int a; int main() { extern int a; // variable a is defined globally, the memory will not be allocated to a printf("% d",a ); } Output: Storage Classes in C

C Array 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. Properties of Array 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.

C Array Advantage of C Array 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 C Array 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.

C Array Declaration of C Array data_type   array_name [ array_size ]; ( or   ) int  marks[5];   Initialization of C Array The simplest way to initialize an array is by using the index of each element. We can initialize each element of the array by using the index. Consider the following example. marks[0]=80; //initialization of array    marks[1]=60;   marks[2]=70;   marks[3]=85;   marks[4]=75;  

#include<stdio.h> int main(){ int i =0; int marks[5];//declaration of array marks[0]=80;//initialization of array marks[1]=60; marks[2]=70; marks[3]=85; marks[4]=75; for( i =0;i<5;i++){ printf("%d \ n",marks [ i ]); }//end of for loop return 0; } Output: C Array

C Array C Array: Declaration with Initialization We can initialize the c array at the time of declaration. Let's see the code. int  marks[5]={20,30,40,50,60};   In such case, there is  no requirement to define the size . So it may also be written as the following code. int  marks[]={20,30,40,50,60};  

C Array 1.  One-dimensional array (1-D arrays) : You can imagine a 1d array as a row, where elements are stored one after another. 1D array Syntax for Declaration of Single Dimensional Array Below is the syntax to declare the single-dimensional array data_type array_name [ array_size ]; where, data_type :  is a type of data of each array block. array_name :  is the name of the array using which we can refer to it. array_size :  is the number of blocks of memory array going to have. For Example int nums [5];

C Array 2. Two-dimensional (2D) array: Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of rows and columns. 2D array Syntax for Declaration of Two-Dimensional Array Below is the syntax to declare the Two-dimensional array data_type array_name [sizeof_1st_dimension][sizeof_2nd_dimension]; where, data_type :  is a type of data of each array block. array_name :  is the name of the array using which we can refer to it. sizeof_dimension :  is the number of blocks of memory array going to have in the corresponding dimension. For Example int nums [5][10]; nums [0][0]==5; n ums [1][2]==35;

C Array 3. Three-dimensional array: A 3-D Multidimensional array contains three dimensions, so it can be considered an array of two-dimensional arrays. 3D array Syntax for Declaration of Three-Dimensional Array Below is the syntax to declare the Three-dimensional array data_type array_name [sizeof_1st_dimension][sizeof_2nd_dimension][sizeof_3rd_dimension]; where, data_type :  is a type of data of each array block. array_name :  is the name of the array using which we can refer to it. sizeof_dimension :  is the number of blocks of memory array going to have in the corresponding dimension. For Example int nums [5][10][2];

C Array

#include<stdio.h> void main () { int i , j,temp ; int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; for( i = 0; i <10; i ++) { for(j = i+1; j<10; j++ ) { if(a[j] > a[ i ]) { temp = a[ i ]; a[ i ] = a[j]; a[j] = temp; } } } printf("Printing Sorted Element List ...\n"); for( i = 0; i <10; i ++) { printf("%d\ n",a [ i ]); } } Output: C Array

#include<stdio.h> void main () { int arr [100], i,n,largest,sec_largest ; printf("Enter the size of the array?"); scanf ("% d",&n ); printf("Enter the elements of the array?"); for( i = 0; i <n; i ++) { scanf ("%d",& arr [ i ]); } largest = arr [0]; sec_largest = arr [1]; for( i =0;i< n;i ++) { if( arr [ i ]>largest) { sec_largest = largest; largest = arr [ i ]; } else if ( arr [ i ]> sec_largest && arr [ i ]!=largest) { sec_largest = arr [ i ]; } } printf("largest = %d, second largest = %d", largest,sec_largest ); } Output: C Array

#include < stdio.h > #define MAX_SIZE 100 int main() { int rows, cols, i , j; int A[MAX_SIZE][MAX_SIZE], B[MAX_SIZE][MAX_SIZE], C[MAX_SIZE][MAX_SIZE]; printf("Enter the number of rows and columns for the matrices (max size %d): ", MAX_SIZE); scanf ("%d %d", &rows, &cols); printf("Enter elements of matrix A:\n"); for ( i = 0; i < rows; i ++) { for (j = 0; j < cols; j++ ) { scanf ("%d", &A[ i ][j]); } } printf("Enter elements of matrix B:\n"); for ( i = 0; i < rows; i ++) { for (j = 0; j < cols; j++ ) { scanf ("%d", &B[ i ][j]); } } for ( i = 0; i < rows; i ++) { for (j = 0; j < cols; j++ ) { C[ i ][j] = A[ i ][j] + B[ i ][j]; } } printf("Resultant matrix C (A + B):\n"); for ( i = 0; i < rows; i ++) { for (j = 0; j < cols; j++ ) { printf("%d ", C[ i ][j]); } printf("\n"); } return 0; } Output: C Array

i j a[ i ][j] 0 0 a[0][0]=12 0 1 a[0][1]=2 0 2 a[0][2]=5 0 3 exit from inner for loop 1 0 a[1][0]=13 1 1 a[1][1]=2 ….. ….. ….. …… ….. ….. 2 2 a[2][2]=2 2 3 exit from inner for loop 3 exit from outer for loop Output: C Array

C Array A 12 2 5 13 2 3 14 4 2 C 12 5 10 14 6 6 16 9 3 B 3 5 1 4 3 2 5 1

#include < stdio.h > #define MAX_SIZE 100 int main() { int rowsA , colsA , rowsB , colsB , i , j, k; int A[MAX_SIZE][MAX_SIZE], B[MAX_SIZE][MAX_SIZE], C[MAX_SIZE][MAX_SIZE]; printf("Enter the number of rows and columns for matrix A (max size %d): ", MAX_SIZE); scanf ("%d %d", & rowsA , & colsA ); printf("Enter the number of rows and columns for matrix B (max size %d): ", MAX_SIZE); scanf ("%d %d", & rowsB , & colsB ); if ( colsA != rowsB ) { printf("Matrix multiplication not possible. Number of columns in A must be equal to number of rows in B.\n"); return 1; } printf("Enter elements of matrix A:\n"); for ( i = 0; i < rowsA ; i ++) { for (j = 0; j < colsA ; j++ ) { scanf ("%d", &A[ i ][j]); } } printf("Enter elements of matrix B:\n"); for ( i = 0; i < rowsB ; i ++) { for (j = 0; j < colsB ; j++ ) { scanf ("%d", &B[ i ][j]); } } for ( i = 0; i < rowsA ; i ++) { for (j = 0; j < colsB ; j++ ) { C[ i ][j] = 0; for (k = 0; k < colsA ; k++) { C[ i ][j] += A[ i ][k] * B[k][j]; } } } printf("Resultant matrix C:\n"); for ( i = 0; i < rowsA ; i ++) { for (j = 0; j < colsB ; j++ ) { printf("%d ", C[ i ][j]); } printf("\n"); } return 0; } Output: C Array

C Array b 1 2 3 2 2 1 A 2 4 6 3 2 1 result 2x1+4x2=10 2x2+4x2=12 2x3+4x1=10