C Interview Questions and Answers for Freshers

    1 Votes

36) Can static variables be declared in a header file?

You can't declare a static variable without definition (this is because they are mutually exclusive storage classes). A static variable can be defined in a header file, but then every source file with in that scope will have their own copy of this variable, which is intended.

37) What is the benefit of using const for declaring constants over #define?

The basic difference between them is that, a const variable is a real variable which has a datatype and it exists at run time, and it can't be altered. But a macro is not a real variable, but it carries a constant value which replaces all the occurrences of that macro at the time of pre-processing.

38) What is a static function?

Static function is a special type of function whose scope is limited to the source file where the function is defined and can not be used other than that file. This feature helps you to hide some functions and to provide some standard interface or wrapper over that local function.

39) Should a function contain a return statement if it does not return a value?

In C, void functions does not return anything. So it is useless to put a return statement at the end of the function, where the control will any way return to the caller function. But, if you want to omit some portion of the function depending upon the scenario, then this return statement is perfect to avoid further execution of that void function.

40) How can you pass an array to a function by value?

An array can be passed to a function by value, by keeping a parameter with an array tag with empty square brackets(like []). From the caller function, just pass the array tag. For instance,

void func(int i[]) {..} /* parameter */
...
int k[10];
func(k); /* caller function */

41) Is it possible to execute code even after the program exits the main() function?

There is a standard C function named atexit() for this purpose that can be used to perform some operations when your program exiting. You can register some functions with atexit() to be executed at the time of termination. Here's an example:

#include <stdio.h>
#include <stdlib.h>
void _some_FUNC_(void);
int main(int argc, char** argv)
{
  ...
  atexit(_some_FUNC_);
  ….
}

42) What does a function declared as PASCAL do differently?

In C, when some function is called, the parameters are put at the top of the stack. Now the order in which they are put is the order in which the parameters are parsed. Normally, the order is right to left. That is, the right most is parsed first and the left most parameter is parsed at last.

If you want to alter this paradigm, then you have to define the function with PASCAL as following:

int PASCAL pascal_func(int, char*, long);

Here, the left most parameter(int) will be parsed first, then char* and then long.

43) Why does PASCAL matter? Is there any benefit to using PASCAL functions?

The main reason behind using PASCAL is that, in the left-to-right parsing the efficiency of switching increases in C.

44) Is using exit() the same as using return?

No. They are not the same. Return statement returns control to the caller function, that is, it exits from the lowest level of the call stack. Where as, exit statement make the program returns to the system from where the application was started. So, exit always exits from the highest level of call stack. Eventually, if there is only one level of function call then they both do the same.

45) Point out the error in the program given below, if any ?

main()
{
  int a=10,b;
  a>= 5 ? b=100 : b=200;
  printf("\n%d",b);
}

A value is required in function main(). The second assignment should be written in parenthesis as follows:

a>= 5 ? b=100 : (b=200);

46) Point out the error in the following program

main()
{
  const int x;
  x = 128;
  printf("%d",x);
}

Variable x should have been initialized during declaration.

Page 5 of 8

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

C++ Interview Questions and Answers for Freshers, Experienced
Advanced C Interview Questions and Answers for Experienced
Embedded C Interview Questions and Answers