0% found this document useful (0 votes)
11 views

Array pdf for c programming

Pdf on array for c programming

Uploaded by

srajalkumar2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Array pdf for c programming

Pdf on array for c programming

Uploaded by

srajalkumar2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

UNIT 04

Array
Keywords in Arrays
Introduction of Arrays
Array: Array helps to store the multiple elements of
same data type
Array index
0 to n-1 (n= size of array)

Array

Index
1. An array is a set of data elements arranged sequentially.
2. These data elements can be either of int, float or char
data type.
3. All elements of an array must be of the same data type.
4. While declaring an array, we give its data type, its name and its
size.
5. An Array is stored as a continuous block in the memory.
Array elements are accessed by their index.
6. Index begins from 0 and goes up to n-1 where n is the number
of elements in the array.
7. An array of 10 elements will have index ranging from 0 to n-1
8. Even though an array may contain elements of any data type,
the index is always int data type.
9. Arrays are extremely useful in computer programming as
they hold a collection of data together like marks of students,
scores of batsmen etc.
10. Character arrays are useful in holding strings in C.
11. A one-dimensional array holds a linear series of data.
12. A two-dimensional array hold data in a tabular form
comprising rows and columns just like a matrix.
Declaration of 1D Arrays

 In above example we only declare the array but did not initialize
Array can be initialized by tow ways
1) Compile time (in the programmed itself ) or
2) At Runtime (taking values from the users)
Initialization of of 1D Arrays
1)Compile time/Static initialization
Initialization of of 1D Arrays
2) Run time/Dynamic initialization

Array elements are initialized while the program is being executed. This
can be done by taking values from the user in a loop, or by calculating
and assigning values to the elements by a program.
3) Partial initialization
Two Dimensional Arrays
 A 2D array is a collection of elements arranged in rows
and columns.
 Each element in the array can be accessed using two
indices: one for the row and one for the column.
This structure allows you to organize and manipulate data
in a more intuitive way when dealing with multi-dimensional
information.
The number of rows and columns in a 2D array is defined
at the time of declaration and cannot be changed during
runtime.
 Elements in a 2D array are accessed using a pair of
indices, one for the row and one for the column, the first
number indicating the row and the second one indicating
the column.
int arr [2][3]
This array can store 2*3=6 elements.
You can visualize this 2-D array as a matrix of 2 rows and 3
columns.
Two Dimensional Arrays
Character Arrays and Strings
Character Arrays and Strings
Character Arrays and Strings
Character Arrays and Strings
Character Arrays and Strings
Reading Strings from Terminal: scanf
Reading Strings from Terminal: using gets()
1. To avoid above problem we can use inbuilt function called gets()
stand for get string.
2. It captures the whole strings irrespective of spaces but still it is risky

3. Problem occurs when we entered string bigger than size of the array
resulting run time error
Reading Strings from Terminal: using fgets()
1. The best and safest method to accept a string input from user we
can use inbuilt function called fgets() stand for file get string.

2. Syntax : fgets (array name, size, stdin);


Reading Strings from Terminal: using fgets()
1. The best and safest method to accept a string input from user we
can use inbuilt function called fgets() stand for file get string.

2. Syntax : fgets (array name, size, stdin);


 String Functions 32
33

 String Handling Library


7. strlwr(str1, str2): convert string to lowercase
8. strlup(str1, str2): convert string to up case
9. strrev(str1,str2): reverse string
1 2

?
3 4
Thank You

You might also like