C Programming Interview Questions and Answers
by Mohammed, on Apr 2, 2018 12:55:58 PM
Q1. What are the key features in C programming language?
Ans:
- Portability: Platform independent language.
- Modularity: Possibility to break down large programs into small modules.
- Flexibility: The possibility to a programmer to control the language.
- Speed: C comes with support for system programming and hence it is compiling and executes with high speed when comparing with other high-level languages.
- Extensibility: Possibility to add new features by the programmer.
Q2. What are the basic data types associated with C?
Ans:
- Int: Represent number (integer)
- Float: Number with a fraction part.
- Double: Double-precision floating point value
- Char: Single character
- Void: Special purpose type without any value.
Q3. What are header files and what are its uses in C programming?
Ans: Header files are also known as library files. They contain two essential things: the definitions and prototypes of functions being used in a program. Simply put, commands that you use in C programming are actually functions that are defined from within each header files. Each header file contains a set of functions.
For example: "stdio.h" is a header file that contains definition and prototypes of commands like printf and scanf.
Q4. What are reserved words?
Ans: Reserved words are words that are part of the standard C language library. This means that reserved words have special meaning and therefore cannot be used for purposes other than what it is originally intended for. Examples of reserved words are int, void, and return.
Q5. What are the general description for loop statement and available loop types in C?
Ans: A statement that allows executing statement or group of statements in repeated way is defined as a loop. Following diagram explains
Q6. Following diagram explains a general form of a loop.
There are 4 types of a loop statement in C.
- While loop
- For Loop
- Do…While Loop
- Nested Loop
Q7. Is that possible to store 32768 in an int data type variable?
Ans: Int data type only capable of storing values between – 32768 to 32767.To store 32768 a modifier needs to use with int data type. Long Int can use and also if there is no any negative values unsigned int is also possible to use.
Q8. What are different storage class specifiers in C?
Ans: auto, register, static, extern.
Q9. What is scope of a variable? How are variables scoped in C?
Ans: Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically (or statically) scoped.
Q10. When is the void keyword used in a function?
Ans: When declaring functions, you will decide whether that function would be returning a value or not. If that function will not return a value, such as when the purpose of a function is to display some outputs on the screen, then void is to be placed at the leftmost part of the function header. When a return value is expected after the function execution, the data type of the return value is placed instead of void.
Q11. What are linked list?
Ans: A linked list is composed of nodes that are connected with another. In C programming, linked lists are created using pointers. Using linked lists is one efficient way of utilizing memory for storage.
Q12. What is NULL pointer?
Ans: NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should make a pointer NULL when memory pointed by it is deallocated in the middle of a program.
Q13. What are binary trees?
Ans: Binary trees are actually an extension of the concept of linked lists. A binary tree has two pointers, a left one and a right one. Each side can further branch to form additional nodes, which each node having two pointers as well.
Q14. How a negative integer is stored?
Ans: Get the two’s compliment of the same positive integer. Eg: 1011 (-5)
Step-1: One’s compliment of 5 : 1010
Step-2: Add 1 to above, giving 1011, which is -5
Q15. Can a program be compiled without main() function?
Ans: Yes, it can be but cannot be executed, as the execution requires main() function definition.
Q16. What is a nested structure?
Ans: A structure containing an element of another structure as its member is referred so.
Q17. What is a token?
Ans: A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.
Q18. What is keyword auto for?
Ans: By default every local variable of the function is automatic (auto). In the below function both the variables ‘i’ and ‘j’ are automatic variables.
void f() {
int i;
auto int j;
}
NOTE − A global variable can’t be an automatic variable.
Q19. 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 address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.
Q20. What is typecasting?
Ans: Typecasting is a way to convert a variable/constant from one type to another type.
Q21. How can we determine whether a file is successfully opened or not using fopen() function?
Ans: On failure fopen() returns NULL, otherwise opened successfully.
Q22. S++ or S = S+1, which can be recommended to increment the value by 1 and why?
Ans: S++, as it is single machine instruction (INC) internally.
Q23. Distinguish between malloc() & calloc() memory allocation.
Ans: Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated memory with 0’s.
Q24. What is lvalue and rvalue?
Ans: 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.
Q25. Explain modular programming.
Ans: Dividing the program in to sub programs (modules/function) to achieve the given task is modular approach. More generic functions definition gives the ability to re-use the functions, such as built-in library functions.
Q26. Where the address of operator (&) cannot be used?
Ans: It cannot be used on constants. It cannot be used on variable which are declared using register storage class.
Q27. What is the general form of function in C?
Ans: Function definition in C contains four main sections:
- Return Type: Data type of the return value of the function.
- Function Name: The name of the function and it is important to have a meaningful name that describes the activity of the function.
- Parameters: The input values for the function that need to use perform the required action.
- Function Body: Collection of statement that needs to perform the required action.
Q28. What is the difference between functions abs() and fabs()?
Ans: These 2 functions basically perform the same action, which is to get the absolute value of the given value. Abs() is used for integer values, while fabs() is used for floating type numbers. Also, the prototype for abs() is under , while fabs() is under .
Q29. What is the difference between text files and binary files?
Ans: Text files contain data that can easily be understood by humans. It includes letters, numbers and other characters. On the other hand, binary files contain 1's and 0's that only computers can interpret.
Q30. What is dynamic data structure?
Ans: Dynamic data structure provides a means for storing data more efficiently into memory. Using dynamic memory allocation, your program will access memory spaces as needed. This is in contrast to static data structure, wherein the programmer has to indicate a fix number of memory space to be used in the program.
Q31. What is the use of a \0' character?
Ans: It is referred to as a terminating null character, and is used primarily to show the end of a string value.
Q32. What is the difference between the = symbol and == symbol?
Ans: The = symbol is often used in mathematical operations. It is used to assign a value to a given variable. On the other hand, the == symbol, also known as equal to or equivalent to, is a relational operator that is used to compare two values.
Q33. What are local static variables? What is their use?
Ans: A local static variable is a variable whose lifetime doesn’t end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables. Static variables can be used to count the number of times a function is called. Also, static variables get the default value as 0. For example, the following program prints “0 1”
#include <stdio.h> void fun() { // static variables get the default value as 0. static int x; printf ( "%d " , x); x = x + 1; } int main() { fun(); fun(); return 0; } // Output: 0 1 |
Q34. What are static functions? What is their use?
Ans: In C, functions are global by default. The “static” keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
Q35. What is the difference between declaration and definition of a variable/function?
Ans: Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function. So that’s all about declaration. Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. Therefore, we can think of definition as a super set of declaration. (or declaration as a subset of definition). From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function).
// This is only declaration. y is not allocated memory by this statement
extern int y;
// This is both declaration and definition, memory to x is allocated by this statement.
int x;
Q36. When should we use pointers variable in a C program?
Ans:
- To get address of a pointer variable
- For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables.
- To pass large structures so that complete copy of the structure can be avoided.
- To implement “linked” data structures like linked lists and binary trees.
Q37. What is Dangling pointer?
Ans: Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. Following are examples.
// EXAMPLE 1 int *ptr = ( int *) malloc ( sizeof ( int )); ............. ............. free (ptr); // ptr is a dangling pointer now and operations like following are invalid *ptr = 10; // or printf("%d", *ptr); |
// EXAMPLE 2 int *ptr = NULL { int x = 10; ptr = &x; } // x goes out of scope and memory allocated to x is free now. // So ptr is a dangling pointer now |
Q38. What are the ways to a null pointer can use in C programming language?
Ans: Null pointers are possible to use in three ways.
- As an error value.
- As a sentinel value.
- To terminate indirection in the recursive data structure.
Q39. How will you print “Hello World” without semicolon?
Ans:
int main( void ) { if ( printf ( "Hello World" )) ; } |
Q40. Write a loop statement that will show the following output:
1
12
123
1234
12345
Ans:
for (a=1; a<=5; i++) {
for (b=1; b<=a; b++)
printf("%d",b);
printf("\n");
}
Q41. What is a stack?
Ans: A stack is one form of a data structure. Data is stored in stacks using the FILO (First In Last Out) approach. At any particular instance, only the top of the stack is accessible, which means that in order to retrieve data that is stored inside the stack, those on the upper part should be extracted first. Storing data in a stack is also referred to as a PUSH, while data retrieval is referred to as a POP.
Q42. What is a sequential access file?
Ans: 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.
Q43. What are comments and how do you insert it in a C program?
Ans: Comments are a great way to put some remarks or description in a program. It can serves as a reminder on what the program is all about, or a description on why a certain code or function was placed there in the first place. Comments begin with /* and ended by */ characters. Comments can be a single line, or can even span several lines. It can be placed anywhere in the program.
Q44. What is debugging?
Ans: Debugging is the process of identifying errors within a program. During program compilation, errors that are found will stop the program from executing completely. At this state, the programmer would look into the possible portions where the error occurred. Debugging ensures the removal of errors, and plays an important role in ensuring that the expected program output is met.
Q45. What does the && operator do in a program code?
Ans: The && is also referred to as AND operator. When using this operator, all conditions specified must be TRUE before the next action can be performed. If you have 10 conditions and all but 1 fails to evaluate as TRUE, the entire condition statement is already evaluated as FALSE.
Related Interview Questions...
- Android Interview Questions and Answers
- Android Interview Questions and Answers for Freshers
- IOS Interview Questions and Answers
- Ionic Interview Questions and Answers
- Mobile Testing Interview Questions and Answers
- Swift Programming Interview Questions
- Advanced C++ Programming Interview Questions and Answers
- C Interview Question and Answers
- React Native and Redux Interview Questions and Answers
- C and Data Structure Interview Questions And Answers