C Interview Q&A.txt2
C Interview Q&A.txt2
Ans. C is one of the oldest programming languages and was developed in 1972 by Dennis
Ritchie. It is a high-level, general-purpose, and structured programming language that is widely
used in various tasks, such as developing system applications, desktop applications, operating
systems as well as IoT applications. It is the simple and flexible language used for a variety of
scripting system applications, which form a significant part of Windows, UNIX, and Linux
operating systems.
Q3. Who is the founder of the C language? When was the C language developed?
Ans. C is developed by Dennis M. Ritchie in 1972 at the Bell laboratories of AT & T.
Machine Independent
Mid-Level programming language
Memory Management
Structured programming language
Simple and efficient
Function rich libraries
Case Sensitive
Q5. Name some different storage class specifiers in C?
Ans. Storage classes represent the storage of any variable. There are four storage classes in C:
Auto
Register
Extern
Static
Q6. What are the data types supported in the C Programming language?
Ans. This is one of the commonly asked C programming interview questions. The data type
specifies the type of data used in programming. C language has some predefined data types with
different storage capacities:
Built-in data types: It includes int, char, double, float and void
Derived data types: It includes array, pointers, and references
User-defined data types: Union, structure, and Enumeration
Q7. What do you mean by the scope and lifetime of a variable in C?
Ans. The scope and lifetime of any variable are defined as the section of the code in which the
variable is executable or visible. Variables that are described in the block are only accessible in
the block. The lifetime of the variable defines the existence of the variable before it is destroyed.
Example:
#include <stdio.h>
int main()
int *ptr, q;
q = 50;
ptr = &q;
printf(“%d”, *ptr);
return 0;
It will assign the pointer variable to the variable with no assigned memory
In pointer related code we can do error handling by checking null pointer before assigning any
pointer variable
Pass a null pointer to the variable if we don’t want to pass any valid memory address
Example:
return 10;
fun(NULL);
#include<string.h>
char *getHello()
{
char str[10]; strcpy(str,”Hello!”);
return(str);
int main()
printf(“%s”, getHello());
int *p1;
…..
int ch;
p1 = &ch;
…..
#include<stdlib.h>
int main()
char **strPtr;
printf(“%s”, *strPtr);
Uses of functions in C:
Functions can be used multiple times in a program by calling them whenever required
In C, functions are used to avoid rewriting of code
It is easy to track any C program when it is divided into functions
Syntax of a function
Example:
#include <stdio.h>
int main()
int a = 9,b = 4, c;
c = a+b;
printf(“a+b = %d \n”,c);
c = a-b;
printf(“a-b = %d \n”,c);
c = a*b;
printf(“a*b = %d \n”,c);
c = a/b;
printf(“a/b = %d \n”,c);
c = a%b;
return 0;
Relational Operators:
These are used to check the relation between two operands. If the relation is TRUE, it returns 1; If
the relation is FALSE, It returns 0.
Example:
#include <stdio.h>
int main()
{
int a = 2, b = 2, c = 6;
return 0;
Logical Operators:
These are used in decision-making operations. If the expression is TRUE, it returns 1; if the
expression is FALSE, it returns 0.
Example:
#include <stdio.h>
int main()
int a = 2, b = 2, c = 6, result;
return 0;
Assignment Operators:
These are used to assign value to a variable. The most used assignment operator is ‘=’.
Example:
#include <stdio.h>
int main()
int a = 5, c;
c = a; // c is 5
c += a; // c is 10
c -= a; // c is 5
c *= a; // c is 25
c /= a; // c is 5
return 0;
Example:
#include <stdio.h>
int main()
return 0;
Bitwise Operators:
These are used to perform bit-level operations between two variables.
Conditional Operator:
These are used in conditional expressions.
Special Operators:
There are some special operators in C used for:
*: Pointer of a variable
Q15. Explain the process of creating increment and decrement operators in C.
Ans. This is one of the most important C programming interview questions. If you want to
perform an increment operation, then use ‘i++,’ which will increase the value by 1. If you want to
perform a decrement operation, then use ‘i–,’ it will decrease the value by 1.
Example:
#include<stdio.h>
int main()
int x = 10, y = 1;
y = ++x;
y = –x;
return 0;
// Global variable
float x = 1;
void my_test() {
float y = 77;
println(x);
println(y);
void setup() {
float y = 2;
println(x);
println(y);
my_test();
println(y);
Example:
void demo
int x;
int main()
int y;
int c[10];
return 1;
Syntax of calloc()
Syntax of malloc()
#include <stdio.h>
int main()
int i = 1;
do
printf(“%d\n”, i);
i++;
if (i < 15)
continue;
} while (false);
getchar();
return 0;
Ans. Output: 1
The do-while loop executes at every iteration. After the continue statement, it will come to the
while (false) statement, and the condition shows false, and ‘i’ is printed only once.
Example:
#include <stdio.h>
int main()
int a = -4;
int b = -3;
Output:
1.333333
1.000000
-0.000000
-1431655764.000000
Example:
#include<stdio.h>
struct address
char city[20];
int pin;
char phone[14];
};
struct employee
char name[20];
};
void main ()
printf(“name:%s\nCity:%s\nPincode:%d\nPhone:
%s”,emp.name,emp.add.city,emp.add.pin,emp.add.phone);
Output:
Joe
Delhi
110001
1234567890
name: Joe
City: Delhi
Pincode: 110001
Phone: 123456789
#include <stdio.h>
int main()
int x, y;
x = x + y;
y = x – y;
x = x – y;
return 0;
#include<stdio.h>
int main()
int n1=0,n2=1,n3,i,number;
scanf(“%d”,&number);
n3=n1+n2;
printf(” %d”,n3);
n1=n2;
n2=n3;
return 0;
Example: x=(a+b);
y=x;
Equal to operator (==): It is also a binary operator used to compare the left-hand side and right-
hand side value, if it is the same, it returns 1 else 0.
int x,y;
x=10;
y=10;
if(x==y)
printf(“True”);
else
printf(“False”);
Let’s move forward and take a look at some more frequently asked C programming interview
questions.
Push
Pop
Peek or Top
isEmpty
Q30. When is the arrow operator used?
Ans. Arrow operator is used to accessing elements in structure and union. It is used with a
pointer variable. Arrow operator is formed by using a minus sign followed by a greater than a
symbol.
Syntax:
(pointer_name)->(variable_name)
#include <stdio.h>
int factorial_of_a_number(int n)
int fact = 1, i;
if(n == 0)
return 1;
else
fact = fact * i;
return fact;
int main()
int n;
printf(“Enter the number : “);
scanf(“%d”,&n);
if(n < 0)
printf(“Invalid output”);
else
return 0;
Keywords
Constants
Identifiers
Strings
Operators
Special symbols
Also Read: Best Software Development Frameworks & Tools To Learn
#include <stdio.h>
void main()
sum = sum + i;
Implicit conversion
Explicit conversion
Example:
#include <stdio.h>
main() {
double mean;
Example:
#include <time.h>
#include <stdlib.h>
Also Read: Top Universities Offering Free Online Courses For Programmers
Q40. What are the disadvantages of void pointer?
Ans. Disadvantages of a void pointer:
Pointer arithmetic is not defined for void pointer
Void pointers can’t be dereferenced
Q41. Define compound statements.
Ans. These are made up of two or more program statements that are executed together.
Q42. Write a program to print numbers from 1 to 100 without using a loop.
Ans. Program to print numbers from 1 to 100
if(n > 0)
printNos(n-1);
printf(“%d “, n);
Q45. What is the name of the function used to close the file stream?
Ans. Fclose().
Example:
struct Point
int x, y;
struct Point
{
int x, y;
};
int main()
#include <stdio.h>
int main()
scanf(“%d”, &n);
while(n != 0)
rem = n%10;
n /= 10;
printf(“%d\n”, rev);
return 0;
#include<stdio.h>
#include<conio.h>
void main()
scanf(“%d”,&n);
m=n/2;
for(i=2;i<=m;i++)
if(n%i==0)
flag=1;
if(flag==0)
printf(“Number is prime”);
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum=0,temp; //declaration of variables.
scanf(“%d”,&n);
temp=n;
while(n>0)
r=n%10;
sum=sum+(r*r*r);
n=n/10;
if(temp==sum)
else
#include<stdio.h>
#include<conio.h>
main()
int n,r,sum=0,temp;
clrscr();
scanf(“%d”,&n);
temp=n;
while(n>0)
r=n%10;
sum=(sum*10)+r;
n=n/10;
if(temp==sum)
else
printf(“not palindrome”);
getch();
Q51. What are reserved keywords? How many reserved keywords are there in C?
Ans. Reserved keywords are those keywords that have predefined meanings and cannot be used
as a variable name. Such keywords are restricted for general use while writing a program. There
are 32 reserved keywords in C programming language:
Reserved Keywords
auto
break
case
char
const
continue
default
do double
else
enum
extern
float
for
goto
if int
long
register
return
short
signed
sizeof
static struct
switch
typedef
union
unsigned
voidvolatilewhile
Q52. What is a union?
Ans. A union is a user-defined data type that enables you to store multiple types of data in a
single unit or the same memory location. While you can define a union with different members,
only one member can hold a value at any given time. Also, it does not hold the sum of the
memory of all members and holds the memory of the largest member only.
Q55. What is the difference between static memory allocation and dynamic memory allocation?
Ans. The differences between static memory allocation and dynamic memory allocation are:
while(0) means that the looping conditions will always be false and the code inside the while
loop will not be executed. On the other hand, while(1) is an infinite loop. It runs continuously until
it comes across a break statement mentioned explicitly.
Q58. Explain is the difference between prefix increment and postfix increment.
Ans. Both prefix increment and postfix increment do what their syntax implies, i.e. increment the
variable by 1.
The prefix increment increments the value of the variable before the program execution and
returns the value of a variable after it has been incremented. The postfix increment, on the other
hand, increments the value of the variable after the program execution and returns the value of a
variable before it has been incremented.
On the other hand, void pointers are generic pointers that do not have any data type associated
with them. They can contain the address of any type of variable. Void pointers can point to any
data type.
Q60. Explain the difference between Call by Value and Call by Reference?
Ans. Call by Value sends the value of a variable as a parameter to a function. Moreover, the value
in the parameter is not affected by the operation that takes place.
Call by Reference, on the other hand, passes the address of the variable, instead of passing the
values of variables. In this, values can be affected by the process within the function.
Q62. What is the difference between actual parameters and formal parameters?
Ans. The differences between actual parameters and formal parameters are:
Q65. What is the difference between const char* p and char const* p?
Ans. const char* p is a pointer to a constant character whereas char const* p is a constant
pointer to a non-constant character. In const char* p, we cannot change the value pointed by ptr.
However, we can the pointer. In char *const ptr, we cannot change the pointer p. However, the
value pointed by ptr can be changed.
Syntax:
#pragma startup: It is used to specify the functions that are needed to run before the program
starts.
#pragma exit: It is used to specify the functions that are needed to run just before program exit.
#pragma warn: It is used to hide the warning messages which are displayed during compilation.
#pragma GCC poison: This directive is used to remove an identifier completely from the
program.
#pragma GCC dependency: This directive allows you to check the relative dates of the current file
and another file.
#pragma once: It allows the current source file to be included only once in a single compilation.
Q69. What is variable initialization in C Programming?
Ans. Variable initialization refers to the process of assigning a value to the variable before it is
used in the program. Using variables without initialization can result in unexpected outputs.
There are two types of variable initialization:
data_type variable_name=constant/literal/expression;
or
variable_name=constant/literal/expression;
Q70. What is the difference between Source Code and Object Code?
Ans. The differences between source code and object code are:
#include <stdio.h>
int main() {
int num;
scanf(“%d”, &num);
if(num % 2 == 0)
else
return 0;
Output:
Enter an integer: 77
77 is odd.
Q72. Write a program to swap two numbers without the use of the third variable.
Ans. We can swap two numbers without the use of the third variable by two methods:
int main()
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
return 0;
#include<stdlib.h>
int main()
a=a*b;//a=200 (10*20)
b=a/b;//b=10 (200/20)
a=a/b;//a=20 (200/10)
system(“cls”);
return 0;
Output:
Q73. What is recursion in C? Write a program to find the factorial of a number using recursion.
Ans. Recursion refers to the process in which a function calls itself directly or indirectly. The
function that calls itself is known as a recursive function.
Program to find the factorial of a number using recursion
#include <stdio.h>
int main()
int n,f;
scanf(“%d”,&n);
f = fact(n);
printf(“Factorial = %d”,f);
int fact(int n)
if (n==0)
return 0;
else if ( n == 1)
return 1;
else
return n*fact(n-1);
Output
Enter a positive integer: 5
Factorial = 120
Macros Functions
Macros are pre-processed. It means that all the macros will be processed before the program
compiles. Functions are not preprocessed but compiled.
Macros do not check for compilation errors which leads to unexpected output. Function checks
for compilation errors. Chances of unpredictable output are less.
It increases the code length. Code length remains the same.
Macros are useful when small code is repeated many times. Functions are useful in large
codes.
Before Compilation, the macro name is replaced by macro value. During function call, transfer
of control takes place.
Faster in execution. A bit slower in execution than Macros.
They do not check any Compile-Time errors. Functions check Compile-Time errors.
Q75. Explain pointer to pointer in C.
Ans. Pointer to pointer is a concept in which one pointer refers to the address of another pointer.
It is a form of multiple indirections or a chain of pointers. The first pointer stores the address of a
variable whereas the second pointer stores the address of the first pointer.
Example:
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 2000;
ptr = &var;
pptr = &ptr;
Output:
12
123
1234
12345
#include<stdio.h>
int main()
for(i=1;i<=5;1++)
for(j=1;j<=5;j++)
print(“%d”,j);
printf(“n”);
return 0;
#include <stdio.h>
int main()
scanf(“%d”, &number);
scanf(“%d”, &a[i]);
temp = a[j];
a[j + 1] = temp;
printf(“\n”);
return 0;
Output:
We hope these C interview questions and answers will help you crack your upcoming interview
easily!
Conclusion
We hope these C programming interview questions will give you an in-depth understanding of
different concepts in C and help you clear your next job interview.