What is Nested Function in C?
Nesting of functions in C is the concept of defining one function inside another. However,
standard C (ANSI C) does not support nested functions directly. Instead, similar behavior can
be achieved using function pointers, static functions, or inline functions.
Some compilers, like GCC, allow nested functions as an extension, but they are not portable
across all C environments.
Does C Support Nested Functions?
No, standard C (ANSI C and ISO C) does not support nested functions. Functions in C
programming must be defined at the global scope and cannot be declared inside other
functions.
However, GCC (GNU Compiler Collection) provides an extension that allows defining
functions inside other functions, but this feature is non-portable and not recommended for
cross-platform development.
Example of Nested Function in C (GCC-specific)
#include <stdio.h>
void outerFunction() {
void innerFunction() { // Nested function (GCC only)
printf("Hello from inner function!\n");
innerFunction(); // Calling inner function
int main() {
outerFunction();
return 0;
Output:
Hello from inner function!
Recursive Functions in C Programming
Recursion in C is a programming technique where a function calls itself to solve
a smaller part of the problem until it reaches a stopping condition. Instead of
repeating code with loops, recursion solves a problem by breaking it down into
simpler versions of the same problem. It continues calling itself with new values
until a specific condition is met, known as the base case.
Functions in C programming are recursive if they can call themselves until the
exit condition is satisfied. If a function allows you to call itself within the
definition in a C program, it is a recursive function. These functions run by
stacking the calls until the exit condition is met and the flow of the program
exits the function. Suppose you want to find the factorial of a number in C; you
can use a recursive function to get the result. Here’s an example to find the
factorial of 8.
1. #include <stdio.h>
int factorial(int n) {
if (n == 0) // Base case
return 1;
else
return n * factorial(n - 1); // Recursive case
}
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
Output:
Let’s calculate the factorial of 8 and confirm if the output is true. The factorial
of the number is calculated as fact(n) = n * (n-1) * (n-2) * … 1. Hence, the
factorial of 8 is 8*7*6*5*4*3*2*1 = 40320. This confirms that our recursive
function worked correctly.
4. Reverse a String Using Recursion
The program below reverses a string in C using recursion:
#include <stdio.h>
void reverse(char str[], int index) {
if (str[index] == '\0')
return;
reverse(str, index + 1);
printf("%c", str[index]);
int main() {
char str[] = "hello";
reverse(str, 0);
return 0;
Types of Recursion in C
The following are the types of recursion in C language with examples:
1. Direct Recursion
When the recursion functions in C call themselves directly, it is known as direct
recursion.
Example:
void directRecursion(int n) {
if (n > 0) {
printf("%d ", n);
directRecursion(n - 1); // Function calling itself directly
Explanation:
The function directRecursion() is directly calling itself with a smaller value.
2. Indirect Recursion
When a function calls another function, and that function again calls the first
function, it’s called indirect recursion.
Example:
void functionA(int n);
void functionB(int n);
void functionA(int n) {
if (n > 0) {
printf("%d ", n);
functionB(n - 1); // Calls another function
void functionB(int n) {
if (n > 0) {
printf("%d ", n);
functionA(n - 1); // Calls the first function again
Explanation:
Here, functionA() and functionB() call each other recursively, forming an
indirect recursion loop.
3. Tail Recursion
A recursion is called tail recursion if the recursive call is the last statement
executed by the function.
Example:
void tailRecursion(int n) {
if (n == 0)
return;
printf("%d ", n);
tailRecursion(n - 1); // Recursive call is the last action
}
Explanation:
Since nothing is done after the recursive call, this is a tail-recursive function.
Tail recursion can be optimized by the compiler.
4. Head Recursion
If the recursive call happens before any other processing in the function, it is
known as head recursion.
Example:
void headRecursion(int n) {
if (n == 0)
return;
headRecursion(n - 1); // Recursive call comes first
printf("%d ", n);
Explanation:
Here, the function first calls itself, and only after the recursion ends, it processes
the printf().
5. Tree Recursion
When a function calls itself more than once in each invocation, it results in
multiple branches, forming a tree-like structure.
Example:
void treeRecursion(int n) {
if (n > 0) {
printf("%d ", n);
treeRecursion(n - 1); // First recursive call
treeRecursion(n - 1); // Second recursive call
}
Explanation:
Each call spawns two more calls, growing exponentially like a tree. Used in
problems like Fibonacci and combinatorics.
6. Nested Recursion
When a recursive function’s argument itself is a recursive function call, it’s
called nested recursion.
Example:
int nestedRecursion(int n) {
if (n > 100)
return n - 10;
else
return nestedRecursion(nestedRecursion(n + 11));
Explanation:
The argument passed to the function is the result of another recursive call,
making it nested.
Function Call by Value in C
a function can be called from any other function, including itself. There are
two ways in which a function can be called − (a) Call by Value and (b) Call by
Reference. By default, the Call by Value mechanism is employed.
Formal Arguments and Actual Arguments
You must know the terminologies to understand how the call by value
method works −
Formal arguments − A function needs certain data to perform its desired
process. When a function is defined, it is assumed that the data values will be
provided in the form of parameter or argument list inside the parenthesis in
front of the function name. These arguments are the variables of a certain
data type.
Actual arguments − When a certain function is to be called, it should be
provided with the required number of values of the same type and in the
same sequence as used in its definition.
type function_name(type var1, type vsnippet − ar2, ...)
#include <stdio.h>
int add(int x, int y){
int z = x + y;
return z;
int main(){
int a = 10, b = 20;
int c = add(a, b);
printf("Addition: %d", c);
#include <stdio.h>
/* function declaration */
void swap(int x, int y);
int main(){
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a: %d\n", a);
printf("Before swap, value of b: %d\n", b);
/* calling a function to swap the values */
swap(a, b);
printf("After swap, value of a: %d\n", a);
printf("After swap, value of b: %d\n", b);
return 0;
void swap(int x, int y){
int temp;
temp = x; /* save the value of x */
x = y; /* put y into x */
y = temp; /* put temp into y */
return;
}
The Call by Reference approach involves passing the address of the variable
holding the value of the actual argument. You can devise a calling method
that is a mix of Call by Value and Call by Reference. In this case, some
arguments are passed by value and others by reference.
Function Call by Reference in C
The Address Operator (&) in C
In C language, a variable is a named memory location. When a variable
declared, the compiler allocates a random location in the memory and
internally identifies the location with the user-defined name.
To fetch the address at which the variable has been created, we use
the address (&) operator.
Example
Take a look at the following example −
#include <stdio.h>
int main(){
int x = 10;
printf("x: %d Address of x: %d", x, &x);
Output
This will print the value of x and its address − x: 10 Address of x: -
1990957196
C Storage Classes (All Types & Examples
Introduction
In C programming, variables behave differently based on where and how they
are declared. This is determined by storage classes in C, which define a
variable's scope, lifetime, visibility, and memory location.
Understanding storage classes helps in writing efficient and optimized
programs. There are four types of storage classes in C language: auto, register,
static, and extern, each serving a specific purpose in memory management and
data accessibility.
Let’s learn everything about these storage classes in detail, their usage with
examples, and how they impact program execution and variable behavior.
What Are Storage Classes in C?
A storage class in C defines how a variable is stored in memory, its scope
(visibility), lifetime (existence duration), and accessibility within a program. It
determines whether a variable is local or global, whether its value persists
between function calls, and where it is stored (RAM or CPU register).
You can think of storage classes like employee roles in a company. Some
employees work temporarily (auto), some retain their tasks daily (static), some
work at the front desk for quick access (register), and some share resources
across teams (extern).
Types of Storage Classes in C
There are 4 types of storage classes in C programming:
1. auto for temporary local variables.
2. register for high-speed variables stored in CPU registers.
3. static for persistent local or global variables.
4. extern for sharing global variables across multiple files.
Auto Storage Classes in C
The auto storage class in C is the default storage class for local variables.
Scope: Block (local to function).
Lifetime: Created when function is called, destroyed when function exits.
Memory Location: RAM.
Example of auto storage class in C:
#include <stdio.h>
void exampleFunction() {
auto int num = 10; // 'auto' keyword is optional
printf("Value of num: %d\n", num);
int main() {
exampleFunction();
return 0;