Unit 3 FOC
Unit 3 FOC
MODULE 3
Arrays
INTRODUCTION
Arrays: Array is a sequential collection of similar data items.
Pictorial representation of an array of 5 integers
10 20 30 40 50
A[0] A[1] A[2] A[3] A[4]
A single dimensional array is a linear list of related data items of same data type.
In memory, all the data items are stored in contiguous memory locations.
Declaration of one-dimensional array(Single dimensional array)
Syntax:
datatype array_name[size];
float b[5];
Nivesh Balyan 1
C programming and problem solving Module 3
The above statement allocatests 5*4=20 Bytes of memory for the array b.
Each element in the array is identified using integer number called as index.
If n is the size of array, the array index starts from 0 and ends at n-1.
Declaration of arrays only allocates memory space for array. But array elements are not initialized
and hence values has to be stored.
Therefore to store the values in array, there are 3 methods
1. Initialization
2. Assigning Values
3. Input values from keyboard through scanf()
Where
datatype can be char,int,float,double
array name is the valid identifier
size is the number of elements in array
v1,v2,v3… ..... vn are values to be assigned.
2 4 34 3 4
Nivesh Balyan 2
C programming and problem solving Module 3
10 20 0 0 0
4. String Initialization
Sequence of characters enclosed within double quotes is called as string.
The string always ends with NULL character(\0)
Nivesh Balyan 3
C programming and problem solving Module 3
S V I T \0
S[0] S[1] S[2] S[3] S[4]
10 20 30
In general
To read ith element:
scanf(“%d”,&a[i]); where i=0; i<n; i++
Nivesh Balyan 4
C programming and problem solving Module 3
data_type array_name[exp1][exp2];
Or
data_type
array_name[row_size][column_size];
For example:
int a[2][3];
The above statements allocates memory for 3*4=12 elements i.e 12*2=24 bytes.
Nivesh Balyan 7
C programming and problem solving Module 3
10 20 30
40 50 60
70 80 90
40 50 0
70 80 0
Nivesh Balyan 8
C programming and problem solving Module 3
#include<stdio.h>
void main()
{
int m,n,i,j,a[3][3];
printf(“enter number of rows and columns\n”);
scanf(“%d %d”,&m,&n);
printf(“eneter array elements\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“array elements are\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
}
Nivesh Balyan 9
C programming and problem solving Module 3
Strings
Definition:
A string is a sequence of characters within double quotes. A string constant is always terminated y null character.
A string is pictorially represented as follows:
a S V I T \0
0 1 2 3 4
String Declaration:
Like all other variable a string variable a string variable also has to be declared before it is used.
0 1 2 3 4 5 6 7 8 9
String Initialization:
Initialization is a process of assigning values to a string, before doing manipulation.
Strings are initialized in 4 ways:
1. Initializing character by character
2. Partial Array Initialization
3. Initialization without size
4. Initialization of Array with string
b S V I T \0
0 1 2 3 4
NOTE: scanf() cannot read spaces and any special symbols i.e
conversion code cannot read spaces, it will terminated as soon
as space appear.
Write a program to read and print an string using scanf() and printf()
#include<stdio.h>
void main()
{
char str[20];
printf(“enter the string\n”);
scanf(“%s”,str);
printf(“The entered string is \n”);
printf(“%s”,str);
}
Write a program to read and print an string using gets() and puts()
#include<stdio.h> OutPut:
#include<string.h>
void main() Enter the string
{ HELLO HOW R U
char str[20]; The entered string is
printf(“enter the string\n”); HELLO HOW R U
gets(str);
printf(“The entered string is \n”);
puts(str);
}
H E L L O H O W R U \0
Based on the kind of data processed, the I/O function are classified into
1. Token Oriented I/O functions:
2. Line Oriented I/O functions
3. Character Oriented I/O functions
2 strcpy strcpy(char dest[ ] , char src[ ]); char src[5] =”SVIT”; This function copies content
char dest[5]; from source string to
strcpy(dest ,src); destination string including \0.
src[0] src[1] src[2] src[3] src[4] Size of dest string should be
s V I T \0 greater or equal to the size of
source string src to store the
S V I T \0 entire source string.
3. strncpy strcpy(char dest[ ] , char src[ ],int n); char src[5] =”SVIT”; This function copies n
char dest[5]; characters from source string to
strcpy(dest ,src,2); destination string .
src[0] src[1] src[2] src[3] src[4] In thisexample only 2
S V I T \0 characters are copied from src
to dest.
S V \0
5 strncat strncat(char s1[ ] , char s2[ ],n); char s1[5]=”SVIT”; This function copies the n
char s2[5]=”ECE”; characters of s2 string to the
strncat(s1,s2,2); end of s1 string.
The delimiter of s1 is replaced
S V I T \0 by first character of s2.
0 1 2 3 4
Page 8
C programming and problem solving
E C E \0
0 1 2 3 4
S1 S V I T E C \
0
0 1 2 3 4 4 6 7 8 9
6 strcmp int strcmp( char s1[ ] , char s2[ ]); 1) Strings are equal where:
S1[4]=”RAM”; s1 is first string
S2[4]=”RAM”; s2 is second string
Strcmp(S1,S2); This function used to
compare two strings.
R == S2[0] R The comparison starts with
S1[0] first character of each
S1[1] A == S2[1] A string.
S1[2] M == S3[2] M This comparison continues
S1[3] \0 == S4[3] \0 till the corresponding
character differ or until the
S1[0]==S2[0] end of the character is
R==R(ASCII value of R is compared) reached.
similarly for other characters. The strcmp Returns 3values
S1[3]==S2[3] Possibly:
\0==\0(ASCII value of \0 is compared and it returns 0 if both strings are equal.
is 0.) returns positive value ,if s1>s2
2)String S1 is Lesser than String S2 returns negative value if s1<s2
S1[4]=”ABC”;
S2[4]=”BAC”;
Strcmp(S1,S2);
S1[0] A == S2[0] B
S1[1] B == S2[1] A
S1[2] C == S3[2] C
S1[3] \0 == S4[3] \0
S1[0]==S2[0]
A==B(ASCII value of A is compared with
ASCII value of B)
i.e 65==66 returns S1<S2
S1[0] B == S2[0] A
S1[1] B == S2[1] B
S1[2] C == S3[2] C
S1[3] \0 == S4[3] \0
S1[0]==S2[0]
A==B(ASCII value of A is compared with
ASCII value of B)
i.e 66==65 returns S1>S2
7 strncmp int strcmp( char s1[ ] , char s2[ ], n); 1) Strings are equal where:
S1[4]=”RAM”; s1 is first string
S2[4]=”RAM”; s2 is second string
strcmp(S1,S2,2); This function used to
comparen number of
R == S2[0] R characters two strings.
S1[0] The comparison starts with
S1[1] A == S2[1] A first character of each
S1[2] M == S3[2] M string.
S1[3] \0 == S4[3] \0 This comparison continues
till the corresponding
Only 2 characters from each S1 and S2 is character differ or until the
compared. end of the character is
Other function is similar to strcmp(). reached or specified number
of characters have been
tested..
The strcmp Returns 3values
Possibly:
returns 0 if both strings are equal.
returns positive value ,if s1>s2
returns negative value if s1<s2
8 strrev( ) void strrev(char str[ ]); Given string This function reverse all
S1 S V I T E C E \0 characters in the S1 except
0 1 2 3 4 5 6 7 89 Null character.
strrev(s1 The original string is lost.
Reverse String
S1 E C E T I V S \0
0 1 2 3 4 5 6 7 8 9
S1[6]==S1[0]
S1[5]==S1[1]
S1[4]==S1[2]
S1[3]==S1[3]
S1[2]==S1[4]
S1[1]==S1[5]
S1[0]==S1[6]
strlen() strrev()
#include<stdio.h>
#include<stdio.h> #include<string.h>
#include<string.h> void main()
void main() {
{ char name[15]; char str[]=”INDIA”;
int len; strrev(str);
printf(“Enter the string\n”); printf(“string=%s”,str);
gets(name); }
len=strlen(name);
printf(“\n The string length is %d”,len); OUTPUT
} String=AIDNI
OUTPUT
Enter the string
COMPUTER
The string length is 8
SCOPE OF VARIABLE
There are two types of Variables on the basis of Scopes:
A. Local Variable
B. Global Variable
Local Variable: The variable that is declared in a function or code is called a local variable. The scope of Local variables is within the defined
function only. You cannot use a local variable outside the function (in which it is declared).
#include <stdio.h>
void person()
{
// Local Variables of the function
int main()
{
person();
return 0;
}
C programming and problem solving
Global Variable: The scope of the global variable is the entire program. They are not defined inside any function and can be used in any function.
#include <stdio.h>
int a = 23;
void function1()
{
void function2()
{
// Function using global variable a
int main()
{
// Calling functions
function1();
function2();
return 0;
}
C programming and problem solving
Program Output
HOW TO CALL C FUNCTIONS IN A PROGRAM?
There are two ways that a C function can be called from a program. They are,
1. Call by value
2. Call by reference
1. CALL BY VALUE:
In call by value method, the value of the variable is passed to the function as parameter.
The value of the actual parameter can not be modified by formal parameter.
Different Memory is allocated for both actual and formal parameters. Because, value of actual
parameter is copied to formal parameter.
Note:
While calling a function, we pass values of variables to it. variables, we pass address of variables(location of variables) to
Such functions are known as “Call By Values”. the function known as “Call By References.
In this method, the value of each variable in calling In this method, the address of actual variables in the calling
function is copied into corresponding dummy variables of function are copied into the dummy variables of the called
With this method, the changes made to the dummy With this method, using addresses we would have an access to
variables in the called function have no effect on the the actual variables and hence we would be able to manipulate
In call by values we cannot alter the values of actual In call by reference we can alter the values of variables through