65 C Programming
65 C Programming
If you are preparing for a C programming interview, then you have reached the right place. This
article covers the top 65 C programming questions and answers which can be asked in your
next C interview.
C is one of the oldest programming languages and was developed in 1972 by Dennis Ritchie. It
is a 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.
Due to its flexibility, C is the building block for many other programming languages, such as
Java, Python, and C++, which is why many programming job interviews involve C Interview
questions.
Here are the most important C programming interview questions and answers that will help
you to improve your chances of getting a job by giving you an edge in the job market where
most global and local organizations, big or small, are looking for professionals proficient in C
programming.
Following are the top C programming interview questions that you must prepare for:
Ans. C is a high-level and general-purpose programming language. 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.
Ans. It is known as a mother language because most of the JVMs, compilers, and Kernels are
written in C language. If you know C, then you can easily grasp other programming languages.
Q3. Who is the founder of the C language? When was the C language developed?
• Machine Independent
• Mid-Level programming language
• Memory Management
• Structured programming language
• Simple and efficient
• Function rich libraries
• Case Sensitive
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. The data type specifies the type of data used in programming. C language has some
predefined data types with different storage capacity:
• 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
Ans. The scope and lifetime of any variable 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.
Ans. Pointers are the variables in C that store the address of another variable. It allocates the
memory for the variable at the run time. The pointer might be of different data types such as
int, char, float, double, etc.
Example: #include
<stdio.h> int
main()
{ int *ptr,
q; q = 50;
ptr = &q;
Check Out Top Free & Paid Programming Courses & Certifications
Ans. A null pointer represents the empty location in the computer’s memory. It is used in C for
various purposes:
• 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);
Ans. Dangling pointers are the pointers pointing to the memory location that has been deleted
or released. There are three different types of dangling pointers:
#include<stdio.h>
#include<string.h> char
*getHello()
return(str);
}
int main()
getHello());
{ int
*p1;
…..
{ int ch;
p1 = &ch;
…..
#include<stdlib.h> int
main()
= “Hello!”; strPtr =
&str; free(str);
printf(“%s”, *strPtr);
Ans. Functions are the basic building blocks of any programming language. All C programs are
written using the function to maintain reusability and understandability.
Uses of functions in C:
{
body of the function
Ans. At the time of compilation, the compiler generates a file with the same name as the C
program file with different extensions.
Ans. Header files are those which contain C function declaration and macro definitions that are
to be shared between sourced files. Header files are generated with the extension .h.
Ans. An operator is a symbol used to operate the values of variables. There is a wide range of
operators used in C programming, including –
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;
Relational Operators: These are used to check 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;
Example: #include
<stdio.h> int
main()
• 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
+= a; // c is 10
c -= a; // c is 5
c *= a; // c is 25
c /= a; // c is 5
c %= a; // c = 0
printf(“c = %d\n”, c);
return 0;
• Increment and Decrement Operators: These are used to change the value of an
operand (constant or variable) by 1.
Example: #include
<stdio.h> int
main()
• Bitwise Operators: These are used to perform bit level operations between two
variables.
*: Pointer of a variable
Ans. If you want to perform increment operation, then use ‘i++,’ which will increase the value
by 1. If you want to perform decrement operation, then use ‘i–,’ it will decrease the value by 1.
Example:
#include<stdio.h> int
main()
}
Also Read>> 5 Best Programming Languages To Learn For Cybersecurity Professionals Q16.
Ans. Global variables are declared outside the function. That variable can be used anywhere in
the program, whereas local variables declared inside the function, and there scope is only
inside that function. // Global variable float x = 1;
void my_test() {
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;
Ans. Dynamic memory allocation is the process of memory allocation at the run time. There are
a group of functions in C used to dynamic memory management i.e. calloc(), malloc(), realloc()
and free().
Ans. The main difference between calloc() and malloc() is that calloc() takes two arguments
while malloc() takes one argument. Secondly, calloc() initializes allocated memory to ZERO
while malloc() does not initialize allocated memory. Syntax of calloc() void *calloc(size_t n,
size_t size);
Syntax of malloc() void
*malloc(size_t n);
#include <stdlib.h>
#include <stdio.h>
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.
Ans. If the number is with a negative sign, then at the time of memory allocation, the number
(ignoring minus sign) is converted into the binary equivalent. Then the two’s complement of
the number is calculated.
Example: #include
<stdio.h> int
main()
int a = -4; int b = -3; unsigned int c = -4; unsigned int d = -3;
Output:
1.333333
1.000000
-0.000000
-1431655764.000000
Q22. What is the use of nested structure in C?
Ans. A nested structure is used to make the complicated code easy. If we want to add the
address of employees with other more details, then we have to create a nested structure for it.
Example:
#include<stdio.h> struct
address
char city[20];
phone[14];
};
struct employee
char name[20];
};
void main ()
{
struct employee emp; printf(“Enter employee information?\n”); scanf(“%s
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
Pincode: 110001
Phone: 123456789
Q23. How to write a program in C for swapping two numbers without the use of the third
variable?
Ans.
#include <stdio.h> int
main()
{ int x,
y;
x = x + y; y = x – y; x = x – y;
return 0;
Ans.
#include<stdio.h> int
main()
{
n3=n1+n2;
printf(” %d”,n3);
n1=n2; n2=n3;
return 0;
Ans. The assignment operator (=): It is a binary operator used to operate two operands. It is
used to assign the value to the variable.
Example: x=(a+b);
y=x;
Equal to operator (==): It is also a binary operator used to compare the left-hand side and
righthand side value, if it is the same, it returns 1 else 0.
y=10; if(x==y)
printf(“True”);
else
printf(“False”);
Ans. The term rvalue refers to objects that appear on the right side, while an Ivalue is an
expression that appears on the left side.
Ans. Yes.
Ans. It is a data structure that is used to store data in a particular order in which operations are
performed. There are two types of storing orders, i.e. LIFO (last in first out) and FIFO (first in
last out).
• Push
• Pop
• Peek or Top
• isEmpty
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)
factorial_of_a_number(int n)
== 0) return 1; else
for(i = 1; i <= n; i+
+)
{ fact = fact *
i;
return fact;
int main()
Ans. The smallest individual unit in a C program is known as a token. Tokens can be classified
as:
• Keywords
• Constants
• Identifiers Strings
• Operators
• Special symbols
Ans.
#include <stdio.h> void
main()
num; i++)
sum = sum + i;
Ans. It is a way to convert constant from one type to another type. If there is a value of float
data type then you can typecast it into other data types.
Example:
Example:
#include <time.h>
#include <stdlib.h>
Also Read>> Top Universities Offering Free Online Courses For Programmers
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.
printNos(unsigned int n)
{ if(n >
0)
printNos(n-1);
printf(“%d “, n);
Ans. FIFO means first in first out. It is a cost flow assumption, which is used to remove costs
from the inventory account.
Q45. What is the name of the function used to close the file stream?
Ans. Fclose().
Ans. A user-defined data type that enables to combine different data types to store a particular
type of record, is known as a structure.
Example: struct
Point
{ int x,
y;
Point
{ int x,
y;
};
int main()
Ans.
#include <stdio.h> int
main()
while(n != 0)
rev*10 + rem; n /=
10;
0;
Ans.
#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
keyword.
Ans.
#include<stdio.h>
#include<conio.h> main()
while(n>0)
r=n%10; sum=sum+
(r*r*r); n=n/10;
if(temp==sum) printf(“armstrong
number “);
else
Ans.
#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) printf(“palindrome
number “);
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
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.
Ans. If a header file is included twice, the compiler will process its contents twice, resulting in
an error. You can use a guard(#) to prevent a header file from being included multiple times
during the compilation process. Thus, even if a header file with proper syntax is included twice,
the second one will get ignored.
Ans. Yes, a C program can be compiled without the main() function. However, it will not
execute without the main() function.
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:
It is done before the program execution. It takes place during program execution.
It uses a stack for managing the static It uses heap for managing the dynamic
allocation of memory. allocation of memory.
Memory size can not be modified while Memory size can be modified while
execution. Example: Array execution. Example: Linked List
Q56. What do you mean by a memory leak in C?
Ans. A memory leak is a kind of resource leak that happens when programmers create a
memory in heap and forget to delete it. Thus, the memory which is no longer needed remains
undeleted. It may also occur when an object is inaccessible by running code but it is still stored
in memory. A memory leak can result in additional memory usage and can affect the
performance of a program.
Ans. In while (1) and while (0), 1 means TRUE or ON and 0 means FALSE or OFF.
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.
Q59. What is the difference between a null pointer and a void pointer?
Ans. A null pointer is the one that does not point to any valid location and its value isn’t known
at the time of declaration.
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.
Ans. The scope of a global symbol can be resolved by using the extern storage, which extends
the visibility or scope of variables and functions. Since C functions are visible throughout the
program by default, you don’t need to use it with function declaration or definition.
Q62. What is the difference between actual parameters and formal parameters?
Ans. The differences between actual parameters and formal parameters are:
They are included at the time of function call. They are included at the time
of the definition of the
function.
Actual Parameters do not require data type but the data Data types need to be
type should match with the corresponding data type of mentioned.
formal parameters.
These can be variables, expressions, and constant without These are variables with their
data types. data type.
Ans. Modular programming is an approach that focuses on dividing an entire program into
independent and interchangeable modules or sub-programs, such as functions and modules for
achieving the desired functionality. It separates the functionality in such a manner that each
sub-program contains everything necessary to execute just one aspect of the desired
functionality.
Ans. As the name suggests, a sequential access file is used to store files sequentially, i.e. one
data is placed into the file after another. It means that if you have to access files you will have
to check each file sequentially (reading one data at a time) until your desired file is reached. It is
more limitations as access time will be very high and storage cost is high
Ans. # pragma is a special purpose directive that is used to turn on or off certain features. Some
of the #pragma directives are:
• #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.
Visit Naukri Learning website for more