This post is a set of five of frequently asked interview questions at entry level. These questions have been attempted below. Care has been taken to make answer simple and clear. Question.12, 13 test whether the candidate appearing for interview in C Language is aware of pass by value and pass by reference concepts. The solution for these questions also carries an example for each of the concept. Question.14 is about Enumerations. Enumerations are quite useful and can make the code clear. Question.15 asks the use of typedef in C Language. This question can give a fair idea to the interviewer about the interview candidate. Freshers may not even have heard about typedef. Question.16 is about register variables. Usage of register variables is limited and they may be pretty useful in Embedded System or Micro-controller programming in C language. See all C interview questions in this blog in one page.
- What is Pass by Value? Write a C program showing this concept. Pass by Value: In this method, the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In pass by value, the changes made to formal arguments in the called function have no effect on the values of actual arguments in the calling function.
- What is Pass by Reference? Write a C program showing this concept.
- What is an Enumeration?
- What is the use of typedef?
- What are register variables? What are advantages of using register variables?
Example:
#include <stdio.h>
void swap(int x, int y) {
int t;
t = x;
x = y;
y = t;
}
int main() {
int m = 10, n = 20;
printf("Before executing swap m=%d n=%d\n", m, n);
swap(m, n);
printf("After executing swap m=%d n=%d\n", m, n);
return 0;
}
Output:
Before executing swap m=10 n=20
After executing swap m=10 n=20
Explanation:
In the main function, value of variables m, n are not changed though they are passed to function ‘swap’. Swap function has a copy of m, n and hence it can not manipulate the actual value of arguments passed to it.
Back to top
Pass by Reference: In this method, the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses, we would have an access to the actual arguments and hence we would be able to manipulate them. C does not support Call by reference. But it can be simulated using pointers.
Example:
#include <stdio.h>
/* function definition */
void swap(int *x, int *y) {
int t;
t = *x; /* assign the value at address x to t */
*x = *y; /* put the value at y into x */
*y = t; /* put the value at to y */
}
int main() {
int m = 10, n = 20;
printf("Before executing swap m=%d n=%d\n", m, n);
swap(&m, &n);
printf("After executing swap m=%d n=%d\n", m, n);
return 0;
}
Output:
Before executing swap m=10 n=20
After executing swap m=20 n=10
Explanation:
In the main function, address of variables m, n are sent as arguments to the function ‘swap’. As swap function has the access to address of the arguments, manipulation of passed arguments inside swap function would be directly reflected in the values of m, n.
Back to top
Enumeration is a data type. We can create our own data type and define values that the variable can take. This can help in making program more readable. enum definition is similar to that of a structure.
Example: consider light_status as a data type. It can have two possible values – on or off.
enum light_status
{
on, off
};
enum light_status bulb1, bulb2;
/* bulb1, bulb2 are the variables */
Declaration of enum has two parts:
a) First part declares the data type and specifies the possible values, called ‘enumerators’.
b) Second part declares the variables of this data type.
We can give values to these variables:
bulb1 = on;
bulb2 = off;
Back to top
typedef declaration helps to make source code of a C program more readable. Its purpose is to redefine the name of an existing variable type. It provides a short and meaningful way to call a data type. typedef is useful when the name of the data type is long. Use of typedef can reduce length and complexity of data types.
Note: Usually uppercase letters are used to make it clear that we are dealing with our own data type.
Example:
struct employee {
char name[20];
int age;
};
struct employee e;
The above declaration of the structure would be easy to use when renamed using typedef as:
struct employee {
char name[20];
int age;
};
typedef struct employee EMP;
EMP e1, e2;
Register variables are stored in the CPU registers. Its default value is a garbage value. Scope of a register variable is local to the block in which it is defined. Lifetime is till control remains within the block in which the register variable is defined. Variable stored in a CPU register can always be accessed faster than the one that is stored in memory. Therefore, if a variable is used at many places in a program, it is better to declare its storage class as register
Example:
register int x=5;
Variables for loop counters can be declared as register. Note that register keyword may be ignored by some compilers.
Back to top
| Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Next |

