Top 70 C Programming Interview Questions & Answers

Most asking C Programming Interview Questions

Here we go.

Q 1) What are the key features in the C programming language?

Answer: Features are as follows:

Portability: It is a platform-independent language.

Modularity: Possibility to break down large programs into small modules.

Flexibility: The possibility of a programmer to control the language.

Speed:  C comes with support for system programming and hence it compiles and executes with high speed when compared with other high-level languages.

Extensibility: Possibility to add new features by the programmer.

Q 2) What are the basic data types associated with C?

Answer:

        Int – Represent the number (integer)

        Float – Number with a fraction part.

        Double – Double-precision floating-point value

        Char – Single character

        Void – Special purpose type without any value.

Q 3) What is the description for syntax errors?

Answer: The mistakes/errors that occur while creating a program are called syntax errors. Misspelled commands or incorrect case commands, an incorrect number of parameters in calling method /function, data type mismatches can be identified as common examples for syntax errors.

Q 4) What is the process to create increment and decrement statement in C?

Answer: There are two possible methods to perform this task.

                Use increment (++) and decrement (-) operator.

                Example When x=4, x++ returns 5 and x- returns 3.

                Use conventional + or – sign.

                Example When x=4, use x+1 to get 5 and x-1 to get 3.

Q 5) What are reserved words with a programming language?

Answer: The words that are a part of the standard C language library are called reserved words. Those reserved words have special meaning and it is not possible to use them for any activity other than its intended functionality.

        Example: void, return int.

Q 6) What is the explanation for the dangling pointer in C?

Answer: When there is a pointer pointing to a memory address of any variable, but after some time the variable was deleted from the memory location while keeping the pointer pointing to that location is known as a dangling pointer in C.

Q 7) Describe static function with its usage?

Answer: A function, which has a function definition prefixed with a static keyword is defined as a static function. The static function should be called within the same source code.

Q 8) What is the difference between abs() and fabs() functions?

Answer: Both functions are to retrieve absolute value. abs( ) is for integer values and fabs( ) is for floating type numbers. Prototype for abs( ) is under the library file < stdlib.h > and fabs( ) is under < math.h >.

Q 9) Describe Wild Pointers in C?

Answer: Uninitialized pointers in the C code are known as Wild Pointers. They point to some arbitrary memory location and can cause bad program behavior or program crash.

Q 10) What is the difference between ++a and a++?

Answer: ‘++a”  is called prefixed increment and the increment will happen first on a variable. ‘a++’ is called postfix increment and the increment happens after the value of a variable used for the operations.

Q 11) Describe the difference between = and == symbols in C programming?

Answer: ‘==’ is the comparison operator which is used to compare the value or expression on the left-hand side with the value or expression on the right-hand side.

‘=’ is the assignment operator which is used to assign the value of the right-hand side to the variable on the left-hand side.

Q 12) What is the explanation for prototype function in C?

Answer: Prototype function is a declaration of a function with the following information to the compiler.

    Name of the function.

    The return type of the function.

    Parameters list of the function.

    prototype function in C

In this example Name of the function is Sum, the return type is the integer data type and it accepts two integer parameters.

Q 13) What is the explanation for the cyclic nature of data types in C?

Answer: Some of the data types in C have special characteristic nature when a developer assigns value beyond the range of the data type. There will be no compiler error and the value changes according to a cyclic order. This is called cyclic nature. Char, int, long int data types have this property. Further float, double and long double data types do not have this property.

Q 14) Describe the header file and its usage in C programming?

Answer: The file containing the definitions and prototypes of the functions being used in the program are called a header file. It is also known as a library file.

Example: The header file contains commands like printf and scanf is from the stdio.h library file.

Q 15) There is a practice in coding to keep some code blocks in comment symbols than delete it when debugging. How this affects when debugging?

Answer: This concept is called commenting out and this is the way to isolate some part of the code which scans possible reason for the error. Also, this concept helps to save time because if the code is not the reason for the issue it can simply be removed from comment.


Q 16) What are the general description for loop statements and available loop types in C?

Answer: A statement that allows the execution of statements or groups of statements in a repeated way is defined as a loop.

The following diagram explains a general form of a loop.

There are 4 types of loop statements in C

        While loop

        For Loop

        Do…While Loop

        Nested Loop

Q 17) What is a nested loop?

Answer: A loop that runs within another loop is referred to as a nested loop. The first loop is called the Outer Loop and the inside loop is called the Inner Loop. The inner loop executes the number of times defined in an outer loop.

Q 18) What is the general form of function in C?

