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

Unit 2 - Programming in C

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

Unit 2 - Programming in C

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

UNIT II

DECISION CONTROL, LOOPING STATEMENTS, FUNCTIONS, AND ARRAYS

Conditional Branching statements, Iterative statements, Nested loops, The Break and continue statements, Goto
statements; Introduction to Functions: Function declaration, Function definition, Function call, return statement,
passing parameters to the function, Recursive Functions; Introduction to Arrays: Declaration, Accessing the
Elements, storing values, operations on arrays, Passing Arrays to functions, two-dimensional array,
Multidimensional arrays.

2.1 Decision Making within a Program


• Decision making is the selection of a course of action from among available alternatives in order to produce a
desired result.
• The conditional test either evaluates to a true or a false.
• The concept of evaluating and obtaining a result is referred to as decision making in a programming language.
Control Statement
• A control statement modifies the order of statement execution.
• A control statement can cause other program statements to execute multiple times or not to execute at all,
depending on the circumstances.
Types of Decision Control Statements
1) Branching Statement : used to select one of the alternative statement
a. Unconditional Branching
i. goto Statement
ii. break Statement
iii. continue Statement
b. Conditional Branching
i. if Statement
ii. if-else Statement.
iii. if-else-if Statement
iv. switch Statement.
2) Looping or Iterative Statement : used to repeat the statement till the condition is true.
i. for loop
ii. while loop
iii. do while loop

2.1.1 Conditional Branching statements


Conditional branching statements are used to execute particular code blocks based on a condition. These
branching instructions in C allow programmers to run the code only when specific conditions are met.
The various categories of conditional branching statements in C are as follows:
i. if Statement
ii. if-else Statement.
iii. if-else-if Statement
iv. switch Statement.
The if Statement:
The most fundamental branching construct in C is the "if" statement. If a given condition is met, it permits the
execution of a block of code. The "if" statement has the following syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
#include <stdio.h>
void main() {
int num = 10;
if (num > 0) {
printf("The number is positive.\n");
}
}
Output:
The number is positive.
The if-else Statement:
By offering a different block of code to run when the condition is false, the "if-else" statement builds on the "if"
statement. The "if-else" expression has the following syntax:
if (condition) {
// Code to execute if condition is true
}
else {
// Code to execute if condition is false
}

Example:
#include <stdio.h>
void main() {
int num = -5;
if (num > 0) {
printf("The number is positive.\n");
}
else {
printf("The number is non-positive.\n");
}
}
Output:
The number is non-positive.
The if-else-if Statement
if else if statement in C programming is used to test a series of conditions sequentially. Furthermore, if a condition
is tested only when all previous if conditions in the if-else ladder are false. If any of the conditional expressions
evaluate to be true, the appropriate code block will be executed, and the entire if-else ladder will be terminated.
if else if expression has the following syntax:
if (condition) {
// Code to execute if condition is true
}
else if (condition) {
// Code to execute if condition is true
}
else {
// Code to execute if all the above conditions are false
}

Example:
#include <stdio.h>
void main() {
int a=5,b=10,c=8;
if (a>b && a>c) {
printf("a is greater\n");
}
else if(b>c){
printf("b is greater");
}
else {
printf("c is greater");
}
}
Output:
b is greater
The switch Statement:
Multiple actions can be taken using the "switch" statement based on the value of a variable or expression. When
managing several instances, it is especially helpful. The "switch" statement has the following syntax:
switch (expression) {
case constant1:
// Code to execute if expression matches constant1
break;
case constant2:
// Code to execute if expression matches constant2
break;
// More cases...
default:
// Code to execute if expression doesn't match any constant
}

