Module 2
Module 2
Module 2
Array:
Array is a linear data structure that is a collection of similar data types. Arrays are stored in
contiguous memory locations. It is a static data structure with a fixed size. It combines data of
similar types.
Declaring Arrays
Syntax:
Ex:
Initialization an array
However you can also initialize the array during declaration like this:
OR
Types of arrays
One-dimensional Array
Two-dimensional Array
Three-dimensional Array
Two dimensional and three dimensional arrays are also called multi-dimensional arrays.
One-dimensional Array
In C programming language, single dimensional arrays are used to store list of values of same
datatype. In other words, single dimensional arrays are used to store a row of values. In single
dimensional array, data is stored in linear form. Single dimensional arrays are also called as one-
dimensional arrays, Linear Arrays or simply 1-D Arrays.
Declaration of Single Dimensional Array
Syntax for declaring a single dimensional array
datatype arrayName [ size ] ;
Ex:
int rollNumbers [60] ;
Examples of Array Program
Ex:1
#include<stdio.h>
void main()
{
int i;
printf("%d\t",arr[i]);
OUTPUT
234
Ex:2
#include <stdio.h>
int main() {
double average;
scanf("%d", &n);
scanf("%d", &marks[i]);
sum += marks[i];
return 0;
Output:
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39.60
C language supports two dimensional arrays also. The simplest form of a multidimensional array
is the two-dimensional array. Both the row's and column's index begins from 0.
data-type array-name[row-size][column-size]
In three-dimensional array, there will be three dimensions. The array face [5] [10] [15] can hold
750 elements (5 * 10 * 15).
#include <stdio.h>
int main()
Array Operation:
Various operations that can be performed on arrays.
The difference between a character array and a string is the string is terminated with a special
character ‘\0’
Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in
C.
Declaration of Strings
Declaring a string is as simple as declaring a one-dimensional array. Below is the basic syntax
for declaring a string.
char str_name[size];
In the above syntax str_name is any name given to the string variable and size is used to
define the length of the string, i.e the number of characters strings will store.
or
Example 1:
#include <stdio.h>
int main ()
return 0;}
Example 2:
#include <stdio.h>
int main()
char name[20];
scanf("%s", name);
return 0;
String Operations:
strlen( ) Function :
strlen( ) function is used to find the length of a character string.
Example:
int n;
n = strlen(st);
• This will return the length of the string 9 which is assigned to an integer variable n.
#include <stdio.h>
#include <string.h>
int main ()
strcpy(str3, str1);
len = strlen(str1);
return 0;
OUTPUT
strlen(str1) : 10
Functions:
In C, we can divide a large program into the basic building blocks known as function.
A function can be called multiple times to provide reusability and modularity to the C program.
Advantage of functions in C:
• By using functions, we can avoid rewriting same logic/code again and again in a
program.
• We can call C functions any number of times in a program and from any place in a
program.
• We can track a large C program easily when it is divided into multiple functions.
Syntax
• Function declaration
• Function call
function_name (argument_list)
• Function definition
function body;
Return Type − A function may return a value. The return_type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
Parameters − A parameter is like a placeholder. When a function is invoked, you pass a
value to the parameter. This value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the parameters of a function.
Parameters are optional; that is, a function may contain no parameters.
Function Body − The function body contains a collection of statements that define what
the function does.
Ex:
#include <stdio.h>
// Function declaration
int max_Num(int i, int j){
// Function definition
if (i > j)
return i;
else
return j;
}
// The main function.
int main(void){
int x = 15, y = 20;
// Calling the function to find the greater number among the two
int m = max_Num(x, y);
printf("The bigger number is %d", m);
return 0;
}
Output:
Types of Functions
• User-defined functions:
Built in or Library Functions: are the functions which are declared in the C header files such as
Example
int main() {
printf("Hello World!");
return 0;
User-defined functions:
User defined functions are the functions which are created by the C programmer, so that
he/she can use it many times.
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,
#include<stdio.h>
void printName();
void main ()
{
printf("Hello ");
printName();
}
void printName()
{
printf(“ECE");
}
#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
10 24
The sum is 34
#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
The sum is 34
#include<stdio.h>
int even_odd(int);
void main()
{
int n,flag=0;
printf("\nGoing to check whether a number is even or odd");
printf("\nEnter the number: ");
scanf("%d",&n);
flag = even_odd(n);
if(flag == 0)
{
printf("\nThe number is odd");
}
else
{
printf("\nThe number is even");
}
int even_odd(int n)
{
if(n%2 == 0)
{
return 1;
}
else
{
return 0;
}
}
Output
C Library Functions
Library functions are the inbuilt function in C that are grouped and placed at a common place
called the library. Such functions are used to perform some specific operations. For example,
printf is a library function used to print on the console. The library functions are created by the
designers of compilers. All C standard library functions are defined inside the different header
files saved with the extension .h.
We need to include these header files in our program to make use of the library functions defined
in such header files.
For example, To use the library functions such as printf/scanf we need to include stdio.h in our
program which is a header file that contains all the library functions regarding standard
input/output.
SN Header Description
file
1 stdio.h This is a standard input/output header file. It contains all the library
functions regarding standard input/output.
3 string.h It contains all string related library functions like gets(), puts(),etc.
4 stdlib.h This header file contains all the general library functions like malloc(),
calloc(), exit(), etc.
5 math.h This header file contains all the math operations related functions like
sqrt(), pow(), etc.