Top

C Programming

61.

How is a Function declared in C Language?

Ans:

A function in C language is declared as follows,

return_type function_name(formal parameter list)
{
       Function_Body;
}

 

62.

What is a pointer to a function? Give the general syntax for the same.

Ans:

A pointer holding the reference of the function is called a pointer to a function. In general, it is declared as follows.

T (*fun_ptr) (T1,T2…); Where T is any date type.

Once fun_ptr refers to a function the same can be invoked using the pointer as follows.

fun_ptr();
[Or]
(*fun_ptr)();

 

63.

What is the default function call method?

Ans:

By default, the functions are called by value.

64.

Can one function call another?

Ans:

Yes, any user-defined function can call any function.

65.

What is a static function?

Ans:

A function’s definition prefixed with a static keyword is called a static function. You would make a function static if it should be called only within the same source code.

66.

What is recursion in C?

Ans:

When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.

Recursive function comes in two phases:

  • Winding phase
  • Unwinding phase

Winding phase: When the recursive function calls itself, and this phase ends when the condition is reached.

Unwinding phase: The unwinding phase starts when the condition is reached, and the control returns to the original call.

Example of recursion

#include   
int calculate_fact(int);  
int main()  
{  
 int n=5,f;  
 f=calculate_fact(n); // calling a function  
 printf("factorial of a number is %d",f);  
  return 0;  
}  
int calculate_fact(int a)  
{  
  if(a==1)  
  {  
      return 1;  
  }  
  else  
  return a*calculate_fact(a-1); //calling a function recursively.  
}  

Output: factorial of a number is 120

67.

What are the different ways of passing parameters to the functions? Which to use when?

Ans:

Call by value: We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used.

Call by reference: We send the address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.

68.

What is the difference between actual and formal parameters?

Ans:

The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called formal parameters.

69.

Differentiate between call by value and call by reference.

Ans:

Factor

Call by Value

Call by Reference
Safety Actual arguments cannot be changed and remain safe Operations are performed on actual arguments, hence not safe

Memory Location

Separate memory locations are created for actual and formal arguments Actual and Formal arguments share the same memory space.
Arguments Copy of actual arguments are sent Actual arguments are passed

Example of Call by Value method

#include  
void change(int,int);  
int main()  
{  
    int a=25,b=50;  
    change(a,b); 
    printf("The value assigned to a is: %d",a);  
    printf("n");  
    printf("The value assigned to of b is: %d",b);  
    return 0;  
}  
void change(int x,int y)  
{  
    x=100;  
    y=200;  
}

Example of Call by Reference method

#include
void change(int*,int*);  
int main()  
{  
    int a=25,b=50;  
    change(&a,&b); 
    printf("The value assigned to a is: %d",a);  
    printf("n");  
    printf("The value assigned to b is: %d",b);  
    return 0;  
}  
void change(int *x,int *y)  
{  
    *x=100;  
    *y=200;  
}

 

70.

Differentiate between getch() and getche().

Ans:

Both the functions are designed to read characters from the keyboard and the only difference is that

getch(): reads characters from the keyboard but it does not use any buffers. Hence, data is not displayed on the screen.

getche(): reads characters from the keyboard and it uses a buffer. Hence, data is displayed on the screen.

Example:

#include
#include
int main()
{
     char ch;
     printf("Please enter a character ");
     ch=getch();
     printf("nYour entered character is %c",ch);
     printf("nPlease enter another character ");
     ch=getche();
     printf("nYour new character is %c",ch);
     return 0;
}

Output:

Please enter a character
Your entered character is x
Please enter another character z
Your new character is z

Loading…
Tags: C Programming Interview Questions and Answers || C Programming Sort Questions and Answers || C Programming Detailed Questions and Answers || C Programming Tutorial