Array
Array is a collective name given to a group of similar quantities.
An ordinary variable is able to store one value at a time but an array is able to store more than one value at
a time.
Declaring the data type, name and maximum number of values to be stored in the array is known as dimensioning
the array.
Single Dimensional array
Collective name given to a group of similar quantities of same data type with only one subscript (index).
Two Dimensional array
Collective name given to a group of similar quantities of same data type with two subscript (index).
Multiplication of matrix
Suppose the two matrix are of size m*n and p*q then the product of matrix will be size m*q.
The matrix multiplication is possible only when number of column of 1st matrix is equal to number of rows of
2nd matrix.
Passing array to function
Like passing argument to a function, we can also pass array to a function. While passing array to a function,
in calling function name of the array is only required.
In called function, name of the array with empty square bracket is required and size is optional.
Passing Multidimensional Arrays to a Function
To pass multidimensional arrays to a function, only the name of the array is passed to the function (similar
to one-dimensional arrays).
While passing two-dimensional arrays, it is not mandatory to specify the number of rows in the array. However,
the number of columns should always be specified.
Note: In C programming, you can pass arrays to functions, however, you cannot return
arrays from functions.
String
String are array of character enclosed in the double quotation mark.
To make the end of the string, C uses the null character (\0) and it is inserted by the compiler automatically
at the end of the string.
Declaration and Initilization
char a[5] = {"abcde"};
char d[] = {"abcde"};
char c[5] = "abcde";
char b[6] = { 'a', 'b', 'c', 'd', 'e','\0' };
Assigning Values to Strings
Arrays and strings do not support the assignment operator once it is declared. For example.
char a[50];
a = "abcde"; // //Error!!
Solution: Use the strcpy() function to copy the string instead.
Read String from the user
The scanf() function is use to reads the sequence of characters until it encounters whitespace (space,
newline, tab, etc).
Even though Aamir Khan was entered in the above program, only Aamir was stored in the name string. It's
because there was a space after Aamir.
How to read a line of text?
You can use the gets() function to read a line of string. And you can use puts() to display the string.
Passing string to a Function
Strings can be passed to a function in a similar way as arrays like..
String Handling Function
Name |
Syntax |
Function & Purpose |
strlen() |
length = strlen(str1) |
This function is used to determine the lenght of the string except null character. |
strrev() |
strrev(str1) |
This function reverse the string and store in the same string i.e. str1 |
strlwr() |
strlwr(str1) |
This function is used to convert upper case string to lower case string. |
strupr() |
strupr(str1) |
This function is used to convert lower case string to upper case string. |
strcat() |
strcat(str1,str2) |
This function is used to concatinate (combine) the two string i.e. str1 and str2. |
strcpy() |
strcpy(str1,str2) |
This function is use to copy the source string (str2) to destination string (str1). |
strcmp() |
if(strcmp(str1,str2) == 0)
if(strcmp(str1,str2) > 0)
if(strcmp(str1,str2)
< 0)
|
This function is used to compare two string and return the ASCII value as below.
if(str1 == str2) returns value 0
if(str1 > str2) returns value > 0 i.e. positive value (+)
if(str1
< str2) returns value < 0 i.e. negative value (-)
|