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

CC102 Array Declaration and Initialization

The document provides an overview of array declaration and initialization in the C programming language, detailing key terminologies, syntax, and examples. It explains the concepts of data types, size, index, and various initialization methods, along with the advantages and disadvantages of using arrays. Additionally, it includes a practical exercise to write a C program for calculating the sum of array elements.

Uploaded by

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

CC102 Array Declaration and Initialization

The document provides an overview of array declaration and initialization in the C programming language, detailing key terminologies, syntax, and examples. It explains the concepts of data types, size, index, and various initialization methods, along with the advantages and disadvantages of using arrays. Additionally, it includes a practical exercise to write a C program for calculating the sum of array elements.

Uploaded by

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

Array Declaration and

Initialization
Computer Programming 1
BSCS – 1B

John Eric U. Tablada


Mark Angelo P. Solsona
Jet Aiden B. Ubalde
Mark Ian S. Zuniega
Key Terminologies in Array Declaration and
Initialization (C Language)
1. Array
2. Array Declaration
3. Array Initialization
4. Data Type
5. Size
6. Index
7. Full Initialization
8. Partial Initialization
9. Implicit Size
10.Character Arrays
(Strings)
C 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.

C Array Declaration
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 for Array Declaration

data_type: The type of data the array will store (e.g., int, float, char).
array_name: The name used to reference the array.
array_size: The number of elements the array can hold (must be a constant or a macro in standard C).
Example of Array Declaration

Here, memory is allocated for 5 integers, but the values are uninitialized (contain garbage values).

Data Type
Specifies the type of data (e.g., int, float, char) the array will hold.
Example: int arr[5];
This declares an array of integers named arr with 5 elements.

Size
The size of an array refers to the number of elements it can hold. It is specified when declaring the array
and must be a constant value. The size determines the array's memory allocation.
Example: int arr[10];
This array can store up to 10 integer values.

Index
The index is the position of an element within an array. It is used to access or modify elements.
C arrays are zero-indexed, meaning:
The first element is at index 0.
The last element is at index size - 1.
C 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.
Syntax for Array Initialization

A. Full Initialization B. Partial Initialization


Provide initial values for all elements in the array. If fewer values are provided during initialization than the
size of the array, the remaining elements are set to 0.
* The rest are initialized to
This creates an array of size 5 with the specified values. 0 making it equivalent to:
{1, 2, 0, 0, 0}
C. Implicit Size
D. Character Arrays (Strings)
Omit the size of the array and let the compiler
For character arrays, you can initialize them with a string literal
determine it based on the number of values provided.
Full Code Examples of Array Declaration and Initialization
1. Integer Array

Output

2. Float Array

Output
Full Code Examples of Array Declaration and Initialization
3. Array for Calculating Average

Output
Advantages of Arrays in C
1. Efficient Memory Usage: Arrays provide a way to store data sequentially, reducing memory
wastage.

2. Easy Data Access: Elements can be accessed directly using their indices.

3. Compact Code: Using loops, operations can be performed on multiple elements, reducing
repetitive code.

4. Static Storage: Data is allocated contiguously, enabling faster access and manipulation.

5. Ideal for Fixed-Size Data: Arrays are perfect when the number of elements is known in
Disadvantages
advance.
of Arrays in C
1. Fixed Size: The size of an array must be defined at the time of declaration, limiting flexibility.

2. Homogeneous Data: Arrays can only store elements of the same data type.

3. No Built-in Bounds Checking: Accessing indices outside the declared range can lead to
undefined behavior.

4. Insertion and Deletion: These operations are time-consuming as they require shifting elements.

5. Memory Allocation: If improperly handled, large arrays can lead to excessive memory usage.
Write a C program to calculate the sum of all elements
in the array.

Do the following operations to type the code


1. Declare an integer array of size 6.
2. Initialize the array with the values {8, 16, 24, 32, 40,
48}
3. Make sure to print each element of the array with it’s
index and the sum of all elements must be shown
Thank You

You might also like