Answer: The function definition in C contains four main sections.

    return_type function_name( parameter list )

    {  

          body of the function

    }

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 are used to perform the required action.

Function Body: Collection of statements that performs the required action.

Q 19) What is a pointer on a pointer in C programming language?

Answer: A pointer variable that contains the address of another pointer variable is called pointer on a pointer. This concept de-refers twice to point to the data held by a pointer variable.

pointer on pointer

        int a=5, *x=&a,**y=&x;

In this example **y returns the value of the variable a.

Q 20) What are the valid places to have keyword “Break”?

Answer: The purpose of the Break keyword is to bring the control out of the code block which is executing. It can appear only in looping or switch statements.

Q 21) What is the behavioral difference when the header file is included in double-quotes (" ") and angular braces (<>)?

Answer: When the Header file is included within double quotes (“ ”), compiler search first in the working directory for the particular header file. If not found, then it searches the file in the include path. But when the Header file is included within angular braces (<>), the compiler only searches in the working directory for the particular header file.

Q 22) What is a sequential access file?

Answer: General programs store data into files and retrieve existing data from files. With the sequential access file, such data are saved in a sequential pattern. When retrieving data from such files each data is read one by one until the required information is found.

Q 23) What is the method to save data in a stack data structure type?

Answer: Data is stored in the Stack data structure type using the First In Last Out (FILO) mechanism. Only top of the stack is accessible at a given instance. Storing mechanism is referred as a PUSH and retrieve is referred to as a POP.

Q 24) What is the significance of C program algorithms?

Answer: The algorithm is created first and it contains step by step guidelines on how the solution should be. Also, it contains the steps to consider and the required calculations/operations within the program.

Q 25) Explain the use of function toupper() with an example code?

Answer: Toupper() function is used to convert the value to uppercase when it used with characters.

Code:

    #include <stdio.h>

    #include <ctype.h>

    int main()

    {

             char c;

             c = 'a';

             printf("%c -> %c", c, toupper(c));

             c = 'A';

             printf("\n%c -> %c", c, toupper(c));

             c = '9';

             printf("\n%c -> %c", c, toupper(c));

             return 0;

    }

    Result:

    1    a -> A

    2    A -> A

    3    9 -> 9

Q 26)  Is it possible to use curly brackets ({}) to enclose a single line code in C program?

Answer: Yes, it works without any error. Some programmers like to use this to organize the code. But the main purpose of curly brackets is to group several lines of codes.

Q 27) Describe the modifier in C?

Answer: Modifier is a prefix to the basic data type which is used to indicate the modification for storage space allocation to a variable.

Example– In a 32-bit processor, storage space for the int data type is 4.When we use it with modifier the storage space change as follows:

    Long int: Storage space is 8 bit

    Short int: Storage space is 2 bit

Q 27) What are the modifiers available in C programming language?

Answer: There are 5 modifiers available in the C programming language as follows:

    Short

    Long

    Signed

    Unsigned

    long long

Q 28)  Is that possible to store 32768 in an int data type variable?

Answer: Int data type is only capable of storing values between – 32768 to 32767. To store 32768 a modifier needs to used with the int data type. Long Int can use and also if there are no negative values, unsigned int is also possible to use.

Q 29) Is there any possibility to create a customized header file with C programming language?

Answer: Yes, it is possible and easy to create a new header file. Create a file with function prototypes that are used inside the program. Include the file in the ‘#include’ section from its name.

Q 30) Describe dynamic data structure in C programming language?

Answer: Dynamic data structure is more efficient to memory. The memory access occurs as needed by the program.

Q 31) Is that possible to add pointers to each other?

Answer: There is no possibility to add pointers together. Since pointer contains address details there is no way to retrieve the value from this operation.

Q 32) What is indirection?

Answer: If you have defined a pointer to a variable or any memory object, there is no direct reference to the value of the variable. This is called the indirect reference. But when we declare a variable, it has a direct reference to the value.

Q 33) What are the ways to a null pointer that can be used in the C programming language?

Answer: 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.

Q 34) What is the explanation for modular programming?

Answer: The process of dividing the main program into executable subsection is called module programming. This concept promotes reusability.


Q 35) What's the value of the expression 5["abxdef"]?

Answer: The answer is 'f'.

Explanation: The string mentioned "abxdef" is an array, and the  expression is equal to "abxdef"[5]. Why is the inside-out expression equivalent?  Because a[b] is equivalent to *(a + b) which is equivalent to *(b + a) which is equivalent to b[a].

Q 36) What is a built-in function in C?

Answer:The most commonly used built-in functions in C are sacnf( ), printf( ), strcpy, strlwr, strcmp, strlen, strcat, and many more.

