C Interview Questions And Answers

1.
What do you mean by the scope of a variable?
Scope indicates the region over which the variable's declaration has an effect.
2.
What are the different types of scopes that a variable can have?
There are four types of scopes- file, function, block and prototype.  
3.
What do you mean by definition of a variable?
In the definition of a variable, space is reserved for the variable and some initial value is given to it.
4.
When we mention the prototype of a function, are we defining the function or declaring it?
When we mention the prototype of a function, we are declaring it.
5.
What is C programming language?
C is a high level programming language which is developed by Dennis M. Ritchie.
6.
Give some examples of software that is built using C language?
UNIX/ Linux Operating system, Databases, Text editors, language compilers.
7.
Define C Compiler?
C compiler is used to convert human readable source code into machine language.
8.
What is main() function in c?
Program execution begins from the main() function. Every C program must have a main() function. When we run a program, the operating system first looks at the main() function.
9.
What are identifiers in C?
Identifiers are the name given to the variables, functions and constants. An identifier can start with A to Z, a to z, underscore (_) and followed by letters, numbers (0 to 9) or underscore(_).
10.
Define keywords in C with examples?
Keywords are reserved words that cannot be used as a variable name, constant name and constant name. Examples - case, break, for , goto, void, do etc.
11.
What is the sizeof operator?
sizeof operator is used to get the exact size of the variable.
12.
What is void data type in c?
void is an empty datatype which is used in place where no value is returned. Like-

1. used with a function
void welcome(char *name)
{
printf("Welcome, %s.", name);
}

2. Used as function parameter - A function having no parameter can use void as parameter.
int rand(void);
13.
Define enumerated data type in c?
An enumerated data type is an arithmetic data type that contains integral constants. Each constant is given a name and a value. Enum keyword is used to define enumerated data type.

enum location  {  east = 1, west = 2, north = 3, south = 4 };
14.
What are the storage size of float and double?
Size of single precision float data type is 4 bytes and double precision float data type is 8 bytes.
15.
Define constants in C?
Constants are fixed values that define only once and does not alter later. Constants can be any data types like an integer constants, floating constants etc.
16.
What is a register storage class?
The register storage class is used to store variable values in CPU register. The value stored in a CPU register can be accessed faster than the one that is stored in memory.
17.
How to include a file in a another file in c?
By using include statement, we can include one file into another.
Example - #include "filename"
18.
What are the different types of linkages?
There are three different types of linkages - external, internal and none.
External linkage means global, non-static variables and functions.
Internal linkage means static variables and functions with file scope.
No linkage means local variables.
19.
Define size_t?
size_t is used to express the size of something. It is the type of the result of the sizeof operator Or it is the type returned by strlen() to indicate the number of characters in a string.
20.
Can we use a switch statement to switch on strings?
No, the cases in a switch must either have integer constants or constant expressions.
21.
How floats are stored in binary form?
Floating point numbers are represented in IEEE format. The IEEE format for floating point storage uses a sign bit, a mantissa and an exponent for representing the power of 2. The sign bit denotes the sign of the number - a 0 represents a positive value and a 1 denotes a negative value. The mantissa is represented in binary after converting it to its normalised form.
22.
Is it necessary that the header files should have a .h extensions?
No. It is not necessary.They have been given a .h extensions only to identify them as something different than the .c program files.
23.
What do the header files usually contain?
Header files contain pre-processor directives like #define, structure, union and enum declarations, typedef declarations, global variable declarations and external function declarations.
24.
Will it result into an error if a header file is included twice?
Yes, unless the header file has taken care to ensure that if already included doesn't get included again.
25.
On doing #include where are the header files searched?
If #included using < > the files get searched in the predefined include path. It is possible to change the predefined include path. If #include with the "" syntax in addition to the predefined include path the files also get searched in the current directory.
26.
Are the expressions *ptr++ and ++*p same?
No, *ptr++ increments the pointer and not the value pointed by it, whereas ++*ptr increments the value being pointer to by ptr.
27.
For which purpose, we can use pointers?
We can use pointer in lot of places, like -
Accessing array or string elements
Dynamic memory allocation
Call by reference
Implementing linked lists, trees, graphs and many other data structures.
28.
What is null pointer?
For each pointer type, C defines a special pointer value. If a pointer is initialized with a NULL value, is considered as NULL Pointer.
29.
What is NULL macro?
A NULL macro is used to represent the null pointer in source code. It has a value 0 associated with it.
30.
What is ASCII NUL character?
The ASCII NUL character has all its bits as 0 but doesn't have any relationship with the null pointer.
31.
How many bytes are occupied by near, far and huge pointers?
A near pointer is 2 bytes long whereas a far and a huge pointer are 4 bytes long. Note the near, far and huge pointers exist only under DOS. Under Windows and Linux every pointer is 4 bytes long.
32.
Is the NULL pointer same as an uninitialised pointer?
No
33.
In which header file is the NULL macro defind?
In "stdio.h" and "stddef.h" files.
34.
How many array elements can be designate in expression num[45]?
46
35.
Which library function is used to reverse a string?
strrev() function is used to reverse a string.
36.
Which library function is used to find the last occurrence of a character in a string?
strchr() function is used to find the last occurrence of a character in a string.
37.
How are structure passing and returning by the compiler?
When structures are passed as arguments to functions, the entire structure is pushed on the stack. To return structures a hidden argument generated by the compiler is passed to the function. This argument points to a location where the returned structure is copied.
38.
What is the difference between a structure and a union?
In a union, complier allocates memory for the largest of all the members whereas in structure all the members get allocated memory.
39.
What is the difference between an enumeration and a set of preprocessor #defines?
The #define has a global effect, whereas an enumeration can have an effect local to the block, if desired.
40.
Which function is more safe to use, fgets() and gets()?
fgets(), because unlike fgets(), gets() cannot be told the size of the buffer into which the string supplied will be stored. As a result, there is always a possiblility of overflow of buffer.
41.
Which bitwise operator is suitable for turning off a particular bit in a number?
The & operator.
42.
How will you use calloc() functions?
calloc() needs two arguments, the number of elements to be allocated and the size of each element.

