unit 4
unit 4
FUNCTIONS
Recursion/Recursive Function
Ex:
Recursion is the process of repeating items in a self-similar way.
In programming languages, if a program allows you to call a function inside the same function,
then it is called a recursive call of the function.
Syntax:
void recursion()
{
recursion(); /*function calls itself*/
}
int main()
{
recursion();
}
Note:
The C programming language supports recursion, i.e., a function to call itself. But while
using recursion, programmers need to be careful to define an exit condition from the
function, otherwise it will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as
calculating the factorial of a number, generating Fibonacci series, etc.
OUTPUT
#include<stdio.h>
int Fibonacci(int i)
{
if(i == 0)
{
return 0;
}
if(i == 1)
{
return1;
}
return Fibonacci(i-1)+Fibonacci(i-2);
}
int main()
{
int i;
printf("Recursion of Fibonacci Series\n");
for(i=0;i<10;i++)
{
printf("%d\t\n", Fibonacci(i));
}
return 0;
}
OUTPUT
Syntax:
data type *pointer_variable_name;
Example :
int *p;
float *f;
EX:
#include <stdio.h>
int main()
{
int num = 42; // Declare and initialize an integer variable
int *ptr; // Declare a pointer to an integer
ptr = # // Initialize the pointer with the address of num
printf("Value of num: %d\n", *ptr);
*ptr = 100;
printf("Modified value of num: %d\n", num);
printf("Address of num: %p\n", (void*)&num);
printf("Address stored in ptr: %p\n", (void*)ptr);
OUTPUT:
Value of num: 42
Modified value of num: 100
Address of num: 0x7ffc5a7a0c4c
Address stored in ptr: 0x7ffc5a7a0c4c
The value of the variable is accessed through dereference operator (*) as below:
EX:
#include<stdio.h>
void main()
{
int a = 10;
int *ptr;
ptr=&a;
printf("\n*********Pointer Demo*********\n");
printf("\n Accessing address and value of variable through variable\n");
printf("The address of variable a is:%d\n", &a);
printf("The value of the variable a is:%d\n", a);
printf("\n Accessing address and value of variable through normal pointer
variable\n"); printf("The address of variable a is :%d\n", ptr);
printf("The address of variable a is : %p\n", ptr);
printf("The value of the variable a is : %d\n", *ptr);
}
OUTPUT:
Pointers can be used to pass information back and forth between the calling
Pointers helps us to build complex data structures like linked list, stack, queues,
Disadvantages of pointers in c
Uninitialized pointers might cause segmentation fault.
Dynamically allocated block needs to be freed explicitly. Otherwise, it would lead to memory
leak.
Pointers are slower than normal variables.
If pointers are updated with incorrect values, it might lead to memory corruption.
Basically, pointer bugs are difficult to debug. Its programmer’s responsibility to use
Defining a Structure
To define a structure, we must use the struct statement. The struct statement defines a new
data type, with more than one member.
When a structure is defined, it creates a user-defined type but, no storage or memory is allocated,
but for structure variables memory gets created.
#include <stdio.h>
struct Student
{
char name[50];
int rollNumber;
float marks;
};
int main()
{
// Declare a structure variable
struct Student student;
// Input student information
printf("Enter student name: ");
fgets(student.name, sizeof(student.name), stdin); // Read a string including spaces
printf("Enter roll number: ");
scanf("%d", &student.rollNumber);
printf("Enter marks: ");
scanf("%f", &student.marks);
// Display student information
printf("\nStudent Information:\n");
printf("Name: %s", student.name);
printf("Roll Number: %d\n", student.rollNumber);
printf("Marks: %.2f\n", student.marks);
return 0;
}
OUTPUT:
Unions are derived data types like structure, Creation and accessing union variables is
similar to the accessing and creating of structures.
Defining a Union
Union variables are created in same manner as structure variables. The keyword union is used to
define unions in C language.
Syntax:
The "union tag" is optional and each member definition is a normal variable definition,
such as "int i;" or "float f;" or any other valid variable definition.
At the end of the union's definition, before the final semicolon, you can specify one or
more union variables.
EXAMPLE:
#include <stdio.h>
union Data
{
int intValue;
float floatValue;
char charValue;
};
int main()
{
// Declare a union variable
union Data data;
// Assign values to the union members one at a time
data.intValue = 42;
OUTPUT:
Structure Union
To define a structure, we utilize the struct statement To define a union, we use the union keyword
Every member has their own memory place A memory location is shared by all data
members
A change in the value of one data member has no A change in one data member’s value has
effect on the structure’s other data members an impact on the value of other data
members
Multiple members can be initialized at the same Only the first member can be initialized at a
time time
Multiple values of various members can be stored in For all of its members, a union stores one value
a structure at a time
The overall size of a structure is the sum of the sizes The biggest data member determines the
of all data members total size of a union
At any moment, users can access or recover any Only one member can be accessed or retrieved at
member a time