Built-function is also known as library functions are provided by the system to make the life of a developer easy by assisting them to do the certain commonly used predefined task. For example: if you need to print output or your program into the terminal we use printf( ) in C.

Q 37) What is the difference between text files and binary files?

Answer: 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 1s and 0s that only computers can interpret.

Q 38) Is it possible to create your own header files?

Answer: Yes, it is possible to create a customized header file. Just include in it the function prototypes that you want to use in your program, and use the #include directive followed by the name of your header file.

Q 39)What is dynamic data structure?

Answer: 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.

Q 40) What are the different data types in C?

Answer: 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.

Q 41) What is the general form of a C program?

Answer: A C program begins with the pre-processor directives, in which the programmer would specify which header file and what constants (if any) to be used. This is followed by the main function heading. Within the main function lies the variable declaration and program statement.

Q 42) What is the advantage of a random access file?

Answer: If the amount of data stored in a file is fairly large, the use of random access will allow you to search through it quicker. If it had been a sequential access file, you would have to go through one record at a time until you reach the target data. A random access file lets you jump directly to the target address where data is located.

Q 43) In a switch statement, what will happen if a break statement is omitted?

Answer: If a break statement was not placed at the end of a particular case portion? It will move on to the next case portion, possibly causing incorrect output.

Q 44) Describe how arrays can be passed to a user defined function?

Answer: One thing to note is that you cannot pass the entire array to a function. Instead, you pass to it a pointer that will point to the array first element in memory. To do this, you indicate the name of the array without the brackets.

Q 45) What are pointers?

Answer: Pointers point to specific areas in the memory. Pointers contain the address of a variable, which in turn may contain a value or even an address to another memory.

Q 46) Can you pass an entire structure to functions?

Answer: Yes, it is possible to pass an entire structure to a function in a call by method style. However, some programmers prefer declaring the structure globally, then pass a variable of that structure type to a function. This method helps maintain consistency and uniformity in terms of argument type.

Q 47) What is gets( ) function?

Answer: The gets( ) function allows a full line data entry from the user. When the user presses the enter key to end the input, the entire line of characters is stored to a string variable. Note that the enter key is not included in the variable, but instead a null terminator \0 is placed after the last character.

Q 48) The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?

Answer: You can do this by using %% in the printf statement. For example, you can write printf("10%%") to have the output appear as 10% on the screen.

Q 49) How do you search data in a data file using random access method?

Answer: Use the fseek() function to perform random access input/ouput on a file. After the file was opened by the fopen() function, the fseek would require three parameters to work: a file pointer to the file, the number of bytes to search, and the point of origin in the file.

Q 50) Are comments included during the compilation stage and placed in the EXE file as well?

Answer: No, comments that were encountered by the compiler are disregarded. Comments are mostly for the guidance of the programmer only and do not have any other significant use in the program functionality.

Q 51) Is there a built-in function in C that can be used for sorting data?

Answer: Yes, use the qsort( ) function. It is also possible to create user defined functions for sorting, such as those based on the balloon sort and bubble sort algorithm.

Q 52) What are the advantages and disadvantages of a heap?

Answer: Storing data on the heap is slower than it would take when using the stack. However, the main advantage of using the heap is its flexibility. That's because memory in this structure can be allocated and remove in any particular order. Slowness in the heap can be compensated if an algorithm was well designed and implemented.

Q 53) How do you convert strings to numbers in C?

Answer: You can write you own functions to do string to number conversions, or instead use C's built in functions. You can use atof to convert to a floating point value, atoi to convert to an integer value, and atol to convert to a long integer value.

Q 54) Create a simple code fragment that will swap the values of two variables num1 and num2.

Answer:

int temp;

temp = num1;

num1 = num2;

num2 = temp;

Q 55) What is the use of a semicolon (;) at the end of every program statement?

Answer: It has to do with the parsing process and compilation of the code. A semicolon acts as a delimiter, so that the compiler knows where each statement ends, and can proceed to divide the statement into smaller elements for syntax checking.

 Q 56) Can I create a customized Head File in C language?

Answer: It is possible to create a new header file. Create a file with function prototypes that need to be used in the program. Include the file in the ‘#include’ section in its name.

Q 57) Write a code to print the following pattern.

     1
    12
    123
    1234
    12345

Answer: To print the above pattern, the following code can be used.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include&lt;stdio.h&gt;
int main()
{
      for(i=1;i&lt;=5;1++)
      {
           for(j=1;j&lt;=5;j++)
           {
                print("%d",j);
           }
           printf("n");
      }
      return 0;
}

