1. What is a global variable in C? Why do we need such a variable?
Ans:-A variable that is declared outside the function or block is called a global variable.
When we make a variable global, any function in the program can directly access that variable.

2. Write the syntax of a function declaration. The name of the function is calculateAge(). The function accepts the current year and birth year of a person. The function returns the age of the person.
Ans:
Syntax:-

int CalculateAge(int BirthYear,int CurrentYear);

3. Write the code segment for the function definition of the above function calculateAge().
Code:-

int Calculateage(int BirthYear,int CurrentYear){
int age;
age=CurrentYear-BirthYear;
return age;
}

4. What are different types of functions in C? Differentiate among them.

Ans:-There are two types of functions in C programming:
a. Library functions: These are the functions that are defined in the C header files. We use these functions while writing our programs. We have already used printf() and scanf() in our programs. There are more such as gets(), puts(), ceil(), floor() etc.
b. User-defined functions: These are the functions that are created by the programmer. The functions we discussed like claculateage() is user-defined functions. These are called user-defined functions because the programmer provides/writes definitions of these functions. But the definitions of library functions are already written in the system.

5. Differentiate between caller and callee functions. write a small C progarm and identify the caller and callee function in it.
Ans:- A caller is a function that calls another function. Whereas,a callee is a function that was called.
Coding:-

#include <stdio.h>
int main() // caller function
{int bage,cage, x;
printf("Enter birth year :-");
scanf("%d",&bage);
printf(" \n Enter current year :-");
scanf("%d",&cage);
x = Calculateage(bage,cage);
printf("Current age is %d",x);
return 0;
}
int Calculateage(int BirthYear,int CurrentYear)//callee function
{
int age;
age=CurrentYear-BirthYear;
return age;
}

In the above code main() is a function and it is calling a function Calculateage().Thus main() can be called as a caller function and Calculateage() can be called as a callee function.

6. When do we call a function user-defined? Is printf() a user-defined function? Justify.
Ans:-The functions which are created by the programmer are known as user-defined functions.The programmer provides/writes definitions of these functions within the program.
No,printf() is not a user-defined function because the definition of printf() is already written in the system.

7. Can we have two functions with the same name but with different numbers of parameters in a single C program? Write a simple C program to justify your answer.
Ans:- No, we cannot have two functions with the same name but with different numbers of parameters in a single C program.
Code-

#include <stdio.h>
int main()
{int bage=2001,cage=1992, x;
int a=1982,b=1876,d=2004,y;
x = Calculateage(bage,cage);
y=Calculateage(a,b,d);
printf("Current age is %d",x);
return 0;
}
int Calculateage(int BirthYear,int CurrentYear)
{
int age;
age=CurrentYear-BirthYear;
return age;
}
int Calculateage(int x,int y ,int z)
{
int age;
age=x+y+z;
return age;
}

Output:-
error: conflicting types for ‘Calculateage’.
note: previous definition of ‘Calculateage’ was here.

8. What are different components of a function? Show with a complete C program.
Ans:-There are three components of a function in C.
a. Function declaration: A function must be declared first before its use. This is to tell the compiler about the function name, function parameters, and return type.
b. Function call: This statement is responsible for the execution of a function. A function starts its execution when we make a call to it.
c. Function definition: This portion of a function contains the actual statements that are executed when the function is called.
Code:-

#include <stdio.h>
int Calculateage(int BirthYear,int CurrentYear);// Function //declaration
int main()
{int bage,cage, x;
printf("Enter birth year :-");
scanf("%d",&bage);
printf(" \n Enter current year :-");
scanf("%d",&cage);
x = Calculateage(bage,cage);//Function call
printf("Current age is %d",x);
return 0;
}
int Calculateage(int BirthYear,int CurrentYear)//Function definition
{
int age;
age=CurrentYear-BirthYear;
return age;
}

9. Define recursive function. Can we use a recursive function to solve all kinds of problems?
Ans:-If a function calls itself, we name it as recursion. The function is called a recursive function.
No,we cannot use a recursive function to solve all kinds of problems because it will make our program more complex and difficult. Recursion can be used in case of similar subtasks like sorting, searching, and traversal problems.

 

10. Consider the below code and list all the syntax errors.

#include<stdio.h>

int fun (int x)

{

if ( x%2==0)

return 1;

else

return 0;
}

int main()
{
int number;

printf (“\n Enter the number: “);
scanf(“%d”, &number );

int x = fun ( );

return 0;

}

Ans:- The parameter of fun() is missing i.e. int x = fun (number ); and the output line is also missing i.e. printf(“%d”,x);.

11. Consider the code segment below and find out the output if the user enters 5 f the keyboard when asked for.
#include<stdio.h>

int fun (int x)

{
if ( x%2==0)
return 1;

else
return 0;
}
int main()
{
int number;
printf (“\n Enter the number: “);
scanf(“%d”, &number );
int x = fun (number );
printf(“%d”,x);

return 0;

}
Ans:-

Output:- Enter the number: 5
0

12. Write a C program and define a function square() that accepts a number as the parameter and returns the square of that number as output.

Code:

#include<stdio.h>
float squareOfNumber(float num)
{
return (num*num);
}
int main()
{
float number, square;
printf("Please Enter any integer Value : ");
scanf("%f", &number);
square = squareOfNumber(number);
printf("square of a given number %.2f is = %.2f", number, square);
return 0;
}

13. Write a C program and define a function search () that searches an element in an array and returns the index of the element.
Code:-

#include<stdio.h>
int main()
{
int a[100], n, element, pos=0;
int i;
printf("Enter array size : ");
scanf("%d", &n);
printf("Enter array elements: ");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("Enter element to search: ");
scanf("%d",&element);
for(i=0; i<n; i++)
{
if(a[i]==element)
{
printf("%d found at index %d", element, i);
return 0;
}
}
printf("%d not found.", element);
return 0;
}

14. Write a C program and define a recursive function to find the summation of first N natural numbers.

Code:

#include <stdio.h>
int addNumbers(int n);
int main() 
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}
int addNumbers(int n) 
{
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}

15. Write a C program and define a function add() that accepts three integers. These integers indicate indices of an integer array. The function returns the summation of the elements stored in those indices.

7 8 8 0 0 9

For example, if we call the function add( 0, 2, 5), the function will return 24. The output is formed by 7+ 8+ 9 because elements at indices 0, 2 and 5 are 7, 8 and 9 respectively.

Code:-

#include<stdio.h>
int main()
{
int a,b,c;
printf("\nEnter 1st index:");
scanf("%d",&a);
printf("\nEnter 2nd index:");
scanf("%d",&b);
printf("\nEnter 3rd index:");
scanf("%d",&c);
int x = add(a,b,c);
return 0;
}
int add(int x,int y,int z)
{
int num[6]={7,8,8,0,0,9};
int sum=0;
sum=sum+num[x];
sum=sum+num[y];
sum=sum+num[z];
printf("\n The summation is: %d",sum);
return sum;
}

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here