In this post I have presented C Programming Interview Questions Answers Part-I. As C programming become very important and most successful programming language of our generation. I have listed questions and answers. To cover all important aspect of C Programming is nearly impossible in one post so I have divided our list of questions into couple of posts. This post will find helpful for preparing any job interview or academic preparation. The post design in such a way to understand nature of questions one may encounter during interview of subject related to C Programming.
2) Can a program be compiled without main() function?
Yes, it can be but cannot be executed, as the execution requires main() function definition.
4) What is a stack?
Stack is a specialized data storage structure (Abstract data type). Unlike, arrays access of elements in a stack is restricted. It has two main functions PUSH and POP. Insertion in a stack is done using PUSH function and removal from a stack is done using POP function. Stack allows access to only the last element inserted hence, an item can be inserted or removed from the stack from one end called the top of the stack. It is therefore, also called Last-In-First-Out (LIFO) list. Stack has three properties: capacity: stands for the maximum number of elements: stack can hold, size: stands for the current size of the stack and elements is the array of elements.
3) What is the difference between Call by Value and Call by Reference?
- 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 address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.
5) What are the different data types in C?
The basic data types are int, char, and float. Int is used to declare variables that will be storing integer values. Float is used to store real numbers. Char can store individual character values.
6) Can I use “int” data type to store the value 32768? Why?
No. “int” data type is capable of storing values from -32768 to 32767. To store 32768, you can use “long int” instead. You can also use “unsigned int”, assuming you don’t intend to store negative values.
7) What is the difference between actual and formal parameters?
The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called as formal parameters.
8) What are pointers?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. Some examples:
[crayon-5c451930555f0683007368/] 9) What is a pointer on pointer?
It’s a pointer variable which can hold the address of another pointer variable. It de-refers twice to point to the data held by the designated pointer variable.
[crayon-5c45193055601815000306/] Therefore ‘x’ can be accessed by **q.
10) What is the advantage of declaring void pointers?
When we do not know what type of the memory address the pointer variable is going to hold, then we declare a void pointer for such.
11) What is a NULL pointer?
A pointer pointing to nothing is called so. Example: char *p=NULL;
12) What is a dangling pointer?
A pointer initially holding valid address, but later the held address is released or freed. Then such a pointer is called as dangling pointer.
13) What are the valid places for the keyword break to appear.
Break can appear only in the looping control and switch statement. The purpose of the break is to bring the control out from the said blocks.
14) Which operator can be used to determine the size of a data type or variable?
15) Can we assign a float variable to a long integer variable?
Yes, with loss of fractional part.
16) What it the return value of a relational operator if it returns any?
Return a value 1 if the relation between the expressions is true, else 0.
17) Differentiate Source Codes from Object Codes
Source codes are codes that were written by the programmer. It is made up of the commands and other English-like keywords that are supposed to instruct the computer what to do. However, computers would not be able to understand source codes. Therefore, source codes are compiled using a compiler. The resulting outputs are object codes, which are in a format that can be understood by the computer processor. In C programming, source codes are saved with the file extension .C, while object codes are saved with the file extension .OBJ
18) Distinguish between malloc() & calloc() memory allocation.
Both allocates memory from heap area/dynamic memory. There are two major differences between malloc and calloc in C programming language: first, in the number of arguments. The malloc() takes a single argument, while calloc() takes two. Second, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.
19) What is keyword auto for?
By default every local variable of the function is automatic (auto). In the below function both the variables ‘i’ and ‘j’ are automatic variables.
[crayon-5c4519305560b465285953/] 20) What is difference between including the header file with-in angular braces < > and double quotes “ “
If a header file is included with in < > then the compiler searches for the particular header file only in the built in include path. If a header file is included with in “ “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built in include path.
21) How a negative integer is stored.
Get the two’s compliment of the same positive integer. Eg: 1101 (-5)
Step-1 − One’s compliment of 5 : 1010
Step-2 − Add 1 to above, giving 1011, which is -5
22) What is a static variable?
A static local variable retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.
[crayon-5c45193055611647947550/] 23) What is the purpose of extern storage specifier?
Used to resolve the scope of global symbol.
[crayon-5c45193055617784510964/] 24) What are storage classes in C?
- Automatic storage classes: Variable with block scope but without static specifier.
- Static storage classes: Variables with block scope and with static specifier. Global variables with or without static specifier.
- Allocated storage classes: Memory obtained from calls to malloc(), alloc() or realloc().
25) Explain the purpose of the function sprintf().
Prints the formatted output onto the character array.
26) What is the meaning of base address of the array?
The starting address of the array is called as the base address of the array.
27) When should we use the register storage specifier?
If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the look up of the variable.
28) S++ or S = S+1, which can be recommended to increment the value by 1 and why?
S++, as it is single machine instruction (INC) internally.
29) What are the different file extensions involved when programming in C?
Source codes in C are saved with .C file extension. Header files or library files have the .H file extension. Every time a program source code is successfully compiled, it creates an .OBJ object file, and an executable .EXE file.
30) What is the purpose of the keyword typedef?
It is used to alias the existing type. Also used to simplify the complex declaration of the type.
31) What is lvalue and rvalue?
The expression appearing on right side of the assignment operator is called as rvalue. Rvalue is assigned to lvalue, which appears on left side of the assignment operator. The lvalue should designate to a variable not a constant.
32) Where an automatic variable is stored?
Every local variable by default being an auto variable is stored in stack memory.
33) What is the difference between variable declaration and variable definition?
Declaration associates type to the variable whereas definition gives the value to the variable.
34) What is a sequential access file?
When writing programs that will store and retrieve data in a file, it is possible to designate that file into different forms. A sequential access file is such that data are saved in sequential order: one data is placed into the file after another. To access a particular data within the sequential access file, data has to be read one data at a time, until the right one is reached.
35) What is spaghetti programming?
Spaghetti programming refers to codes that tend to get tangled and overlapped throughout the program. This unstructured approach to coding is usually attributed to lack of experience on the part of the programmer. Spaghetti programming makes a program complex and analyzing the codes difficult, and so must be avoided as much as possible.
36) What is a self-referential structure?
A structure containing the same structure pointer variable as its element is called as self-referential structure.
37) Does a built-in header file contain built-in function definition?
No, the header file only declares function. The definition is in library which is linked by the linker.
38) What is a token?
A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.
39) What is a preprocessor?
Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.
40) Explain the use of %i format specifier with respect to scanf().
Can be used to input integer in all the supported format.
41) Does a break is required by default case in switch statement?
Yes, if it is not appearing as the last case and if we do not want the control to flow to the following case after default if any.
42) When to use –> (arrow) operator?
If the structure/union variable is a pointer variable, to access structure/union elements the arrow operator is used.
43) What are bit fields?
We can create integer structure members of differing size apart from non-standard size using bit fields. Such structure size is automatically adjusted with the multiple of integer size of the machine.
45) What is the purpose of built-in stricmp() function
This function stricmp() compares two strings by ignoring the case.
46) Describe the file opening mode “w+”.
Opens a file for reading and writing. If a file doesn’t exist it creates one, else if the file exists it will be over written.
47) Where the address of operator (&) cannot be used?
It cannot be used on constants. Also it cannot be used on variable which are declared using register storage class.
48) Is FILE a built-in data type?
No, it is a structure defined in stdio.h.
49) What is reminder for 5.0 % 2?
Error, It is invalid that either of the operands for the modulus operator (%) is a real number.
50) How many operators are there under the category of ternary operators?
There is only one operator and is conditional operator (? : ).
51) Which key word is used to perform unconditional branching?
52) What is a pointer to a function? Give the general syntax for the same.
A pointer holding the reference of the function is called pointer to a function. In general it is declared as follows.
[crayon-5c45193055624855449244/] Once fun_ptr refers a function the same can be invoked using the pointer as follows:
[crayon-5c45193055629702831702/] 53) Explain the use of comma operator (,).
Comma operator can be used to separate two or more expressions.
[crayon-5c4519305562e290880215/] 54) What is a NULL statement?
A NULL statement is no executable statements such as ; (semicolon).
[crayon-5c45193055634172264591/] Above does nothing 10 times.
55) What is a static function?
A function’s definition prefixed with static keyword is called as a static function. You would make a function static if it should be called only within the same source code.
56) Which compiler switch to be used for compiling the programs using math library with gcc compiler?
Opiton –lm to be used as > gcc –lm <file.c>
57) Which operator is used to continue the definition of macro in the next line?
Backward slash (\) is used to define multiline macro.
[crayon-5c45193055639572906079/] 58) Which operator is used to receive the variable number of arguments for a function?
Ellipses (…) is used for the same. A general function definition looks as follows
[crayon-5c4519305563f719207396/] 59) What is the problem with the following coding snippet?
[crayon-5c45193055644065577663/] s1 points to a string constant and cannot be altered.
60) Which built-in library function can be used to re-size the allocated dynamic memory?
[crayon-5c45193055649779649331/] 61) What are enumerations?
Enumerations are list of integer constants with name. Enumerators are defined with the keyword enum.
62) Which built-in function can be used to move the file pointer internally?
[crayon-5c4519305564e831277166/] 63) Can variables belonging to different scope have same name? If so show an example.
Variables belonging to different scope can have same name as in the following code snippet.