Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
200 views

C Programming Interview Questions Edu

The document discusses C programming interview questions. It covers beginner, intermediate, and advanced questions related to C programming. Some example questions covered include data types in C, dangling pointers, scope of variables, static variables and functions, differences between calloc() and malloc(), valid uses of break statements, storing negative integers, actual and formal parameters, if a C program can run without main(), and nested structures. The document aims to help prepare for C programming interviews by providing a list of common questions asked at various experience levels.

Uploaded by

pooja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
200 views

C Programming Interview Questions Edu

The document discusses C programming interview questions. It covers beginner, intermediate, and advanced questions related to C programming. Some example questions covered include data types in C, dangling pointers, scope of variables, static variables and functions, differences between calloc() and malloc(), valid uses of break statements, storing negative integers, actual and formal parameters, if a C program can run without main(), and nested structures. The document aims to help prepare for C programming interviews by providing a list of common questions asked at various experience levels.

Uploaded by

pooja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

C Programming Interview Questions 

have become a crucial part of the interview process in


almost all MNC companies. This article is mainly focused on the most asked and the latest
updated questions that are appearing in most of the current interviews. You will also get a mix
of Basic to Advanced C Programming Interview Questions in this article.

 Beginners C Programming Interview Questions


 Intermediate C Programming Interview Questions
 Advanced C Programming Interview Questions

Want to Upskill yourself to get ahead in your career? Check out the Top Trending
Technologies.
 

C Programming Interview Questions


 What are the basic Datatypes supported in C Programming Language?
 What do you mean by Dangling Pointer Variable in C Programming?
 What do you mean by the Scope of the variable? What is the scope of the variables in C?
 What are static variables and functions?
 Differentiate between calloc() and malloc()
 What are the valid places where the programmer can apply Break Control Statement?
 How can we store a negative integer?
 Differentiate between Actual Parameters and Formal Parameters.
 Can a C program be compiled or executed in the absence of a main()?
 What do you mean by a Nested Structure?

Beginners C Programming Interview Questions


Q1. What are the basic Datatypes supported in C Programming Language?

Ans: The Datatypes in C Language are broadly classified into 4 categories. They are as follows:

 Basic Datatypes
 Derived Datatypes
 Enumerated Datatypes
 Void Datatypes

The Basic Datatypes supported in C Language are as follows:

Datatype Name Datatype Size Datatype Rang


short 1 byte -128 to 127
unsigned short 1 byte 0 to 255
char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
int 2 bytes -32,768 to 32,76
unsigned int 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147
unsigned long 4 bytes 0 to 4,294,967,29
float 4 bytes 3.4E-38 to 3.4E+
double 8 bytes 1.7E-308 to 1.7E+
long double 10 bytes 3.4E-4932 to 1.1E+
 

Q2. What do you mean by Dangling Pointer Variable in C Programming?


 

Ans: A Pointer in C Programming is used to point the memory location of an existing variable.
In case if that particular variable is deleted and the Pointer is still pointing to the same memory
location, then that particular pointer variable is called as a Dangling Pointer Variable.

Q3. What do you mean by the Scope of the variable? What is the scope of the
variables in C?

Ans: Scope of the variable can be defined as the part of the code area where the variables
declared in the program can be accessed directly. In C, all identifiers are lexically (or statically)
scoped. 

Q4. What are static variables and functions?

Ans: The variables and functions that are declared using the keyword Static are considered as
Static Variable and Static Functions. The variables declared using Static keyword will have their
scope restricted to the function in which they are declared.

Q5. Differentiate between calloc() and malloc()

Ans: calloc() and malloc() are memory dynamic memory allocating functions. The only


difference between them is that calloc() will load all the assigned memory locations with value 0
but malloc() will not.

Q6. What are the valid places where the programmer can apply Break Control
Statement?

Ans: Break Control statement is valid to be used inside a loop and Switch control statements.

Q7. How can we store a negative integer?

 
Ans: To store a negative integer, we need to follow the following steps. Calculate the two’s
complement of the same positive integer.

Eg: 1011 (-5)

Step-1 − One’s complement of 5: 1010

Step-2 − Add 1 to above, giving 1011, which is -5

Q8. Differentiate between Actual Parameters and Formal Parameters.