Example

p = (int*) calloc(10, sizeof(int));

The above example allocate space for a 10-integer array.


43.
Which function should be used to free the memory allocated by calloc()?
free() function should be used to free the memory allocated by calloc().
44.
What is Include directive?
The ‘include’ directive is used to make the inclusion of any header file available to the C program. It causes the preprocessor to add the contents of the corresponding header file to the program where you have written include statement.
45.
Define Keywords?
Keywords are the reserve words whose meaning has been already explained to the compiler. The keywords can not be used as a variable names. Example – float, case, return, void, while
46.
What is constants?
A constant is an entity that does not changed the value at run time.
47.
What is variables?
Variable is an identifier, which is used to store a data values for processing. A variable can have different data values at different times during program execution.
48.
What is local variable?
Local variable are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name in different functions.
49.
What is global variable?
Global variable are declared outside any function and they can be accessed on any function in the program.
50.
What is stdio.h?
The header files named ‘stdio.h’ has the collection of input/output functions. These functions are associated with keyword input/file input and screen output/file output.
51.
What is scanf() function?
scanf() is a library functions. It is used to receive values of variables from keyboard.
52.
What is conio.h?
conio.h header file contains functions for console input/output in C programming. Functioning of conio.h can be used to clear screen, change color of text and background.
53.
What is Goto Statement?
This is an unconditional control statement that transfers the flow of control to another part of the program.
54.
What is getch()?
This function gets a character from console but does not echo it to the screen. When getch() occurs in a program, it indicates the program waits for user input and when user presses any key, the execution of program continue.
55.
Define clrsrc()?
This is a library function to clear the output screen. It is pre-defined function in header file conio.h.
56.
Define switch statement?
This is a multidirectional conditional control statement. Sometimes there is a need in program to make choice among number of alternatives. For making choice, we use the switch statement.
57.
What is Call 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.
58.
What is Call by Reference?
In this method, the addresses of actual arguments in the calling function are copied into the formal arguments of the called function. It means, using these addresses, we would have an access to the actual arguments.
59.
What is recursion?
A function is called recursive if a statement within the body of the function calls the same function. Recursion is the process defining something in terms of itself.
60.
What is the difference between the function rewind() and fsetpos()?
rewind() function repositions file pointer to streams beginning. fsetpos() positions the file pointer of a stream at the desired position.




Read more articles