Example:
#include <stdio.h>
void main() {
int num = 2;
switch (num) {
case 1:
printf("Value is 1\n");
break;
case 2:
printf("Value is 2\n");
break;
case 3:
printf("Value is 3\n");
break;
default:
printf("Value is not 1, 2, or 3\n");
break;
}
return 0;
}
Output:
Value is 2
2.1.2 Unconditional Branching Statements:
C uses unconditional branching statements to alter the typical course of program execution. These statements
give programmers the freedom to jump to a specific location in their code under any circumstance. The various
categories of unconditional branching statements in C are as follows:
i. goto Statement
ii. break Statement
iii. continue Statement
The goto Statement:
The "goto" command is used to transfer the program control to a predefined label. It is an unconditional branching
statement.

Example:
#include <stdio.h>
void main()
{
int age;
printf("Enter your age: ");
scanf("%d",&age);
vote:
printf("You are eligible to vote");
novote:
printf("You are not eligible to vote");
if(age>=18)
goto vote;
else
goto novote;
}
Output:
Enter your age: 19
You are eligible to vote
The 'break' Statement:
The "break" statement is frequently employed in switch statements as well as looping constructions like "for",
"while", and "do-while". It enables you to skip the statement that follows the loop or switch and end the execution
of the closest enclosing loop or switch statement. The "break" statement in C is written in the following syntax.
break;

Example:
#include <stdio.h>
void main() {
int i;
for (i = 1; i< =10; i++) {
if (i == 5)
break;
printf("%d\t", i);
} }
Output:
1 2 3 4
The continue Statement:
In the C programming language, the continue statement is used to go to the next iteration of a loop while skipping
the current iteration. Usually, this statement is used in loops like for or while. Syntax of continue statement in C
is as follows:
continue;

Example:
#include <stdio.h>
void main() {
int i;
for (i = 1; i< =10; i++)
{
if (i == 5)
break;
printf("%d\t", i);
}
}
Output:
1 2 3 4 6 7 8 9 10

2.1.3 Iterative statements


The Iterative statements can be defined as repeating the same process multiple times until a specific condition
satisfies. They are also called as looping statements. There are three types of loops used in the C language.
i. while
ii. do while
iii. for
The while loop
The block of statements is executed in the while loop until the condition specified in the while loop is satisfied.
It is also called a pre-tested loop. The syntax of while loop in c language is given below:
while(condition){
//code to be executed
}
Example:
#include <stdio.h>
void main(){
int i = 0;
while(i<3) {
printf("Hello World\n");
i++;
}
}
Output:
Hello World
Hello World
Hello World

The do-while loop


The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is used when it
is necessary to execute the loop at least once. The syntax of do-while loop in c language is given below:
do{
//code to be executed
}while(condition);

Example:
#include <stdio.h>
void main(){
int i = 0;
do {
printf("Hello World\n");
i++;
} while (i < 3);
}
Output:
Hello World
Hello World
Hello World
The for loop in C
The for loop is used in the case where we need to execute some part of the code until the given condition is
satisfied. The for loop is also called as a pre-tested loop. The syntax of for loop in c language is given below:
for(initialization;condition;incr/decr){
//code to be executed
}
Example:
#include <stdio.h>
void main()
{
int i;
for(i=0;i<3;i++)
{
printf("Hello World\n");
}
}
Output:
Hello World
Hello World
Hello World

while Loop do-While Loop for Loop


Syntax: Syntax: Syntax:
while(condition){ do{ for(initialization;condition;incr/decr){
// statements //statements //code to be executed
} }while(expression); }
Initialization and updating is Initialization and updating is not Initialization and updating is a part
not the part of the syntax the part of the syntax of the syntax
The control will never enter The control will enter in to the
The control will never enter in to the
in to the loop if the condition loop for the first time even if the
loop if the condition is false
is false condition is false
No semicolon is needed after semicolon needed after the end of no semicolon is needed after the end
the end of the condition. the condition of the condition.
pre-tested loop post tested loop pre-tested loop

