Download C Programming: Arrays, Two-Dimensional Arrays, Functions and String Manipulation and more Study notes C programming in PDF only on Docsity!
UNIT- 3
Arrays, Functions, Storage classes, Strings
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.
C array is beneficial if you have to store similar elements. For example, if we want to store the marks
of a student in 6 subjects, then we don't need to define different variables for the marks in the different
subject. Instead of that, we can define an array which can store the marks in each subject at the
contiguous memory locations.
Properties of Array
The array contains the following properties.
- 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.
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. 1.marks[0]=80;//initialization of array 2.marks[1]=60; 3.marks[2]=70; 4.marks[3]=85; 5.marks[4]=75;
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};
Example of 1-D array
#include<stdio.h>
int main(){
int i;
int marks[5]={20,30,40,50,60};//declaration and init
ialization of array
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
return 0;
Output:
Two Dimensional Array in C
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.
Declaration of two dimensional Array in C
The syntax to declare the 2D array is given below.
data_type array_name[rows][columns];
int twodimen[4][3];
Here, 4 is the number of rows, and 3 is the number of columns.
Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the declaration and initialization are
being done simultaneously. However, this will not work with 2D arrays. We will have to define at
least the second dimension of the array. The two-dimensional array can be declared and defined in
the following way.
1.int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Two-dimensional array example in C
#include<stdio.h>
int main(){
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
Output
arr[0][0] = 1 arr[0][1] = 2
arr[0][2] = 3 arr[1][0] = 2
arr[1][1] = 3 arr[1][2] = 4
arr[2][0] = 3 arr[2][1] = 4
arr[2][2] = 5 arr[3][0] = 4
arr[3][1] = 5 arr[3][2] = 6
Function Aspects There are three aspects of a C function.
- Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type.
- Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.
- Function definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function. SN C function aspects Syntax 1 Function declaration return_type function_name (argument list); 2 Function call function_name (argument_list) 3 Function definition return_type function_name (argument list) {function body;}
The syntax of creating function in c language is given below: return_type function_name(data_type parameter...) { //code to be executed } Types of Functions There are two types of functions in C programming: 1.Library Functions : are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. 2.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.
Call by value and Call by reference in C There are two methods to pass the data into the function in C language, i.e., call by value and call by reference.
Call by value in C
- In call by value method, the value of the actual parameters is copied into the formal parameters. In other words, we can say that the value of the variable is used in the function call in the call by value method.
- In call by value method, we can not modify the value of the actual parameter by the formal parameter.
- In call by value, different memory is allocated for actual and formal parameters since the value of the actual parameter is copied into the formal parameter.
- The actual parameter is the argument which is used in the function call whereas formal parameter is the argument which is used in the function definition. Let's try to understand the concept of call by value in c language by the example given below:
Call by reference in C
- In call by reference, the address of the variable is passed into the function call as the actual parameter.
- The value of the actual parameters can be modified by changing the formal parameters since the address of the actual parameters is passed.
- In call by reference, the memory allocation is similar for both formal parameters and actual parameters. All the operations in the function are performed on the value stored at the address of the actual parameters, and the modified value gets stored at the same address.
Swapping the values of the two variables using call by reference #include <stdio.h> void swap(int *, int *); //prototype of the function int main() { int a = 10; int b = 20; printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main swap(&a,&b); printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters do change in call by r eference, a = 10, b = 20 } void swap (int *a, int *b) { int temp; temp = a; a=b; b=temp; printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10 } Output Before swapping the values in main a = 10, b = 20 After swapping values in function a = 20, b = 10 After swapping values in main a = 20, b = 10
Recursion in C Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem. Any function which calls itself is called recursive function, and such function calls are called recursive calls. Recursion involves several numbers of recursive calls. Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be defined in terms of similar subtasks. For Example, recursion may be applied to sorting, searching, and traversal problems. However, some problems are best suited to be solved by the recursion, for example, tower of Hanoi, Fibonacci series, factorial finding, etc. In the following example, recursion is used to calculate the factorial of a number.
#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 Enter the number whose factorial you want to calculate? factorial = 120