0% found this document useful (0 votes)
8 views12 pages

Unit Ii-Arrays

Uploaded by

lakshmi.t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Unit Ii-Arrays

Uploaded by

lakshmi.t
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT II-ARRAYS AND STRINGS

ARRAYS
• An array in C is a fixed-size collection of similar
data items stored in contiguous memory
locations.
• It can be used to store the collection of
primitive data types such as int, char, float,
etc., and also derived and user-defined data
types such as pointers, structures, etc
Array Declaration

• In C, we have to declare the array like any


other variable before using it.
• We can declare an array by specifying its
name, the type of its elements, and the size of
its dimensions.
• When we declare an array in C, the compiler
allocates the memory block of the specified
size to the array name.
• Syntax of Array Declaration
• data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];

• where N is the number of dimensions.


example

• // C Program to illustrate the array declaration


• #include <stdio.h>
• int main()
• {
• // declaring array of integers
• int arr_int[5];
• // declaring array of characters
• char arr char[5];

• return 0;
• }
Array Initialization
• Initialization in C is the process to assign some
initial value to the variable.
• When the array is declared or allocated
memory, the elements of the array contain
some garbage value.
• So, we need to initialize the array to some
meaningful value. There are multiple ways in
which we can initialize an array in C.
• data_type array_name [size] = {value1,
value2, ... valueN};
• // C Program to demonstrate array initialization
• #include <stdio.h>

• int main()
• {

• // array initialization using initializer list


• int arr[5] = { 10, 20, 30, 40, 50 };

• // array initialization using initializer list without


• // specifying size
• int arr1[] = { 1, 2, 3, 4, 5 };

• // array initialization using for loop


• float arr2[5];
• for (int i = 0; i < 5; i++) {
• arr2[i] = (float)i * 2.1;
• }
• return 0;
• }
example
• // C Program to demonstrate the use of array
• #include <stdio.h>

• int main()
• {
• // array declaration and initialization
• int arr[5] = { 10, 20, 30, 40, 50 };

• // modifying element at index 2


• arr[2] = 100;

• // traversing array using for loop


• printf("Elements in Array: ");
• for (int i = 0; i < 5; i++) {
• printf("%d ", arr[i]);
• }

• return 0;
• }
output
• Elements in Array: 10 20 100 40 50

You might also like