Nested Loops
Nesting of loops is the feature in C that allows the looping of statements inside another loop.
Syntax of Nested loop
Outer_loop {
Inner_loop {
// inner loop statements.
}
// outer loop statements.
}
Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of
loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for
example, you can define 'while' loop inside a 'for' loop.
Example:
#include <stdio.h>
void main()
{
for(int i=1;i<=3;i++) // outer loop
{
for(int j=1;j<=5;j++) // inner loop
{
printf("*\t"); // printing the value.
}
printf("\n");
}
}
Output:
* * * * *
* * * * *
* * * * *
2.3 Introduction to Functions
In c, we can divide a large program into the basic building blocks known as function. The function contains the
set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and
modularity to the C program. In other words, we can say that the collection of functions creates a program.
Advantage of functions in C
There are the following advantages of C functions.
 By using functions, we can avoid rewriting same logic/code again and again in a program.
 We can call C functions any number of times in a program and from any place in a program.
 We can track a large C program easily when it is divided into multiple functions.
 Reusability is the main achievement of C functions.
 However, Function calling is always an overhead in a C program.
Function Aspects
There are three aspects of a C function.
 Function declaration: A function must be declared globally in a c program to tell the compiler about the
function name, function parameters, and return type.
return_type function_name (argument list);
 Function call: Function can be called from anywhere in the program. The parameter list must not differ in
function calling and function declaration. We must pass the same number of functions as it is declared in
the function declaration.
function_name (argument_list)
 Function definition: It contains the actual statements which are to be executed. It is the most important
aspect to which the control comes when the function is called. Here, we must notice that only one value can
be returned from the function.
return_type function_name (argument list)
{
function body;
}
Types of Functions
There are two types of functions in C programming:
 Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), getch(),
clrscr() etc.
 User-defined functions: are the functions which are created by the C programmer, so that he/she can use it
many times. It reduces the complexity of a big program and optimizes the code.
Return Value
A C function may or may not return a value from the function. If you don't have to return any value from the
function, use void for the return type.
Example without return value:
void hello(){
printf("hello c");
}
If you want to return any value from the function, you need to use any data type such as int, long, char, etc. The
return type depends on the value to be returned from the function.
Example with return value:
int get(){
return 10;
}
In the above example, we have to return 10 as a value, so the return type is int. If you want to return floating-
point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the method.
float get(){
return 10.2;
}
Different aspects of function calling
A function may or may not accept any argument. It may or may not return any value. Based on these facts: There
are four different aspects of function calls.
 function without arguments and without return value
 function without arguments and with return value
 function with arguments and without return value
 function with arguments and with return value
Example for Function without argument and return value
#include<stdio.h>
void add();
void main () {
add();
}
void add() {
int a=5,b=2,c;
c=a+b;
printf("Sum= %d",c); }
Output:
Sum= 7
Example for Function without argument and with return value
#include<stdio.h>
int add();
void main () {
int c=add();
printf("Sum= %d",c);
}
int add() {
int a=5,b=2,sum;
sum=a+b;
return sum;
}
Output:
Sum= 7
Example for Function with argument and return value
#include<stdio.h>
int add(int,int);
void main () {
int a=5,b=2,c;
c=add(a,b);
printf("Sum= %d",c);
}
int add(int x,int y) {
int sum=x+y;
return sum;
}
Output:
Sum= 7
Example for Function with argument and without return value
#include<stdio.h>
void add();
void main () {
int a=5,b=2;
add(a,b);
}
int add(int x,int y) {
int sum=x+y;
printf("Sum= %d",sum); }
Output:
Sum= 7

Passing Parameters to the function