Ans: The Parameters which are sent from main function to the subdivided function are called
as Actual Parameters and the parameters which are declared a the Subdivided function are
called as Formal Parameters.

Q9. Can a C program be compiled or executed in the absence of a main()?

Ans: The program will be compiled but will not be executed. To execute any C program, main()
is required.

Q10. What do you mean by a Nested Structure?

Ans: When a data member of one structure is referred by the data member of another function,
then the structure is called a Nested Structure.

Q11. What is a C Token?

Ans: Keywords, Constants, Special Symbols, Strings, Operators, Identifiers used in C program


are referred to as C Tokens.

Q12. What is Preprocessor?


Ans: A Preprocessor Directive is considered as a built-in predefined function or macro that acts
as a directive to the compiler and it gets executed before the actual C Program is executed.

#include<stdio.h>

Q13. Why is C called the Mother of all Languages?

Ans: C introduced many core concepts and data structures like arrays, lists, functions,
strings, etc. Many languages designed after C are designed on the basis of C Language.
Hence, it is considered as the mother of all languages.

Q14. Mention the features of C Programming Language.

Ans: 

Q15. What is the purpose of printf() and scanf() in C Program?


Ans: printf() is used to print the values on the screen. To print certain values, and on the other
hand, scanf() is used to scan the values. We need an appropriate datatype format specifier for
both printing and scanning purposes. For example,

 %d: It is a datatype format specifier used to print and scan an integer value.


 %s: It is a datatype format specifier used to print and scan a string.
 %c: It is a datatype format specifier used to display and scan a character value.
 %f: It is a datatype format specifier used to display and scan a float value.

Q16. What is an array?

Ans. The array is a simple data structure that stores multiple elements of the same datatype in
a reserved and sequential manner. There are three types of arrays, namely,

 One Dimensional Array


 Two Dimensional Array
 Multi-Dimensional Array

Q17. What is \0 character?

Ans: The Symbol mentioned is called a Null Character. It is considered as the terminating


character used in strings to notify the end of the string to the compiler.

Q18. What is the main difference between the Compiler and the Interpreter?

Ans: Compiler is used in C Language and it translates the complete code into the Machine
Code in one shot. On the other hand, Interpreter is used in python Langauge and other high-
end programming languages. It is designed to interpret the code in line by line fashion.

Q19. Can I use int datatype to store 32768 value?

Ans: No, Integer datatype will support the range between -32768 and 32767. Any value
exceeding that will not be stored. We can either use float or long int.

 
Intermediate C Programming Interview Questions

Q20. How is a Function declared in C Language?

Ans: A function in C language is declared as follows,

1 return_type function_name(formal parameter list)


2 {
3        Function_Body;
}
4
 

Q21. What is Dynamic Memory allocation? Mention the syntax. 

Ans: Dynamic Memory Allocation is the process of allocating memory to the program and its
variables in runtime. Dynamic Memory Allocation process involves three functions for allocating
memory and one function to free the used memory.

malloc() – Allocates memory

Syntax:

1 ptr = (cast-type*) malloc(byte-size);


calloc() – Allocates memory

Syntax:
1 ptr = (cast-type*)calloc(n, element-size);
realloc() – Allocates memory

Syntax:

1 ptr = realloc(ptr, newsize);


free() – Deallocates the used memory

Syntax:

1 free(ptr);
 

Q22. What do you mean by Dangling Pointer Variable in C Programming?

Ans: A Pointer in C Programming is used to point the memory location of an existing variable.
In case if that particular variable is deleted and the Pointer is still pointing to the same memory
location, then that particular pointer variable is called as a Dangling Pointer Variable.

Q23. Where can we not use &(address operator in C)?

Ans: We cannot use & on constants and on a variable which is declared using the register


storage class.

Q24. Write a simple example of a structure in C Language

Ans: Structure is defined as a user-defined data type that is designed to store multiple data
members of the different data types as a single unit. A structure will consume the memory equal
to the summation of all the data members.

1 struct employee
{
2     char name[10];
3     int age;
4 }e1;
5 int main()
6 {
    printf("Enter the name");
7     scanf("%s",e1.name);
8     printf("n");
9     printf("Enter the age");
10     scanf("%d",&e1.age);
11     printf("n");
12
13     printf("Name and age of the employee: %s,%d",e1.name,e1.age);
14     return 0;
15 }
16
 

