Arrays in C
Definition:
An array in C is a collection of variables of the same data type stored at contiguous memory
locations. It allows storing multiple values under a single variable name, accessed using
indices. Arrays help in efficiently managing large amounts of data.
Array structure:
Syntax:
data_type array_name[size];
Declaration and Initialization:
Arrays can be declared and initialized in the following ways:
int arr[5]; // Declaration of an integer array of size 5
int arr[5] = {10, 20, 30, 40, 50}; // Declaration with initialization
int arr[] = {1, 2, 3, 4, 5}; // Size is optional if initialized at declaration
Types of Arrays:
1. One Dimensional Array
Definition: A one-dimensional array is a list of elements of the same type stored in a single
row.
Syntax: data_type array_name[size];
Example: 1
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
for(int i=0; i<5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Example 2:
#include <stdio.h>
int main() {
int arr[5];
int i;
printf("Enter 5 integers:\n");
for(i = 0; i < 5; i++) {
scanf("%d", &arr[i]); // taking input
printf("You entered:\n");
for(i = 0; i < 5; i++) {
printf("%d ", arr[i]); // displaying values
return 0;
2. Two Dimensional Array
Definition: A two-dimensional array is like a matrix, stored in rows and columns.
Syntax: data_type array_name[rows][columns];
Example 1:
#include <stdio.h>
int main() {
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
for(int i=0; i<2; i++) {
for(int j=0; j<3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Example 2:
#include <stdio.h>
int main() {
int mat[2][3];
int i, j;
printf("Enter elements for a 2x3 matrix:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
scanf("%d", &mat[i][j]); // taking input
printf("Matrix is:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", mat[i][j]); // displaying matrix
printf("\n");
return 0;
}
3. Three Dimensional Array
Definition: A three-dimensional array is an array of two-dimensional arrays, useful for
representing 3D data (like cubes).
Syntax: data_type array_name[x][y][z];
Example 1:
#include <stdio.h>
int main() {
int arr[2][2][2] = {
{ {1,2}, {3,4} },
{ {5,6}, {7,8} }
};
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
for(int k=0; k<2; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
}
return 0;
}
Example 3:
#include <stdio.h>
int main() {
int arr[2][2][2];
int i, j, k;
printf("Enter 8 elements for a 2x2x2 array:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
for(k = 0; k < 2; k++) {
scanf("%d", &arr[i][j][k]); // taking input
printf("3D Array elements:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
for(k = 0; k < 2; k++) {
printf("arr[%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);
return 0;
Functions arguments in C (According to Arguments and Return Value)
In C, functions can be categorized into four types based on whether they take arguments
and whether they return a value. These types help in organizing code, reusability, and
proper implementation of logic.
1. Function with Arguments and Return Value
Definition: A function that takes parameters (arguments) and also returns a value to the
calling function.
Syntax:
return_type function_name(data_type arg1, data_type arg2, ...);
{
// function body
return value;
}
Example:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("Sum = %d", result);
return 0;
}
2. Function with Arguments and No Return Value
Definition: A function that takes parameters (arguments) but does not return any value.
Syntax:
void function_name(data_type arg1, data_type arg2, ...);
{
// function body
}
Example:
#include <stdio.h>
void printSum(int a, int b) {
printf("Sum = %d", a + b);
}
int main() {
printSum(5, 3);
return 0;
}
3. Function with No Arguments and With Return Value
Definition: A function that does not take any arguments but returns a value.
Syntax:
return_type function_name();
{
// function body
return value;
}
Example:
#include <stdio.h>
int getNumber() {
return 10;
}
int main() {
int num = getNumber();
printf("Number = %d", num);
return 0;
}
4. Function with No Arguments and No Return Value
Definition: A function that neither takes arguments nor returns any value.
Syntax:
void function_name();
{
// function body
}
Example:
#include <stdio.h>
void greet() {
printf("Hello, World!");
}
int main() {
greet();
return 0;
}