In C programming language, parameters are variables that are used to pass values or references between functions.
There are two types of parameters: actual and formal parameters.
Actual Parameters:
Actual parameters are the values that are passed to a function when it is called. They are also known as arguments.
These values can be constants, variables, expressions, or even other function calls. When a function is called, the
actual parameters are evaluated, and their values are copied into the corresponding formal parameters of the called
function. In C, actual parameters are enclosed in parentheses and separated by commas. For example, consider
the following function call:
int result = add(2, 3);
In this function call, 2 and 3 are the actual parameters, and they are passed to the function add, which takes two
formal parameters.
Formal Parameters:
Formal parameters are the variables declared in the function header that are used to receive the values of the
actual parameters passed during function calls. They are also known as function parameters. Formal parameters
are used to define the function signature and to specify the type and number of arguments that a function can
accept.
In C, formal parameters are declared in the function declaration or definition. For example, consider the following
function declaration:
int add(int a, int b);
In this function declaration, a and b are the formal parameters. They are used to receive the values of the actual
parameters passed during function calls.
Methods of Parameter Passing
There are two methods to pass the data into the function in C language, i.e., call by value and call by reference.
Call by value in C
When passing parameters by value, a copy of the actual parameter's value is made and passed to the function. It
means that any modifications made to the parameter inside the function do not affect the original value of the
actual parameter. It is useful when we want to manipulate the value of a variable inside a function but don't want
to modify the original value outside the function.
Example
#include <stdio.h>
void swap(int , int);
void main()
{
int a = 10,b = 20;
printf("Before swapping \n a = %d, b = %d\n",a,b);
swap(a,b);
}
void swap (int a, int b)
{
int temp
temp = a
a=b
b=temp
printf("After swapping \na = %d, b = %d\n",a,b);
}
Output:
Before swapping
a=10
b=20
After swapping
a=20
b=10
Call by reference in C
In call by reference, the address of the variable is passed into the function call as the actual parameter. It means
that any modifications made to the parameter inside the function affect the original value of the actual parameter.
It is useful when we want to modify the value of a variable outside the function.
Example
#include <stdio.h>
void swap(int , int);
void main()
{
int a = 10,b = 20;
printf("Before swapping \n a = %d, b = %d\n",a,b);
swap(&a,&b);
}
void swap (int *a, int *b) {
int temp;
temp = *a;
*a= *b;
*b=temp;
printf("After swapping \na = %d, b = %d\n", *a, *b);
}
Output:
Before swapping
a=10
b=20
After swapping
a=20
b=10
Recursive Functions
Any function which calls itself is called recursive function, and such function calls are called recursive calls.
Recursion involves several numbers of recursive calls. However, it is important to impose a termination condition
of recursion. Recursion code is shorter than iterative code however it is difficult to understand.
#include<stdio.h>
int fibonacci(int);
void main () {
int i,n,f;
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
f = fibonacci(n);
printf("%d",f);
}
}
int fibonacci (int n) {
if (n==0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}
Output:
Enter the value of n: 6
0 1 1 2 3 5
2.3 Introduction to Arrays
An array is defined as the collection of similar type of data items stored at contiguous memory locations. The
array is the simplest data structure where each data element can be randomly accessed by using its index number.
Properties of Array
The array contains the following properties.
 Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes.
 Elements of the array are stored at contiguous memory locations where the first element is stored at the
smallest memory location.
 Elements of the array can be randomly accessed since we can calculate the address of each element of the
array with the given base address and the size of the data element.
Advantages
1. Code Optimization: Less code to the access the data.
2. Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3. Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4. Random Access: We can access any element randomly using the array.
Disadvantage
1. Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the limit. So,
it doesn't grow the size dynamically like LinkedList which we will learn later.
Declaration of C Array
We can declare an array in the c language in the following way.
data_type array_name[array_size];
Now, let us see the example to declare the array.
int marks[5];
Initialization of C Array
The simplest way to initialize an array is by using the index of each element. We can initialize each element of
the array by using the index. Consider the following example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;

Example
#include<stdio.h>
void main()
{
int i, marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
for(i=0;i<5;i++){ //traversal of array
printf("%d \t",marks[i]);
}
}
Output
80 60 70 85 75
Declaration with Initialization
We can initialize the c array at the time of declaration. Let's see the code.
int marks[5]={20,30,40,50,60};
Example
#include<stdio.h>
int main()
{
int i=0;
int marks[5]={80,60,70,85,75};//declaration and initialization of array
for(i=0;i<5;i++){ //traversal of array
printf("%d \n",marks[i]);
}
}
Output
80 60 70 85 75
2.3.1 Operations on arrays
 Traverse
 Insert − at the specified index, adds an element.
 Delete− the element at the specified index is deleted.
 Search − Uses the provided index or the value to search for an element.
 Merge – Combines two arrays.
 Sort – Sort the given array in ascending or descending order
Traversal
 Accessing each element in the array one by one.
Example
#include<stdio.h>
int main(){
int i=0;
int arr[5]={80,60,70,85,75};//declaration and initialization of array
for(i=0;i<5;i++){ //traversal of array
printf("Array[%d]: %d \n",i,arr[i]);
}
}
Output
Array[0]: 80
Array[1]: 60
Array[2]: 70
Array[3]: 85
Array[4]: 75
Insertion
− at the specified index, adds an element.
Let's take an array of 5 integers.
1, 20, 5, 78, 30.
If we need to insert an element 100 at position 2, the execution will be,
1. We need to insert element 100 at position 2.

2. Move all the elements from the last index(4) to the position(2) to one position right.
arr[4] (30) will be placed in arr[5].
arr[3] (78) will be placed in arr[4].
arr[2] (5) will be placed in arr[3].
2. Finally, the element 100 is placed at the position 2.

Example
#include<stdio.h>
# define size 5
void main()
{
int arr[size] = {1, 20, 5, 78, 30};
int element, pos, i;
printf("Enter position and element:");
scanf("%d%d",&pos,&element);
if(pos <= size && pos >= 0)
{
//shift all the elements from the last index to pos by 1 position to right
for(i = size; i > pos; i--)
arr[i] = arr[i-1];
//insert element at the given position
arr[pos] = element;
//print the new array
for(i = 0; i <= size; i++)
printf("%d\t", arr[i]);
}
else
printf("Invalid Position\n");
}
Output:
Enter position and element: 2 100
1 20 100 5 78 30
Searching in an Array
− Uses the provided index or the value to search for an element.
Let's take an array of 5 elements.
34, 2, 23, 100, 60.
If we search element 100, the execution will be,
1. 34 != 100. Move to the next element.

2. 2 != 100. Move to the next element.

3. 23 != 100. Move to the next element.


3. 100 == 100. Search Found.

Example
#include<stdio.h>
# define size 5
void main()
{
int arr[size] = {1, 20, 5, 78, 30};
int key, i, index = 0;
printf("Enter element to search: ");
scanf("%d",&key);
for(i = 0; i < size; i++) {
if(arr[i] == key) {
index = i;
break;
}
}
if(index != 0) {
printf("Element is found at index: %d",index);
}
else
printf("Element Not Found\n");
}
Output:
Enter element to search: 5
Element is found at index: 2
Deletion
− the element at the specified index is deleted.
Let's take an array of 5 elements.
1, 20, 5, 78, 30.
If we remove element 20 from the array, the execution will be,
1. We need to remove the element 20 (index 1) from the array.

2. Shift all the elements from index + 1 (index 2 to 4) by 1 position to the left.
arr[2] (value 5) will be placed in arr[1].
arr[3] (value 78) will be placed in arr[2].
arr[4] (value 30) will be placed in arr[3].

3. Finally, the new array.


Example
#include<stdio.h>
# define size 5
void main()
{
int arr[size] = {1, 20, 5, 78, 30};
int key, i, index = 0;
printf("Enter element to delete: ");
scanf("%d",&key);
/* iterate the array elements using loop
* if any element matches the key, store the index */
for(i = 0; i < size; i++)
{
if(arr[i] == key)
{
index = i;
break;
}
}
if(index != 0)
{
//shift all the element from index+1 by one position to the left
for(i = index; i < size - 1; i++)
arr[i] = arr[i+1];
printf("New Array : ");
for(i = 0; i < size - 1; i++)
printf("%d\t",arr[i]);
}
else
printf("Element Not Found\n");
}
Output:
Enter element to delete: 20
New Array : 1 5 78 30
Merge two Arrays
– Combines two arrays.
Example
#include<stdio.h>
# define size1 3
# define size2 3
void main()
{
int arr1[size1] = {1,20,5};
int arr2[size2] = {100,78,30};
int tot=size1+size2;
int arr3[tot],key, i, index = 0;
for(i = 0; i < size1; i++)
{
arr3[i]=arr1[i];
}
for(i = size1; i < tot; i++)
{
arr3[i]=arr2[i-size2];
}
for(i = 0; i < tot; i++)
{
printf("%d\t",arr3[i]);
}
}
Output:
1 20 5 100 78 30
Sorting an array values
– Sort the given array in ascending or descending order
Example
#include<stdio.h>
int main(){
int temp,i,j;
int arr[5]={80,60,70,85,75};
printf("Before Sorting\n");
for(i=0;i<5;i++){
printf("\tArray[%d]: %d \n",i,arr[i]);
}
for(i=0;i<5;i++){
for(j=i+1;j<5;j++){
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("After Sorting\n");
for(i=0;i<5;i++){
printf("\tArray[%d]: %d \n",i,arr[i]);
}
}
Output:
Before Sorting
Array[0]: 80
Array[1]: 60
Array[2]: 70
Array[3]: 85
Array[4]: 75
After Sorting
Array[0]: 60
Array[1]: 70
Array[2]: 75
Array[3]: 80
Array[4]: 85
2.3.2 Passing Arrays to functions
In C, there are various general problems which requires passing more than one variable of the same type to a
function. For example, consider a function which sorts the 10 elements in ascending order. Such a function
requires 10 numbers to be passed as the actual parameters from the main function. Here, instead of declaring 10
different numbers and then passing into the function, we can declare and initialize an array and pass that into the
function.
Example
#include<stdio.h>
int minarray(int arr[],int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++)
{
if(min>arr[i])
{
min=arr[i];
}
}
return min;
}
void main()
{
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};
min=minarray(numbers,6);
printf("minimum number is %d \n",min);
}
Output:
minimum number is 3
2.3.4 Two Dimensional Array
The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can
be represented as the collection of rows and columns. The syntax to declare the 2D array is given below.
data_type array_name[rows][columns];
Here, 4 is the number of rows, and 3 is the number of columns.
Initialization of 2D Array
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Example 1
#include<stdio.h>
void main()
{
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}
}
}
Output
arr[0] [0] = 1
arr[0] [1] = 2
arr[0] [2] = 3
arr[1] [0] = 2
arr[1] [1] = 3
arr[1] [2] = 4
arr[2] [0] = 3
arr[2] [1] = 4
arr[2] [2] = 5
arr[3] [0] = 4
arr[3] [1] = 5
arr[3] [2] = 6
Example 2
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
printf("\nprinting the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}
Output
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34
Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78

printing the elements ....

56 10 30
34 21 34
45 56 78
2.3.3 Multi-Dimensional Array
They allow you to store data in a table-like format, where each row and column can be accessed using an index.
To create a multidimensional array in C, you need to specify the number of dimensions and the size of each
dimension. The general syntax for declaring a multidimensional array is as follows:
data_type array_name[size1][size2]...[sizeN];
Example
#include<stdio.h>
void main(){
int i, j, k;
int arr[3][3][3]= {
{
{11, 12, 13},
{14, 15, 16},
{17, 18, 19}
},
{
{21, 22, 23},
{24, 25, 26},
{27, 28, 29}
},
{
{31, 32, 33},
{34, 35, 36},
{37, 38, 39}
},
};
printf("Printing 3D Array Elements\n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++){
for(k=0;k<3;k++){
printf("%4d",arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
}
Output:
Printing 3D Array Elements
11 12 13
14 15 16
17 18 19

21 22 23
24 25 26
27 28 29

31 32 33
34 35 36
37 38 39

You might also like