Back to C Programming
2026-07-145 min read

Example 1: Array Input/Output (C Programming)

Learn Example 1: Array Input/Output (C Programming) step by step with clear examples and exercises.

Why This Matters

In this full guide, we'll delve into the essential topic of working with arrays for input and output operations in C programming. We'll cover the core concept, a worked example, common mistakes, practice questions, and frequently asked questions to help you master this fundamental skill.

Why This Matters

Arrays are a cornerstone data structure in C that enable storing multiple values of the same type under a single variable name. They play a crucial role in handling large amounts of data efficiently, making them indispensable for various real-world applications like sorting algorithms and matrix operations. Understanding arrays is vital for acing programming exams, job interviews, and solving complex coding challenges.

Prerequisites

Before diving into arrays, it's essential to have a solid understanding of the following C concepts:

  1. Variables and data types
  2. Basic input/output (printf(), scanf())
  3. Control structures (if-else, for loops)
  4. Pointers (optional but recommended)

Core Concept

Declaring and Initializing Arrays

To declare an array in C, you specify its data type, array name, and size enclosed within square brackets:

dataType arrayName[arraySize];

For example, to create an array of 5 integers, use the following syntax:

int marks[5];

You can initialize the elements of an array during declaration by providing a comma-separated list of values within curly braces:

int marks[] = {10, 20, 30, 40, 50};

Accessing Array Elements

To access an array element, use its index (starting from 0). The last index is arraySize - 1. For example:

printf("%d", marks[2]); // Output: 30

Inputting Data into Arrays

Use the scanf() function to read data into an array. Remember that you should always check for successful input by verifying the return value of scanf(). Here's an example:

scanf("%d", &marks[i]);
if (scanf("%d", &marks[i]) != 1) {
printf("Invalid input. Exiting...\n");
exit(0);
}

Outputting Array Elements

To display the contents of an array, loop through each element using a for loop or while loop:

for (int i = 0; i < 5; i++) {
printf("%d ", marks[i]);
}

Multi-dimensional Arrays

To declare a multi-dimensional array, specify multiple sizes separated by commas:

dataType arr[rows][columns];

For example, to create a 2D array of 3x3 integers:

int matrix[3][3];

Dynamic Memory Allocation for Arrays

You can dynamically allocate memory for arrays using the malloc() function:

dataType *arr = (dataType *)malloc(size * sizeof(dataType));

Worked Example

Let's write a program that takes input of 5 numbers and finds the maximum number in the array:

#include <stdio.h>

int main() {
int arr[5];
int max = INT_MIN;

printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
if (arr[i] > max) {
max = arr[i];
}
}

printf("The maximum number is: %d\n", max);
return 0;
}

Common Mistakes

  1. Forgetting to check scanf() return value: Always verify the successful input by checking if the scanf() function returns 1 for each input operation.
  2. Accessing out-of-bounds array elements: Be careful not to access elements outside the declared array size (index < arraySize).
  3. Not initializing variables: Always initialize your variables before using them, especially when dealing with arrays.
  4. Confusing array indices: Remember that array indices start from 0, not 1.
  5. Incorrectly comparing floating-point numbers: Floating-point comparisons should use == instead of =.
  6. Not handling edge cases: Ensure your code can handle empty arrays or arrays with a single element.
  7. Not freeing dynamically allocated memory: If you allocate memory using malloc(), don't forget to deallocate it using free() before the program ends.
  8. Using incorrect data types for array elements: Be sure to use the appropriate data type for each array, such as int, float, char, or custom structs.

Practice Questions

  1. Write a program to find the sum of elements in an array.
  2. Write a program that sorts an array of integers using bubble sort.
  3. Write a program to find the second largest number in an array.
  4. Write a program to reverse the order of elements in an array.
  5. Write a program that checks if an array contains a specific target value.
  6. Write a program to find the average of elements in an array.
  7. Write a program to find the minimum and maximum values in a multi-dimensional array.
  8. Write a program to transpose a multi-dimensional array (swap rows with columns).
  9. Write a program that finds the missing number in an array where one number is missing and the other numbers are sorted in ascending order.
  10. Write a program to find duplicate elements in an array using a hash table or sorting algorithm.

FAQ

  1. How do I declare a 2D array in C?

To declare a 2D array, specify two sizes separated by commas:

dataType arr[rows][columns];
  1. What happens if I try to access an out-of-bounds array element?

Accessing an out-of-bounds array element results in undefined behavior, which can lead to program crashes or security vulnerabilities.

  1. Can I use dynamic memory allocation for arrays in C?

Yes, you can dynamically allocate memory for arrays using the malloc() function:

dataType *arr = (dataType *)malloc(size * sizeof(dataType));
  1. How do I find the average of elements in an array?

To calculate the average, first find the sum of all elements and then divide by the number of elements:

float total = 0;
for (int i = 0; i < size; i++) {
total += arr[i];
}
float avg = total / size;
  1. How do I find the maximum and minimum values in an array?

To find the maximum value, follow a similar approach as in the worked example but initialize max with INT_MIN. For the minimum value, initialize it with INT_MAX and compare using < instead of >.

  1. How do I transpose a 2D array (swap rows with columns)?

To transpose a 2D array, use nested loops to iterate through each element and swap the row index with the column index:

for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < columns; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
  1. How do I find the missing number in a sorted array with one number missing?

To find the missing number, first calculate the sum of all possible numbers from the range and subtract the sum of the given numbers:

int total = 0;
for (int i = min; i <= max; i++) {
total += i;
}
int sum_of_given_numbers = ...; // calculate the sum of given numbers
int missing_number = total - sum_of_given_numbers;