Questions on Functions in C Language

Jan 10 • Resources • 9305 Views • 21 Comments on Questions on Functions in C Language

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-

C interview question and answer

C Language role

Q1. What is the output of the following program?
main()
{
int x=20;
int y=10;
swap(x,y);
printf(“%d %d”,y,x+2);
}
swap(int x,int y)
{
int temp;
temp =x;
x=y;
y=temp;
}

Ans: 10 22 (duplicate copy of arguments are generated on calling a function)

Q2. What will be the output of the following code ?
output()
{
printf(“%p”,output);
}

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 ?
main()
{
int i;
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”);
scanf(“%d”,&num);
fflush(stdin);
printf(“Enter a character\n”);
scanf(“%c”,&ch);

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-

#include<stdio.h>
int add(int, int); /* Function prototype */
int main()
{
int a = 4, b = 3, c;
c = add(a, b);
printf(“c = %d\n”, c);
return 0;
}
int add(int a, int b)
{
/* returns the value and control back to main() function */
return (a+b);
}

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

Related Links-
MACROS in C Questions and Answers
C Aptitude Questions and Answers with pdf
Important C interview questions with answers

Tell us Your Queries, Suggestions and Feedback

Your email address will not be published.

21 Responses to Questions on Functions in C Language

  1. funk says:

    what are the frequently asked questions on function in c programming?

  2. koti reddy says:

    important short answer questions in functions of c language

  3. saurabh jajoo says:

    now a days many companies used to mix this type of questions in the aptitude test. For being cleared in these tests this post can help you very much.

  4. sakshi chaudhary says:

    this article consist of few basic but important questions on functions that are frequently asked in the interviews…with the help of this post use and application of functions can easily be understood….

  5. Ankita Prajapati says:

    This article proves to be very helpful as it gives overview of important or frequently asked questions in interview…

  6. saurabh singh says:

    This Post on functions is really nice as functions are somewhat complicated to understand and with the help of this post we can easily grasp the type of questions asked in interview.

  7. BHARTI PANDEY says:

    This ARTICLE is dealt on FUNCTIONS by the Author….It was a great help for me to have an OVERVIEW of FUNCTIONS….before my INTERVIEW.Really m debted to this Post for my Selection:))

« »