A Function is a combined block of instructions written to perform a specific task. A function takes a value and after performing requisite processing, it returns the value in the program. Lets have a look over the different questions on Functions in C Language that can be asked in interview, exams and other places. The Questions with their answers are as follow-
Q1. What is the output of the following program?
swap(int x,int y)
Ans: 10 22 (duplicate copy of arguments are generated on calling a function)
Q2. What will be the output of the following code ?
Ans: Some address will be printed as function names are just addresses. Similarly output() is also a function and its address will be printed.
Q3. What will be the output of the following code ?
printf(“%d”,scanf(“%d”,&i)); // value 10 is given as input here
Ans: 1 as Scanf returns number of items read successfully. So number of items read is 1.
Q4. What is a static function?
Ans: A static function is a function whose scope is limited to the current source file. Scope refers to the visibility of a function or variable. For Example-
static int fun(void)
printf(“I am a static function “);
Q5. Is it possible to execute code even after the program exits the main() function?
Ans: C library consists of a function named atexit() that can be used to perform “cleanup” operations when your program terminates. You can set up a set of functions you want to perform automatically when your program exits by passing function pointers to the atexit() function.
Q6. Is using exit() the same as using return?
Ans: No. The exit() function is used to exit from your program and the return function is used to return from a function and return control to the calling function.
Q7. Explain the use of fflush() function?
Ans: It is used to empty the buffer associated with the o/p stream. For Example-
printf(“Enter a number\n”);
printf(“Enter a character\n”);
Q8. What is the difference b/w malloc() and calloc()?
Ans: malloc allocates uninitialized memory while calloc initializes the allocated memory with a constant (0).
malloc uses single parameter while calloc uses 2 parameters to initialize memory.
Q9. State the keyword which is used to transfer the controls back to a calling function from a function. Also write a sample program to justify your answer.
Ans: The keyword which is used to transfer controls back to a calling function from a function is “RETURN” and the sample program is as follows-
int add(int, int); /* Function prototype */
int a = 4, b = 3, c;
c = add(a, b);
printf(“c = %d\n”, c);
int add(int a, int b)
/* returns the value and control back to main() function */
The output of the program will be c=7
Q10. How many arguments can be used in a function?
Ans: As such c language doesn’t put any restriction on number of arguments but arguments greater than 8 is not preferred.
Download Questions with answers on Functions in C Language in Pdf QUESTIONS ON FUNCTIONS IN C LANGUAGE,pdf
Get more questions on Functions in C language, click the link
Functions in C Language