Q25. Differentiate between call by value and call by reference.

Ans:

Factor Call by Value Call by Reference


Actual arguments cannot be changed and Operations are performed on a
Safety
remain safe arguments, hence not safe
Memory Separate memory locations are created for Actual and Formal arguments share
Location actual and formal arguments memory space.
Arguments Copy of actual arguments are sent Actual arguments are passe
//Example of Call by Value method

1
2 #include<stdio.h> 
3 void change(int,int); 
4 int main() 
5 { 
    int a=25,b=50; 
6     change(a,b);
7     printf("The value assigned to a is: %d",a); 
8     printf("n"); 
9     printf("The value assigned to of b is: %d",b); 
    return 0; 
10 } 
11 void change(int x,int y) 
12 { 
13     x=100; 
14     y=200; 
}
15
16
//Output

The value assigned to of a is: 25


The value assigned to of b is: 50

//Example of Call by Reference method

1 #include<stdio.h>
2
3 void change(int*,int*); 
4 int main() 
5 { 
    int a=25,b=50; 
6     change(&a,&b);
7     printf("The value assigned to a is: %d",a); 
8     printf("n"); 
9     printf("The value assigned to b is: %d",b); 
10     return 0; 

11 void change(int *x,int *y) 
12 { 
13     *x=100; 
14     *y=200; 
}
15
16
//Output

The value assigned to a is: 100


The value assigned to b is: 200

In case you are facing any challenges with these C Programming Interview Questions, please
write your problems in the comment section below.

Q26. Differentiate between getch() and getche().

Ans: Both the functions are designed to read characters from the keyboard and the only
difference is that

getch(): reads characters from the keyboard but it does not use any buffers. Hence, data is not
displayed on the screen.

getche(): reads characters from the keyboard and it uses a buffer. Hence, data is displayed on
the screen.

//Example

1 #include<stdio.h>
#include<conio.h>
2 int main()
3 {
4      char ch;
5      printf("Please enter a character ");
6      ch=getch();
     printf("nYour entered character is %c",ch);
7      printf("nPlease enter another character ");
8      ch=getche();
9      printf("nYour new character is %c",ch);
     return 0;
10
11 }
12
13
//Output

Please enter a character


Your entered character is x
Please enter another character z
Your new character is z

Q27. Explain toupper() with an example.

Ans. toupper() is a function designed to convert lowercase words/characters into upper case.

//Example

1
#include<stdio.h>
2 #include<ctype.h>
3 int main()
4 {
5     char c;
6     c=a;
    printf("%c after conversions  %c", c, toupper(c));
7     c=B;
8     printf("%c after conversions  %c", c, toupper(c));
9
//Output:

a after conversions A
B after conversions B

Q28. Write a code to generate random numbers in C Language.

Ans: Random numbers in C Language can be generated as follows:

1 #include<stdio.h>
#include<stdlib.h>
2
int main()
3 {
4      int a,b;
5      for(a=1;a<=10;a++)
6      {
           b=rand();
7
8
9            printf("%dn",b);
     }
10      return 0;
11 }
12
//Output

1987384758
2057844389
3475398489
2247357398
1435983905

Q29. Can I create a customized Head File in C language?

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

Q30. What do you mean by Memory Leak?

Ans: Memory Leak can be defined as a situation where programmer allocates dynamic memory
to the program but fails to free or delete the used memory after the completion of the code. This
is harmful if daemons and servers are included in the program.

1 #include<stdio.h>
#include<stdlib.h>
2 int main()
3 {
4      int* ptr;
5      int n, i, sum = 0;
6      n = 5;
     printf("Enter the number of elements: %dn", n);
7      ptr = (int*)malloc(n * sizeof(int));
8      if (ptr == NULL)
9      {
10             printf("Memory not allocated.n");
11             exit(0);
     }
12      else
13      {
14             printf("Memory successfully allocated using malloc.n");
15             for (i = 0; i<= n; ++i)
            {
16                   ptr[i] = i + 1;
17              }
18
19
20
21              printf("The elements of the array are: ");
22              for (i = 0; i<=n; ++i)
23             {
                  printf("%d, ", ptr[i]);
24             }
25      }
26      return 0;
27 }
28
29
//Output

Enter the number of elements: 5


Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,

In case you are facing any challenges with these C Programming Interview Questions, please
write your problems in the comment section below.

Q31. Explain Local Static Variables and what is their use?

Ans: A local static variable is a variable whose life doesn’t end with a function call where it is
declared. It extends for the lifetime of the complete program. All calls to the function share the
same copy of local static variables.

1
2 #include<stdio.h>
3 void fun()
{
4     static int x;
5     printf("%d ", x);
6     x = x + 1;
7 }
8 int main()
{
9     fun();
10     fun();
11     return 0;
12 }
13
//Output

0 1

 
Q32. What is the difference between declaring a header file with < > and ” “?

Ans: If the Header File is declared using < > then the compiler searches for the header file
within the Built-in Path. If the Header File is declared using ” ” then the compiler will search for
the Header File in the current working directory and if not found then it searches for the file in
other locations.

Q33. When should we use the register storage specifier?

Ans: We use Register Storage Specifier if a certain variable is used very frequently. This helps
the compiler to locate the variable as the variable will be declared in one of the CPU registers.

Q34. Which statement is efficient and why? x=x+1; or x++; ?

Ans: x++; is the most efficient statement as it just a single instruction to the compiler while the
other is not.

Q35. Can I declare the same variable name to the variables which have different
scopes?

Ans: Yes, Same variable name can be declared to the variables with different variable scopes
as the following example.

1
int var;
2 void function()
3 {
4    int variable;
5 }
6 int main()
{
7    int variable;
8 }
9
 

Q36. Which variable can be used to access Union data members if the Union variable
is declared as a pointer variable?
Ans: Arrow Operator( -> ) can be used to access the data members of a Union if the Union
Variable is declared as a pointer variable.

Q37. Mention File operations in C Language.

Ans: Basic File Handling Techniques in C, provide the basic functionalities that user can
perform against files in the system.

Function Operation
fopen() To Open a File
fclose() To Close a File
fgets() To Read a File
fprint() To Write into a File
In case you are facing any challenges with these C Programming Interview Questions, please
write your problems in the comment section below.

Q38. What are the different storage class specifiers in C?

Ans: The different storage specifiers available in C Language are as follows:

 auto
 register
 static
 extern

Q39. What is typecasting?

Ans: Typecasting is a process of converting one data type into another is known as


typecasting. If we want to store the floating type value to an int type, then we will convert the
data type into another data type explicitly.

Syntax:

1 (type_name) expression;
 

Q40. Write a C program to print hello world without using a semicolon (;).

Ans: 
1 #include<stdio.h>     
2 void main()
3 {     
4       if(printf("hello world")){}
}
5
//Output:

hello world

Q41. Write a program to swap two numbers without using the third variable.

Ans:

1
2 #include<stdio.h>     
3 #include<conio.h>     
main()     
4 {     
5      int a=10, b=20;   
6      clrscr();      
7      printf("Before swapping a=%d b=%d",a,b);       
8      a=a+b;      
     b=a-b;   
9      a=a-b;     
10      printf("nAfter swapping a=%d b=%d",a,b);     
11      getch();     
12 }
13
//Output

Before swapping a=10 b=20


After swapping a=20 b=10

Advanced C Programming Interview Questions


 

Q42. How can you print a string with the symbol % in it?

Ans: There is no escape sequence provided for the symbol % in C. So, to print % we should
use ‘%%’ as shown below.

1 printf(“there are 90%% chances of rain tonight”);


 

Q43. Write a code to print the following pattern.

1
12
123
1234
12345

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

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

Q44. Explain the # pragma directive.

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

Q45. How can you remove duplicates in an array?

Ans: The following program will help you to remove duplicates from an array.

1 #include <stdio.h>
int main()
2
{
3      int n, a[100], b[100], calc = 0, i, j,count;
4      printf("Enter no. of elements in array.n");
5      scanf("%d", &n);
6      printf("Enter %d integersn", n);
     for (i = 0; i < n; i++)
7            scanf("%d", &a[i]);
8            for (i = 0; i<n; i++)
9            {
10                  for (j = 0; j<calc; j++)
11                  {
                        if(a[i] == b[j])
12                         break;  
13                   }
14            if (j== calc)
15            {
                  b[count] = a[i];
16                   calc++; 
17             }
18       }
19       printf("Array obtained after removing duplicate elementsn");
20       for (i = 0; i<calc; i++)
      { 
21              printf("%dn", b[i]);
22       }
23       return 0;
24
25
26 }
27
28
29
//Output

Enter no. of elements in array. 5


Enter 5 integers
12
11
11
10
4
Array obtained after removing duplicate elements
12
11
10
4

Q46. What is Bubble Sort Algorithm? Explain with a program.

Ans: Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements and swaps them if they are in the wrong order. The pass through the list is
repeated until the list is sorted. 

The following code executes Bubble Sort.

1 int main()
{
2       int array[100], n, i, j, swap;
3       printf("Enter number of elementsn");
4       scanf("%d", &n);
5       printf("Enter %d Numbers:n", n);
6       for(i = 0; i<n; i++)
           scanf("%d", &array[i]);
7            for(i = 0 ; i<n - 1; i++)
8            {
9                  for(j = 0 ; j < n-i-1; j++) { if(array[j]>array[j+1])
10                        {
11                                swap=array[j];
                               array[j]=array[j+1];
12                                array[j+1]=swap;
13                        }
14                  }
15            }
16
17
18            printf("Sorted Array:n");
           for(i = 0; i < n; i++)
19                  printf("%dn", array[i]);
20            return 0;
21 }
22
23
 

Q47. What is Round-robin algorithm? Write a code for Round Robin Scheduling.

Ans: Round-robin Algorithm is one of the algorithms employed by process and network


schedulers in computing in order to evenly distribute resources in the system.

The following code will execute Round Robin Scheduling

1 #include<stdio.h>
2   
3 int main()
{
4       int i, limit, total = 0, x, counter = 0, time_quant
5       int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_ti
6       float average_wait_time, average_turnaround_time;
7       printf("nEnter Total Number of Processes:t");
      scanf("%d", &limit);
8       x = limit;
9       for(i = 0; i<limit; i++)
10       {
11             printf("nEnter Details of Process[%d]n", i +
12             printf("Arrival Time:t");
            scanf("%d", &arrival_time[i]);
13             printf("Burst Time:t");
14             scanf("%d", &burst_time[i]);
15             temp[i] = burst_time[i];
16       }
17   
18       printf("nEnter Time Quantum:t");
      scanf("%d", &time_quantum);
19       printf("nProcess IDttBurst Timet Turnaround Timet Waiting
20       for(total = 0, i = 0; x != 0;)
21       {
22             if(temp[i] <= time_quantum && temp[i] > 0)
            {
23                   total = total + temp[i];
24                   temp[i] = 0;
25                   counter = 1;
26             }
27             else if(temp[i]>0)
            {
28                   temp[i] = temp[i] - time_quantum;
29                   total = total + time_quantum;
30
31
32
33
34
35             }
36             if(temp[i] == 0 && counter == 1)
            {
37                   x--;
38                   printf("nProcess[%d]tt%dtt %dttt %d", i + 1, burst_time[i], total - arriv
39 burst_time[i]);
40                   wait_time = wait_time + total - arrival_time[i] -
                  turnaround_time = turnaround_time + total - arri
41                   counter = 0;
42             }
43             if(i == limit - 1)
44             {
45                   i = 0;
            }
46             else if(arrival_time[i + 1] <= total)
47             {
48                   i++;
49             }
50             else
            {
51                   i = 0;
52             }
53       }
54   
55       average_wait_time = wait_time * 1.0 / limit;
      average_turnaround_time = turnaround_time * 1.0 / li
56
      printf("nnAverage Waiting Time:t%f", average_wait_ti
57       printf("nAvg Turnaround Time:t%fn", average_turnaround_
58       return 0;
59 }
60
61
62
63
64
//Output
 

In case you are facing any challenges with these C Programming Interview Questions, please
write your problems in the comment section below.

Q48. Which structure is used to link the program and the operating system?

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

Q49. What are the limitations of scanf() and how can it be avoided?

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

Q50. Differentiate between the macros and the functions.

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

You might also like