Recursion in C Programming

In C programming language, a function that calls itself is known as a recursive function. And, this technique is called as recursion.

Recursive functions are very useful to solve many problems such as  Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.

How recursion works?

Example: Write a program in a C language to find the factorial using recursion.

OUTPUT

Example: Write a program in a C language to print the Fibonacci series using recursion.

Fibonacci series are 0, 1, 1, 2, 3, 5, 8, …,.

In fibonacci series, except for the first two terms of the series, every other term is the sum of the previous two terms, for example, 5 = 2 + 3.

Difference between structure and union in C

union occupies less memory than structure

union variable can store one value at a time & same memory location, can be used to store multiple types of data.

Union in C Programming

In C programming language, union is a special user defined data type that allows to store different data types in the same memory location.

“union”  uses the same memory location for multiple-purpose.

Or

In a union data type, single variable, i.e., same memory location, can be used to store multiple types of data.

In c programming union is similar to the structure but major difference is that it requires less memory than structure.

Apart from this difference, you can define a union with many variables, but only one member can contain  single value at any given time.

Defining a Union

We can define union with “union” keyword. “union” defines a new user defined data type with one or more than one member.

Syntax of union definition:

In the union’s definition at the end & before the final semicolon, you can specify one or more union variables but it is optional.

Example:

Above define a union type named book having three members i, f, and c.

In the above example variables of Book is book1 and book2.

These variables can store an integer, a floating-point number, or a string of characters.

During the union definition, memory occupied by a union variable will be large enough to hold the largest member of the union.

In the above example, user defined data type Book will occupy 30 bytes of memory space because this is the maximum space which can be occupied by a character string.

Example: Write a program to store and print the following information such as name, roll number, class and section of the student using union.

OUTPUT

In the above example, we can see that the values of name, rollno, and Class of union got corrupted because the final value assigned to the variable has occupied the memory location and this is the reason that the value of Section is getting printed very well.

Note: In union, one variable can contain a single value at any given time which is the main purpose of having unions −

Correct Implementation of Above program

OUTPUT

Structures in C Programming

There are various types of data type such as int, char float etc.

For example:

int  i;      here variable i can store only data of integer type

char c;   here variable c can store only data of character type

float  f; here variable f can store only data of floting-point type

Variable of each data type can store only single type of data.

But,  if we want to store different  type of data in a single variable then we use structure variable.

“structure” is user defined data type that allows to store data items of different data type.

Defining a Structure

We can create a structure, by using the struct keyword.

The struct keyword defines a user defined new data type that can hold more than one member of different data type.

Format of structure is as follows

In a C programming Structures are used to represent a record. Suppose you want to store the information of  a school student such as : −

• Name
• Class
• Section
• Roll Number

Example:

Here s1 and s2 are the structure variable.

Another way of creating a structure variable is:

Example: Write a program to store and print the following information such as name, roll number, class and section of the student using structure.

OUTPUT

Example: Write a program to store and print the following information such as name, roll number, class and section of 2 students using structure.

OUTPUT

Example: Write a program to add two distances which is in feet and inches using structure.

OUTPUT

Matrix Multiplication in C Programming

Example: Write a program to multiply two matrix. Firstly, ask from user order of matrix( number of rows and column) Then take the elements of matrix from user as a input and print the resultant matrix.

Addition of Two Matrix in C Programming

Example: Write a program to add two matrix. Firstly, ask from user order of matrix( number of rows and column). Then take the elements of matrix from user as a input and print the resultant matrix

For example, if a user input order as 3,3, i.e., three rows and three columns and
First matrix
1             2             3
4             5             6
7             8             9

Second matrix:
9             8             7
6             5             4
3             2             1

then the output of the program (addition of the two matrices) is:

10           10           10
10           10           10
10           10           10

OUTPUT

Multi-dimensional Array in C Programming

C programming language also support multidimensional arrays.

Syntax of multidimensional array declaration:

Data_type    array_name[size1][size2]…[sizeN];

For example, if we want to creates a three dimensional integer array −

int  num[5][10][4];

Two-dimensional Arrays