Q 58) Explain the # pragma directive.

Answer: The following points explain the Pragma Directive.

  • This is a preprocessor directive that can be used to turn on or off certain features.
  • It is of two types #pragma startup, #pragma exit and pragma warn.
  • #pragma startup allows us to specify functions called upon program startup.
  • #pragma exit allows us to specify functions called upon program exit.
  • #pragma warn tells the computer to suppress any warning or not.
 
Q 59) Which structure is used to link the program and the operating system?
Answer: The answer can be explained through the following points,
  • The structure used to link the operating system to a program is file.
  • The file is defined in the header file “stdio.h”(standard input/output header file).
  • It contains the information about the file being used, its current size and its location in memory.
  • It contains a character pointer that points to the character that is being opened.
  • Opening a file establishes a link between the program and the operating system about which file is to be accessed.
 
Q 60) What are the limitations of scanf() and how can it be avoided?
Answer: The Limitations of scanf() are as follows:
  • scanf() cannot work with the string of characters.
  • It is not possible to enter a multiword string into a single variable using scanf().
  • To avoid this the gets( ) function is used.
  • It gets a string from the keyboard and is terminated when enter key is pressed.
  • Here the spaces and tabs are acceptable as part of the input string.
 
Q 61Differentiate between the macros and the functions?
Answer: The differences between macros and functions can be explained as follows:
  • Macro call replaces the templates with the expansion in a literal way.
  • The Macro call makes the program run faster but also increases the program size.
  • Macro is simple and avoids errors related to the function calls.
  • In a function, call control is transferred to the function along with arguments.
  • It makes the functions small and compact.
  • Passing arguments and getting back the returned value takes time and makes the program run at a slower rate.
Q 62) Suppose a global variable and local variable have the same name. Is it is possible to access a global variable from a block where local variables are defined?
Answer: No. It is not possible in C. It is always the most local variable that gets preference.

Q 63) What is static memory allocation?

Answer:

  • In case of static memory allocation, memory is allocated at compile time, and memory can't be increased while executing the program. It is used in the array.
  • The lifetime of a variable in static memory is the lifetime of a program.
  • The static memory is allocated using static keyword.
  • The static memory is implemented using stacks or heap.
  • The pointer is required to access the variable present in the static memory.
  • The static memory is faster than dynamic memory.
  • In static memory, more memory space is required to store the variable.
For Example:
        int a[10];
Q 64) What is an auto keyword in C?

Answer: In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.

Q 65) What is the difference between near, far and huge pointers?

Answer: A virtual address is composed of the selector and offset.

A near pointer doesn't have explicit selector whereas far, and huge pointers have explicit selector. When you perform pointer arithmetic on the far pointer, the selector is not modified, but in case of a huge pointer, it can be modified.

These are the non-standard keywords and implementation specific. These are irrelevant in a modern platform.

Q 66)  What is the maximum length of an identifier?

Answer: It is 32 characters ideally but implementation specific.

Q 67)Why is C called a mid-level programming language?

Answer: C is called a mid-level programming language because it binds the low level and high -level programming language. We can use C language as a System programming to develop the operating system as well as an Application programming to generate menu driven customer driven billing system.

Q 68What is recursion in C?

Answer: When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.

Recursive function comes in two phases:

  1. Winding phase
  2. Unwinding phase

Winding phase: When the recursive function calls itself, and this phase ends when the condition is reached.

Unwinding phase: Unwinding phase starts when the condition is reached, and the control returns to the original call.

    Example of recursion:-

  1. #include <stdio.h>  
  2. int calculate_fact(int);  
  3. int main()  
  4. {  
  5.  int n=5,f;  
  6.  f=calculate_fact(n); // calling a function  
  7.  printf(“factorial of a number is %d”,f);  
  8.   return 0;  
  9. }  
  10. int calculate_fact(int a)  
  11. {  
  12.   if(a==1)  
  13.   {  
  14.       return 1;  
  15.   }  
  16.   else  
  17.   return a*calculate_fact(a-1); //calling a function recursively.  
  18.    }  

Output:

 factorial of a number is 120

Q 69) Why is int known as a reserved word?
Answer: As int is a part of standard C language library, and it is not possible to use it for any other activity except its intended functionality, it is known as a reserved word.
Q 70) What is the stack area?

Answer: The stack area is used to store arguments and local variables of a method. It stays in memory until the particular method is not terminated.

Conclusion

The questioner is based on the C programming language , the knowledge of its syntax and some example programs that use the Basic C programming. Theatrical and practical knowledge of the candidate is examined with the questions.

Also Join Us:-

Post a Comment

0 Comments