C Interview Questions & Answers (Page -5)

User Define & Library Functions

51) What is the difference between User Define and Library Functions?

Library Functions:

The functions, which are predefined in any header file, we just include header file and call the functions.

For example, printf is a library function because it's declaration and definition are exists in the stdio.h header file.

User Define Functions (UDF):

The functions, which are defined by the programmer. Function’s declarations, callings and definitions are defined by the programmer, any functions which are created by the programmer is known as User Define Functions (UDF's).

52) When we should use/create UDF’s (User Define Functions)?

It's good programming technique to create User Define Functions, when source code is too large or complicated and has multiple tasks of different types. Then divide your code into functions (which are also known as modules) and call them wherever needed. This process also known as modularization of the code. Breaking code into small parts and the name of those small parts are known as function (also we can define as "Functions are set of user define instructions").

53) Can we use return statement in a function definition, if function's return type is void?

If function has any return type, then we return a value according to that type. We can also use return statement, if function does not has any return type. When compiler reaches at return program’s execution come out from the function and will not execute further statements.

Consider the example:

    void anyFun(int a)
    {
        if(a==0)
	        return;
        printf("Value is not Zero.");
    }

Here, we are checking the value of a, if it is 0, program's execution will return from the function to calling function. And if it is not 0, it will print "Value is not Zero".

54) Are exit () and return statements same in function definition?

No, both are different. exit() is used when you want to exit from program immediately while return is used to return the control of program's execution from called function to calling function.

55) What are called and calling functions?

Parent function which calls any function is known as calling function and the function which is being called is know as calling function.

Consider the examples:

    int main()
    {
	    abcFunc()
    }

Here, abcFunc() is called within main() function, so main() is called, while abcFunc() is calling function.

56) What is a static function?

By default, C functions are global; they can be called any file within the project. When you want to restrict a function to call in another file, just make it static. static keyword restrict function in the file where it is declared, it can not be access in another file.

Consider the example:

    static int myFun(void)
    {
        printf("Hello Guys!!! I’m a static function.");
        return 0;
    }

57) Where and how much space function occupies?

A function has two parts: function body (executable part/set of statements) and function data (function variables). Function body (executable part i.e. function's code) stored in Data Segment. But when a function is called by calling function, it takes space into Stack Segment according to declared variables in the function.

58) Explain call by value and call by reference?

Call by value:

When value is passed as function parameter, it is known as call by value. In this method value copies into actual arguments to formal arguments any change will be made in formal argument (arguments/ variables that are using in function declaration). No changes will not be made in actual arguments that are passing in function calling.

Call by reference:

When reference of variable (pointer) is passed as function parameter, it is known as call by reference. In this method variable’s reference (address) passes into format parameter which are pointer type. Any changes will be made in actual arguments.

Consider the following examples:

Call by value:

    #include <stdio.h>
    /* function : call by value*/
    void fun(int a,int b)
    {
        a+=10;
        b+=10;
    }

    int main()
    {
        int val1=10;
        int val2=10;
        printf("\nValue before function calling : %d, %d",val1,val2);
        fun(val1,val2);
        printf("\nValue after function calling : %d, %d",val1,val2);
        return 0;
    }

Output

    Value before function calling : 10, 10
    Value after function calling : 10, 10

Call by reference:

#include <stdio.h>
/* function : call by reference*/
void fun(int *a,int *b)
{
	*a+=10;
	*b+=10;
}


int main()
{
    int val1=10;
    int val2=10;
    printf("\nValue before function calling : %d, %d",val1,val2);
    fun(&val1,&val2);
    printf("\nValue after function calling : %d, %d",val1,val2);
    return 0;
}

Output

    Value before function calling : 10, 10
    Value after function calling : 20, 20

59) What are actual & formal parameters/ arguments?

The parameters/ arguments which are passed by calling function to the called function are known as actual parameters while the parameters/ arguments which are declared in the function declarations are known as formal parameters.

Consider the example:

    #include <stdio.h>
    /* function : call by value*/
    void fun(int a,int b)
    {
        a+=10;
        b+=10;
    }

    int main()
    {
        int val1=10;
        int val2=10;
        printf("\nValue before function calling : %d, %d",val1,val2);
        fun(val1,val2);
        printf("\nValue after function calling : %d, %d",val1,val2);
        return 0;
    }

Here, val1, val2 are actual parameters/arguments while a,b are formal parameters/arguments.

60) Can we define a function with in a function?

No, we can not define a function with in a function. But we can declare a function with in the function, which will accessible for that function only.

Consider the example:

    #include <stdio.h>

    void fun1(void)
    {
	    void fun2(void);	/*another function declaration*/
	    printf("\nI am fun1");
	    fun2();
    }
    int main()
    {

	    fun1();
	    return 0;
    }
    void fun2(void)
    {
	    printf("\nI am fun2");
    }

Output

    I am fun1
    I am fun2

Now, try to call fun2 in main()

    #include <stdio.h>

    void fun1(void)
    {
	    void fun2(void);	/*another function declaration*/
	    printf("\nI am fun1");
	    fun2();
    }
    int main()
    {

	    fun1();
        fun2();
	    return 0;
    }
    void fun2(void)
    {
	    printf("\nI am fun2");
    }

Output

    Error: ‘fun2’: Identifier not found.

Here, fun2 will not access in main() because it is declared in fun1.

61) What is function pointer, how will you declare, define and call?

When a pointer points to a function, it is known as function pointer or pointer to a function.

Consider the example – how to declare, define and call:

    #include <stdio.h>

    /*Create a function : declare with define*/
    int sumOfTwoNumbers(int a, int b) {
        int c;
	    c=a+b;
	    return c;
	    /*Or you can direct write : return (a+b)*/
    }

    /*************
	    declare function pointer, pointer to a function.
	    return type and arguments type must be same as function
    *************/
    int (*funcPointer)(int,int);

    int main()
    {

	    int val1=10;
	    int val2=10;
	    int sum =0;
	    /*function pointer initialization, point the address of function*/
	    funcPointer=&sumOfTwoNumbers;

	    sum=(*funcPointer)(val1,val2); /*function calling*/
	    printf("\nSum is = %d",sum);
	    return 0;
    }

Output

    Sum is = 20





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.