The simplest form of multidimensional array is the two-dimensional array.

if we want to declare a two dimensional integer array of size [x][y] ( where x is a  number of rows and y is a number of columns ) you would write something as follows −

int  a[3][4];

In the above line we have created two-dimensional integer array ”a” with 3 rows and 4 columns.

In a C programming language Multidimensional arrays may be initialized by specifying bracketed values for each row.

For example we want to create an integer array with 3 rows and each row has 4 columns.

Above the nested braces indicates the intended rows.

The following initialization is equivalent to the previous example −

int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};

which indicate the nested braces for intended row, are optional.

Accessing Elements of Two-Dimensional Array

Create a two-dimensional array of size 3*3 and  nested loop is used to handle a two-dimensional array −

Example: Write a program to create 3*3 matrix and take all elements of matrix as an input from user and print it.

Array in C Programming

Array is a kind of data structure that can store a elements of the same type. Arrays stores the elements in a contiguous memory locations.

Array is a collection of similar type elements

For example: we want to declare 100 integer variable

Instead of declaring 100 individual variables, such as

int number0, number1,number2,number3, … …., number99;

you declare one integer array variable such as

int number[100];

here number[100] is an integer array of size 100, means this array can store 100 integer value.

Array indexing starts from index 0 to n-1. Means first integer number store in array number[0], second integer number store in array number[1], third integer number store in array number[2], and so on. Here 100th  number store in array numbers[99].

Declaring Array

Syntax of declare an array in C:

DataType  arrayName [ arraySize ];

This is called a single-dimensional array.

The arraySize is an integer constant greater than zero.

datatype can be any valid C data type.

Example: write a program to create integer array and store 5 integer number and print.

Example: write a program to take 5 number from user and store integer number in integer array and print.

Another way to Initialize Array

We can also initialize array by this way:

int num[5] = { 54 , 4,  13,  2,  17 };

above integer array num[5], stores five numbers. Where num[0] store 54, num[1] store 4….. , & num[4] store 17.

Call by value and Call by reference in C Programming

There are two ways to pass arguments in a function

1. Call by value
2. Call by reference

Lets see above one by one

Call by value

Whenever we call a function and passes the value of variable to the called function is called call by value.

OUTPUT

Call by reference

Whenever we call a function and passes the reference of variable to the called function is called call by reference.

In the call by reference, to pass the reference of the variable we use pointer.

OUTPUT

Functions in C Programming

In a C programming language a function is a group of statements that together perform a special task. In every C program has at least one function, which is main().

In a C library has a numerous predefined or built-in functions. For example, strcat() to concatenate two strings, main() from where program execution starts.

In a C programming function has a 3 part:

• Function declaration
• Function definition
• Function call

Function declaration : function declaration tells the compiler about a function’s name, return type, and parameters.

A syntax of function declaration –

return_type   function_name( parameter list );

Example of  the function declaration is as follows −

int max(int num1, int num2);

here int is a return type of function which tells function will return integer value and max is a name of the function.

“ int num1, int num2” is a parameter list which tells max function will receive two integer arguments.

Arguments are optional which depends on program requirements.

Function definition : function definition is an actual body of the function.

Syntax of function definition:

function definition consist a return type, function name, parameter list and body of the function.

Function call

whenever  program calls a function, the flow of control is transferred to the called function definition.

A called function performs a defined task and when flow of control reached to function-ending closing brace, flow of control returns to back to the main program.

Function can be categories as:

• Function with no argument, no return value
• Function with  argument but  no return value
• Function with  argument and  return value
• Function with no argument, but  return value

Function  with  no  argument  & no return value

Example: Write a program to find the area of circle using function with no argument, no return value.

The formula for the area of the circle is :

Area_circle = Π * r * r

Program

Output

Function  with  argument  but  no return value

Example: Write a program to find the area of circle using function with argument but no return value.

Output

Function  with  argument  &  return value

Example: Write a program to find the area of circle using function with argument but return value.

Output

Function with no argument but  return value

Example: Write a program to find the area of circle using function with no argument but return value.

Output