C Fundamentals
C Fundamentals
1. Define Functions.
A function is a set(block) of instructions that are used to perform specified task
which repeatedly occurs in main program. It is a self-contained block of program
statements that performs a particular task.
2. How function works?
Declaration – Function1 .
.
Call Function1 .
. .
.
.
Call Function1
.
.
.
Call Function1
.
.
Output:
Enter value for x:2.3
Smallest integer greater than or equal to 2.300000 = 3.0
Largest integer less than or equal to 2.300000 = 2.0
It says that, the function printf must have a string argument at first, and the rest
will be of any type.
14. What are User-defined functions?
The functions defined by the users according to their requirements are called user-
defined functions or programmer-defined functions.
The statements to perform a particular task, which is repeatedly used in the main
function, can be created as user-defined functions. These functions are useful to break
down a large program in to a number of smaller functions.
15. Give syntax for function declaration.
The general form of a function declaration statement is as follows:
<return_type> <function_name>(<formal_argument_list>)
{
// Body of function
}
<function_name><actual_arguments>
18. Write a program to find sum of two numbers using user defined functions.
//To find sum of two integers
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,sum;
int add(int,int);
clrscr();
printf("\nEnter two integers:");
scanf("%d%d",&x,&y);
sum=add(x,y);
printf("\nSum = %d",sum);
getch();
}
int add(int x,int y)
{
return(x+y);
}
Output
Enter two integers:2 3
Sum = 5
19. How will you classify the user-defined functions?
Depending upon their inputs and outputs, userdefined-functions are classified as,
1. Functions with no input-output
2. Function with inputs and no output
3. Function with inputs and one output
4. Function with inputs and outputs
20. Write a program to swap two numbers without using third variable.
#include<stdio.h>
int main()
{
int x,y;
void swap(int,int);
printf("Enter two numbers:");
scanf("%d%d",&x,&y);
printf("\n\nBefore Swapping:\n\nx = %d\n\ny = %d",x,y);
swap(x,y);
return 0;
}
void swap(int a, int b)
{
a=a+b;
b=a-b;
a=a-b;
printf("\n\nAfter Swapping:\n\nx = %d\n\ny = %d\n\n",a,b);
}
Output
Enter two numbers: 4 7
Before Swapping:
x=4
y=7
After Swapping:
x=7
y=4
21. Define call by reference.
The method of passing arguments by address or reference is also known as call by
address or call by reference. Here, the addresses of the actual arguments are passed to the
formal parameters of the function.
If the arguments are passed by reference, changes in the formal parameters also
make changes on actual parameters.
22. Write a program to swap two numbers by Passing Arguments as
Address/Reference
#include<stdio.h>
int main()
{
int x,y;
void swap(int*,int*);
printf("Enter two numbers:");
scanf("%d%d",&x,&y);
printf("\n\nBefore Swapping:\n\nx = %d\n\ny = %d",x,y);
swap(&x,&y);
printf("\n\nAfter Swapping:\n\nx = %d\n\ny = %d\n\n",x,y);
return 0;
}
void swap(int *a, int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
Enter two numbers: 2 7
Before Swapping:
x=2
y=7
After Swapping:
x=7
y=2
23. Write a program to find minimum value in the array using Array as argument
for function.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,min;
int findmin(int[]); //OR int findmin(int*);
clrscr();
printf("Enter 10 integers:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
min=findmin(a); //Argument as Array Name “a”
printf("Minimum value in the array is: %d",min);
getch();
}
int findmin(int x[]) // Parameter with Array specification
{
int temp,i;
temp=x[0];
for(i=1;i<10;i++)
{
if(temp>x[i])
temp=x[i];
}
return temp;
}
Output
Enter 10 integers:
34
65
78
12
09
23
56
78
89
54
Minimum value in the array is: 9
24. Write a program to find minimum value in a two dimensional array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],i,j,min;
int findmin(int[][2]);
clrscr();
printf("Enter 10 integers:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
min=findmin(a);
printf("Minimum value in the 2-D array is: %d",min);
getch();
}
int findmin(int x[][2])
{
int temp,i,j;
temp=x[0][0];
for(i=1;i<2;i++)
{
for(j=0;j<2;j++)
if(temp>x[i][j])
temp=x[i][j];
}
return temp;
}
Output
Enter 10 integers:
45 78
12 54
Minimum value in the 2-D array is: 12
25. What is the purpose of Default Argument?
It is possible to pass argument values (as constant) at function call. In such case,
we need to use formal variable to receive that constant value.
During function declaration, the argument can be made default by using
initialization syntax within the parameter list.
A function declaration can specify default arguments for all or subset of
parameters.
Default argument function can be called with or without parameters.
If argument is not provided, it considers default value. Otherwise, considers
passed value.
Default argument (Default value) should not be specified in the function
definition.
26. Write a program to find area of circle using default arguments.
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float area(int r,float pi=3.14); // pi value 3.14 is default
argument
clrscr();
printf("Enter radius:");
scanf("%d",&r);
printf("Area of Circle : %2.2f",area(r));
getch();
}
float area(int r,float pi)
{
return(pi*r*r);
}
Output
Enter radius: 2
Area of Circle : 12.56
27. What is Recursion?
When function is defined in terms of itself then it is called as “Recursion
Function”.
Recursive function is a function which contains a call to itself.
A function calls itself is known as recursive function.
Output
Enter the number: 5
Factorial of 5 is 120
31. Give Pattern of Recursive Calls.
Recursive calls are classified as,
1. Linear Recursion
2. Binary Recursion
3. N-ary Recursion
4 * factorial (3)
3 * factorial (2)
2 * factorial (1)
1 * factorial (0)
33. Give example for Binary Recursion
Binary recursion calls itself twice. Binary recursion is used in solving some of the
important computing problems like:
1. Tower of Honai problem
2. Sorting by merge sort
3. Searching by binary search
4. Fibonacci series generation, etc.
Output
Enter the number of terms: 6
Fibonacci sequence for 10 terms is :
1
1
2
3
5
8
36. Is it possible to create Pointers to Functions
Yes. Function Pointers are pointers, i.e. variables, which point to an address of a
function. The running programs get a certain space in the main memory. Both, the
executable compiled program code and the used variables, are put inside this memory.
A function can take many types of arguments including the address of another
function. Function pointer can be a clever way to bind a function to a specific algorithm
at runtime.
37. What is Pointer?
A pointer is a variable that holds the address of a variable or a function. Every
pointer variable occupies same amount of memory space to store the variables belongs
int, float, char or any other type.
P X
Example
int *ptr; // Pointer to a integer variable
float *ptr; // Pointer to a float variable
char *ptr; // Pointer to a character variable
38. What is Void Pointer?
A void pointer is a special type of pointer, it can point any type ie, an integer, a
float or string of characters. Its limitation is that pointed data cannot be referenced
directly using *. Type casting should be done to display that pointed value.
a=10;
b=10.2;
p=&a;
printf("\nInteger a = %d",*(int *)p);
p=&b;
printf("\nFloat b = %f",*(float *)p);
getch();
}
Output
Integer a = 10
Float b = 10.200000
40. What is Null Pointer?
A null pointer is a special pointer value that points nowhere. To make pointer as
null pointer, initialize it with zero. That represents the null pointer is not intended to point
to an accessible memory location.
Example
#include<stdio.h>
void main()
{
int a;
float b;
void *p=0;
clrscr();
a=10;
b=10.2;
p=&a;
printf("\nInteger a = %d",*(int *)p);
p=&b;
printf("\nFloat b = %f",*(float *)p);
getch();
}
Address of p1 Address of x X
Part B
Declaration – Function1
Working Principle
Call Function1
.
. Definition - Function1
.
Call Function1 .
. .
. .
. .
Call Function1
.
.
The function “Function1” defined as a section of a program performing a specific
job.
Function1 is declared and called inside main program.
While Function1 call, program control moves to Definition of Function1 and
again returns to main function after execution of Function1.
Need of Functions
1. It makes programs easier to understand and maintain by breaking large
program into easily manageable blocks.
2. The main program consists of a series of function calls rather than countless
lines of code.
3. It is possible to execute as many times as necessary from different points in
the main program.
4. Well-written functions may be reused in multiple programs.
5. Different programmers working on one large project can divide the workload
by writing different functions.
6. It makes debugging and testing easier.
Advantage of functions
1. The length of source program can be reduced by reducing redundancy.
2. It enables reusing codes.
3. Better readability.
4. Provides way to hide information if necessary.
5. Improved debugging and testing.
6. Easy to understand and maintain.
Classification of Functions
Based upon who develops the function, functions are classified as:
1. Library Functions
2. User-defined Functions
Library Functions:
Library functions are the functions whose functionality has already been
developed by someone and are available to the user for use. There are two aspects of
working with library functions:
1) Declaration of Library functions.
2) Use(call) of Library functions.
Use of Library Functions
The role and usage of some of the common library functions are listed below.
Library of Mathematical Functions
It defines some common mathematical functions. The declarations of thes
functions are available in the header file math.h.
Exponential, logarithmic and power functions.
S. No Function Function Declaration Purpose
Example
//To display power and square root of a given number
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
double x;
printf("\nEnter value for x:");
scanf("\n%lf",&x);
printf("\n%lf Power 5 = %lf",x,pow(x,5));
printf("\nSquare Root of %lf = %lf",x,sqrt(x));
getch();
}
Output:
Enter value for x:4
4.000000 Power 5 = 1024.000000
Square Root of 4.000000 = 2.000000
It says that, the function printf must have a string argument at first, and the rest
will be of any type.
2. What are userdefined functions? Explain with example.
User-defined functions
The functions defined by the users according to their requirements are called user-
defined functions or programmer-defined functions.
The statements to perform a particular task, which is repeatedly used in the main
function, can be created as user-defined functions. These functions are useful to break
down a large program in to a number of smaller functions.
There are three major components associated with the user-defined function.
Example 2:
void add(int a, int b) // Function Header
{
int c;
c=a+b; // Executable statements
cout<<”Sum:”<<c;
}
Function Invocation/Call/Use
All function, within the standard library or user-written, are called from within this
main() function. While functions call, the program control passes to that of the function
definition. Once the function completes its task, the program control is passed back to the
calling environment(ie., main() function).
The general form of the function call statement is as follows:
<function_name><actual_arguments>
Output
Enter two integers:2 3
Sum = 5
Depending upon their inputs and outputs, userdefined-functions are classified as,
1) Functions with no input-output
2) Function with inputs and no output
3) Function with inputs and one output
4) Function with inputs and outputs
Functions with no input-output
It does not accept any input and does not return any result. It means that functions
with no arguments and valueless return type.
Value less return type is void.
Main()
{
Fn_name();
.
.
} Control transferred to function
Control returned back
to main
Fn_name()
{
.
.
.
}
Example
//To display sum of two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
void add(); //Function Declaration
add(); //Function Call
getch();
}
void add()
{
int x,y;
printf("Enter two integers:"); //Function Definition
scanf("%d%d",&x,&y);
printf("\nSum = %d",x+y);
}
Output
Enter two integers:5 7
Sum = 12
Functions with input and no output
It accepts input and does not return any result. It means that inputs passed as
arguments and return type is valueless.
Main()
{
void Fn_name(arg_list);
.
.
} Control transferred to
Control returned back function
to main
Fn_name(parameter_list)
{
.
.
.
}
Example
//To display sum of two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
void add(int,int); //Function Declaration
clrscr();
printf("Enter two integers:");
scanf("%d%d",&x,&y);
add(x,y); //Function Call
getch();
}
void add(int x,int y)
{
printf("\nSum = %d",x+y); //Function Definition
}
Output
Enter two integers: 3 4
Sum = 7
Functions with input and one output
It accepts input and returns output. It means that inputs passed as arguments and
return type is based on output to be returned. There are two forms of return statements:
o When a function does not return any value return or return 0; statement is used.
o To return any output from called function to calling function, return statement
with variable name or expression is used.
It cannot be placed inside the body of void function.
If results of evaluation and return type are not same, return
statement implicitly converts result type to the return type.
A function can return only one value. But, it is possible to specify
more than one return statement in the same function.
The return statement can return only one value from the called
function to the calling function.
Return statement can be present anywhere in the user-defined
function.
Parenthesis used around the expression in a return statement is
optional.
Main()
{
ret_type Fn_name(arg_list);
.
.
} Control transferred to
Control returned back function
to main
ret_type Fn_name(para_list)
{
.
.
return;
}
Example
//To display sum of two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,sum;
int add(int,int); //Function Declaration
clrscr();
printf("Enter two integers:");
scanf("%d%d",&x,&y);
sum=add(x,y); //Function Call
printf("\nSum = %d",sum);
getch();
}
int add(int x,int y)
{
return(x+y); //Function Definition
}
Output
Enter two integers: 3 4
Sum = 7
4. Illustrate the difference between parameters and arguments with example.
Formal vs Actual parameters
What is parameter?
A parameter is a special kind of variable, used in a function to refer pieces
of data provided as input to the function. These pieces of data are called
arguments.
In C programming Function passing parameter is optional.
We can call function without passing parameter.
Parameter vs arguments
The names given in the function definition are called parameters.
The values supplied in the function call are called arguments.
Before Swapping:
x=4
y=7
After Swapping:
x=7
y=4
Output
Enter 10 integers:
34
65
78
12
09
23
56
78
89
54
Minimum value in the array is: 9
Passing Two-Dimensional Arrays to Functions
Rules
1. It is mandatory to specify the column specifier of formal parameter and actual
argument.
2. The corresponding parameter type in the function declaration should be a
matching array type or pointer type.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],i,j,min;
int findmin(int[][2]);
clrscr();
printf("Enter 10 integers:");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
min=findmin(a);
printf("Minimum value in the 2-D array is: %d",min);
getch();
}
int findmin(int x[][2])
{
int temp,i,j;
temp=x[0][0];
for(i=1;i<2;i++)
{
for(j=0;j<2;j++)
if(temp>x[i][j])
temp=x[i][j];
}
return temp;
}
Output
Enter 10 integers:
45 78
12 54
Minimum value in the 2-D array is: 12
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float area(int r,float pi=3.14); // pi value 3.14 is default
argument
clrscr();
printf("Enter radius:");
scanf("%d",&r);
printf("Area of Circle : %2.2f",area(r));
getch();
}
float area(int r,float pi)
{
return(pi*r*r);
}
Output
Enter radius: 2
Area of Circle : 12.56
Example
Factorial (n) =
n * Factorial (n-1)
#include<stdio.h>
int main()
{
int n;
int factorial(int);
printf("Enter the number:");
scanf("%d",&n);
printf("Factorial of %d is %d",n,factorial(n));
return 0;
}
int factorial(int n)
{
if(n==0)
return 1;
else
return(n*factorial(n-1));
}
Output
Enter the number: 5
Factorial of 5 is 120
Execution
Warning!
1. Recursive function must have at least one terminating condition that can be
satisfied.
2. Otherwise, the recursive function will call itself repeatably until the run time stack
overflows.
How it works
3 * factorial (2)
2 * factorial (1)
1 * factorial (0)
#include<stdio.h>
#include<conio.h>
void main()
{
void move(int,int,int,int);
int disks=3;
clrscr();
printf("Follow these moves:\n");
move(disks,1,3,2);
}
void move(int count,int start,int finish, int temp)
{
if(count>0)
{
move(count-1,start,temp,finish);
printf("%d from %d to %d ",count,start,finish);
move(count-1,temp,finish,start);
}
}
P X
Syntax
Example
Pointer_variable_name=&Variable_Name;
Example
p=&x;
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
int *ptr;
printf("Enter value for x:");
scanf("%d",&x);
ptr=&x;
printf("\n\nx = %d",x);
printf("\n\n&x = %u",&x);
printf("\n\nptr = %u",ptr);
printf("\n\n*ptr = %d",*ptr);
getch();
}
Output
x = 23
&x = 65524
ptr = 65524
*ptr = 23
a=10;
b=10.2;
p=&a;
printf("\nInteger a = %d",*(int *)p);
p=&b;
printf("\nFloat b = %f",*(float *)p);
getch();
}
Output
Integer a = 10
Float b = 10.200000
13. Explain the use of pointers to swap two values with example.
Example
#include<stdio.h>
int main()
{
int x,y;
void swap(int*,int*);
printf("Enter two numbers:");
scanf("%d%d",&x,&y);
printf("\n\nBefore Swapping:\n\nx = %d\n\ny = %d",x,y);
swap(&x,&y);
printf("\n\nAfter Swapping:\n\nx = %d\n\ny = %d\n\n",x,y);
return 0;
}
void swap(int *a, int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
Before Swapping:
x=2
y=7
After Swapping:
x=7
y=2
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y;
Output:
15. Write a C program to find the sum of the digits using recursive
function.
#include <stdio.h>
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
OUTPUT:
$ cc pgm25.c
$ a.out
Enter the number: 2 3 4 5
Sum of digits in 2 3 4 5 is 14
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main()
{
int size, i, arr[MAX];
int *ptr;
clrscr();
ptr = &arr[0];
getch();
}
OUTPUT:
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
OUTPUT:
$ cc pgm66.c
$ a.out
Enter the value of N
6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780
#include<stdio.h>
int main()
{
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
return (0);
}
OUTPUT:
Enter the number of Rows of Mat1 :3
Enter the number of Columns of Mat1 : 3