Understanding Arrays and File Operations in C
1. What is an Array?
An array is a data structure that stores a collection of elements, all of the same data type,
in a contiguous block of memory. Each element in the array can be accessed using an index or a
subscript.
Arrays are useful when you need to store multiple values of the same type and want to access them
efficiently by
their position in the list (index).
2. Array Declaration and Initialization
In C, an array is declared by specifying the type of elements it will store, followed by the
name of the array and its size (the number of elements it can hold).
The general syntax for declaring an array is:
type array_name[size];
For example:
int arr[5]; // Declares an array of integers with 5 elements
### Static Initialization:
An array can be initialized at the time of its declaration with specific values:
int arr[5] = {1, 2, 3, 4, 5};
If you omit the size, the compiler automatically determines it:
int arr[] = {1, 2, 3, 4, 5};
### Dynamic Initialization:
You can also assign values after declaring the array:
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
3. Example Program to Demonstrate Array Declaration and Initialization
#include <stdio.h>
int main() {
// Static initialization of an array
int arr1[5] = {1, 2, 3, 4, 5};
// Dynamic initialization of an array
int arr2[5];
arr2[0] = 10;
arr2[1] = 20;
arr2[2] = 30;
arr2[3] = 40;
arr2[4] = 50;
// Print the arrays
printf("Array 1 (Static Initialization): ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr1[i]);
printf("\nArray 2 (Dynamic Initialization): ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr2[i]);
return 0;
4. Output:
Array 1 (Static Initialization): 1 2 3 4 5
Array 2 (Dynamic Initialization): 10 20 30 40 50
5. File Operations in C
In C, files are handled through the File I/O functions provided by the <stdio.h> library.
The basic operations include opening a file, reading from and writing to a file, and closing the file.
To open a file in C, we use the `fopen()` function, which allows a program to access and manipulate
files.
The syntax is:
FILE *fopen(const char *filename, const char *mode);
Common file modes:
- "r" : Open for reading
- "w" : Open for writing
- "a" : Open for appending
- "r+" : Open for both reading and writing
- "w+" : Open for both reading and writing (truncates the file if it exists)
After the file operation, it is important to close the file using `fclose(FILE *stream);`.
6. Example Program for fwrite()
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
FILE *file;
struct Student student1 = {1, "John Doe", 85.5};
// Open file for writing (binary mode)
file = fopen("student_data.bin", "wb");
if (file == NULL) {
printf("Error opening file.\n");
return 1; // Exit if the file could not be opened
// Write data to the file using fwrite()
size_t result = fwrite(&student1, sizeof(struct Student), 1, file);
if (result == 1) {
printf("Data written successfully to the file.\n");
} else {
printf("Error writing data to the file.\n");
// Close the file
fclose(file);
return 0;
7. Conclusion
In this document, we have explored:
1. The concept of arrays, their declaration, and initialization in C.
2. File operations, specifically how to write data to a file using `fwrite()`.
These concepts are fundamental in C programming, enabling efficient data handling and storage.