C Interview Questions and Answers (2023)
C Interview Questions and Answers (2023)
1. For Freshers
2. For Experienced
In this article, you will get the frequently and most asked C programming interview
questions and answers at the fresher and experienced levels. So, let us start with
Questions for freshers.
Due to its ability to support both low-level and high-level features, C is considered a
middle-level language. It is both an assembly-level language, i.e. a low-level language,
and a higher-level language. Programs that are written in C are converted into
assembly code, and they support pointer arithmetic (low-level) while being machine-
independent (high-level). Therefore, C is often referred to as a middle-level language. C
can be used to write operating systems and menu-driven consumer billing systems.
Each variable in C has an associated data type. Each data type requires different
amounts of memory and has some specific operations which can be performed over it.
It specifies the type of data that the variable can store like integer, character, floating,
double, etc. In C data types are broadly classified into 4 categories:
Primitive data types: Primitive data types can be further classified into integer, and
floating data types.
Void Types: Void data types come under primitive data types. Void data types
provide no result to their caller and have no value associated with them.
User Defined data types: These data types are defined by the user to make the
program more readable.
:
Derived data types: Data types that are derived from primitive or built-in data types.
Data Types in C
Tokens are identifiers or the smallest single unit in a program that is meaningful to the
compiler. In C we have the following tokens:
Scope in a programming language is the block or a region where a defined variable will
have its existence and beyond that region, the variable is automatically destroyed.
Every variable has its defined scope. In simple terms, the scope of a variable is equal
to its life in the program. The variable can be declared in three places These are:
Static variables in the C programming language are used to preserve the data values
between function calls even after they are out of their scope. Static variables preserve
their values in their scope and they can be used again in the program without initializing
again. Static variables have an initial value assigned to 0 without initialization.
:
C
// C program to print initial
// value of static variable
#include <stdio.h>
int main()
{
static int var;
int x;
printf("Initial value of static variable %d\n", var);
printf("Initial value of variable without static %d",
x);
return 0;
}
Output
calloc() and malloc() library functions are used to allocate dynamic memory. Dynamic
memory is the memory that is allocated during the runtime of the program from the
heap segment. “stdlib.h” is the header file that is used to facilitate dynamic memory
allocation in the C Programming language.
For more information, refer to the article – Dynamic Memory Allocation in C using
malloc(), calloc(), free(), and realloc()
// Driver code
int main()
{
char res[20];
float a = 32.23;
sprintf(res, "%f", a);
printf("\nThe string for the num is %s", res);
return 0;
}
Output
Recursion is the process of making the function call itself directly or indirectly. A
recursive function solves a particular problem by calling a copy of itself and solving
smaller subproblems that sum up the original problems. Recursion helps to reduce the
length of code and make it more understandable. The recursive function uses a LIFO (
Last In First Out ) structure like a stack. Every recursive call in the program requires
extra space in the stack memory.
12. What is the difference between the local and global variables in
C?
Local variables are declared inside a block or function but global variables are declared
:
outside the block or function to be accessed globally.
Pointers are used to store the address of the variable or a memory location. Pointer can
also be used to refer to another pointer function. The main purpose of the pointer is to
save memory space and increase execution time. Uses of pointers are:
Syntax:
Here,
Example:
:
typedef long long ll
15. What are loops and how can we create an infinite loop in C?
Loops are used to execute a block of statements repeatedly. The statement which is to
be repeated will be executed n times inside the loop until the given condition is reached.
There are two types of loops Entry controlled and Exit-controlled loops in the C
programming language. An Infinite loop is a piece of code that lacks a functional exit.
So, it repeats indefinitely. There can be only two things when there is an infinite loop in
the program. One it was designed to loop endlessly until the condition is met within the
loop. Another can be wrong or unsatisfied break conditions in the program.
Types of Loops
// Driver code
int main()
{
for (;;) {
printf("Infinite-loop\n");
}
while (1) {
printf("Infinite-loop\n");
}
do {
printf("Infinite-loop\n");
} while (1);
return 0;
}
16. What is the difference between type casting and type conversion?
The data type is converted to another data type by The data type is converted to
a programmer with the help of a casting operator. another data by a compiler.
In Type casting in order to cast the data type into In type conversion, there is no
another data type, a caste operator is needed need for a casting operator.
:
Type conversion is less efficient
Type casting is more efficient and reliable. and less reliable than type
casting.
Type casting takes place during the program design Type conversion is done at
by the programmer. compile time.
Syntax:
Syntax:
int a = 20; float b; b = a; // a =
destination_data_type = (target_data_type)
20.0000
variable_to_be_converted;
For more information, refer to the article – Type Casting and Type Conversion.
The function is a block of code that is used to perform a task multiple times rather than
writing it out multiple times in our program. Functions avoid repetition of code and
increase the readability of the program. Modifying a program becomes easier with the
help of function and hence reduces the chances of error. There are two types of
:
functions:
User-defined Functions: Functions that are defined by the user to reduce the
complexity of big programs. They are built only to satisfy the condition in which the
user is facing issues and are commonly known as “tailor-made functions”.
Built-in Functions: Library functions are provided by the compiler package and
consist of special functions with special and different meanings. These functions give
programmers an edge as we can directly use them without defining them.
Macro Function
The macro name is replaced by the macro Transfer of control takes place during
value before compilation. the function call.
Macro doesn’t check any Compile-Time Errors. Function check Compile-time errors.
The structure is a keyword that is used to create user-defined data types. The structure
allows storing multiple types of data in a single unit. The structure members can only be
accessed through the structure variable.
Example:
struct student
{
char name[20];
int roll_no;
char address[20];
char branch[20];
};
:
Below is the C program to implement structure:
C
// C Program to show the
// use of structure
#include <stdio.h>
#include <string.h>
// Driver code
int main()
{
struct student obj;
strcpy(obj.name, "Kamlesh_Joshi");
obj.roll_no = 27;
strcpy(obj.address, "Haldwani");
strcpy(obj.branch, "Computer Science And Engineering");
return 0;
}
Output
Name: Kamlesh_Joshi
:
Roll_No: 27
Address: Haldwani
A union is a user-defined data type that allows users to store multiple types of data in a
single unit. However, a union does not occupy the sum of the memory of all members. It
holds the memory of the largest member only. Since the union allocates one common
space for all the members we can access only a single variable at a time. The union
can be useful in many situations where we want to use the same memory for two or
more members.
Syntax:
union name_of_union
{
data_type name;
data_type name;
};
Dummy variables copy the value of each variable Dummy variables copy the address
in the function call. of actual variables.
A simple technique is used to pass the values of The address values of variables
variables. must be stored in pointer variables.
For more information, refer to the article – Call by Value and Call by Reference
sleep() function in C allows the users to wait for a current thread for a given amount of
time. sleep() function will sleep the present executable for the given amount of time by
the thread but other operations of the CPU will function properly. sleep() function
returns 0 if the requested time has elapsed.
In C, enumerations (or enums) are user-defined data types. Enumerations allow integral
constants to be named, which makes a program easier to read and maintain. For
example, the days of the week can be defined as an enumeration and can be used
anywhere in the program.
C
// An example program to demonstrate working
// of enum in C
#include <stdio.h>
int main()
{
enum week day;
day = Wed;
printf("%d", day);
return 0;
}
Output
In the above example, we declared “day” as the variable, and the value of “Wed” is
allocated to day, which is 2. So as a result, 2 is printed.
Volatile keyword is used to prevent the compiler from optimization because their values
can’t be changed by code that is outside the scope of current code at any time. The
:
System always reads the current value of a volatile object from the memory location
rather than keeping its value in a temporary register at the point it is requested, even if
previous instruction is asked for the value from the same object.
Fibonacci Numbers
C
// C program to print Fibonacci Series
// with recursion and without recursion
#include <stdio.h>
// Driver code
int main()
{
int num;
printf(
"Fibonacci Series with the help of Recursion:\n");
Fibonacci(num - 2, 0, 1, 0);
first = second;
second = third;
}
return 0;
}
Output:
0 1 1 2 3
0 1 1 2 3
// Driver code
int main()
{
int num;
int check = 1;
if (num <= 1) {
check = 0;
}
if (check == 1) {
printf("%d is a prime number", num);
}
else {
printf("%d is not a prime number", num);
}
:
return 0;
}
Source code can be easily modified and Object code cannot be modified and
contains less number of statements than contains more statements than source
object code. code.
Source code can be changed over time and is Object code can be modified and is
not system specific. system specific.
For more information, refer to the article – Static and Dynamic Memory Allocation in C
Pass by reference allows a function to modify a variable without making a copy of the
variable. The Memory location of the passed variable and parameter is the same, so
any changes done to the parameter will be reflected by the variables as well.
:
C
// C program to change a variable
// using pass by reference
#include <stdio.h>
// Driver code
int main()
{
int num = 20;
printf("Value of num before passing is: %d\n", num);
return 0;
}
Whenever a variable is defined some amount of memory is created in the heap. If the
programmer forgets to delete the memory. This undeleted memory in the heap is called
a memory leak. The Performance of the program is reduced since the amount of
available memory was reduced. To avoid memory leaks, memory allocated on the heap
should always be cleared when it is no longer needed.
:
For more information, refer to the article – Memory Leak
Arguments that are passed to the main() function of the program in the command-line
shell of the operating system are known as command-line arguments.
Syntax:
// Driver code
int main()
{
// This will print the hello-world
// on the screen without giving any error
if (printf(“Hello - World”)) {
}
return 0;
}
int main()
{
// Variable declaration
int var1 = 50;
int var2 = 60;
printf(
"Values before swap are var1 = %d and var2 = %d\n",
var1, var2);
return 0;
}
Output
// Driver code
int main()
{
Palindrome("abba");
return 0;
}
Output
abba is a Palindrome
long
short
signed
unsigned
long long
Factorial of a Number
:
C
// C program to find factorial
// of a given number
#include <stdio.h>
// Driver code
int main()
{
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
Output
Factorial of 5 is 120
// Driver code
int main()
{
int n;
int var = n;
int sum = 0;
Enter Number
0 is an Armstrong number
C
// C program to reverse digits
// of a number
#include <stdio.h>
// Driver code
int main()
{
int n, rev = 0;
return 0;
}
Output:
:
Enter Number to be reversed :
The extern keyword is used to extend the visibility of the C variables and functions in
the C language. Extern is the short name for external. It is used when a particular file
needs to access a variable from any other file. Extern keyword increases the
redundancy and variables with extern keyword are only declared not defined. By default
functions are visible throughout the program, so there is no need to declare or define
extern functions.
printf() function is used to print the value which is passed as the parameter to it on the
console screen.
Syntax:
print(“%X”,variable_of_X_type);
scanf() method, reads the values from the console as per the data type specified.
Syntax:
scanf(“%X”,&variable_of_X_type);
In C format specifiers are used to tell the compiler what type of data will be present in
the variable during input using scanf() or output using print().
Near Pointers: Near pointers are used to store 16-bit addresses only. Using the near
pointer, we can not store the address with a size greater than 16 bits.
Far Pointers: A far pointer is a pointer of 32 bits size. However, information outside
the computer’s memory from the current segment can also be accessed.
Huge Pointers: Huge pointer is typically considered a pointer of 32 bits size. But bits
located outside or stored outside the segments can also be accessed.
In C programming Basic File Handling Techniques provide the basic functionalities that
programmers can perform against the system.
:
File Operations in C
C
// C program to check if the linked list is circular
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int isCircular(struct Node* head)
{
int main()
{
C
// C program to merge two sorted
// linked lists
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
/* UTILITY FUNCTIONS */
/* MoveNode() function takes the node
from the front of the source, and
move it to the front of the dest.
It is an error to call this with the
source list empty.
push(&b, 20);
push(&b, 3);
push(&b, 2);
return 0;
}
Output
2 3 5 10 15 20
For more information, refer to the article – Merge Two Sorted Linked List
getc(): The function reads a single character from an input stream and returns an
integer value (typically the ASCII value of the character) if it succeeds. On failure, it
returns the EOF.
getchar(): Unlike getc(), gechar() can read from standard input; it is equivalent to
getc(stdin).
getch(): It is a nonstandard function and is present in ‘conio.h’ header file which is
mostly used by MS-DOS compilers like Turbo C.
getche(): It reads a single character from the keyboard and displays it immediately
on the output screen without waiting for enter key.
:
For more information, refer to the article – Difference between getc(), getchar(), getch(),
getche()
Recommended Articles
1. Top 50 C Coding Interview Questions and Answers (2023)
2. Commonly Asked C Programming Interview Questions | Set 1
3. Commonly Asked C Programming Interview Questions | Set 2
4. Commonly Asked C Programming Interview Questions | Set 3
5. Top 10 Programming Languages for Blockchain Development
6. Getting System and Process Information Using C Programming and Shell in Linux
7. Structured Programming Approach with Advantages and Disadvantages
8. Fork() - Practice questions
9. A C Programming Language Puzzle
10. Sort an array using socket programming in C
11. AKTU (UPTU) Previous Year Solved Papers | C Programming
12. C++: Methods of code shortening in competitive programming
13. Working of Keyword long in C programming
14. Hello World Program : First program while learning Programming
15. How to create GUI in C programming using GTK Toolkit
16. Introduction to the C99 Programming Language : Part I
17. Introduction to the C99 Programming Language : Part II
18. Introduction to the C99 Programming Language : Part III
19. Features of C Programming Language
20. Role of SemiColon in various Programming Languages
21. Draw a Chess Board using Graphics Programming in C
22. What are the C programming concepts used as Data Structures
23. Brief Overview & Comparison of Object-Oriented Programming from C to Java
24. Handling multiple clients on server with multithreading using Socket Programming in
C/C++
:
25. Short-circuit evaluation in Programming
feedback@geeksforgeeks.org
Company
About Us
Legal
Careers
In Media
Contact Us
Advertise with us
Explore
:
Job-A-Thon Hiring Challenge
Hack-A-Thon
DSA in JAVA/C++
Master CP
GeeksforGeeks Videos
Languages
Python
Java
C++
PHP
GoLang
SQL
R Language
Android Tutorial
DSA Concepts
Data Structures
Arrays
Strings
Linked List
Algorithms
Searching
Sorting
Mathematical
Dynamic Programming
:
Dynamic Programming
DSA Roadmaps
DSA for Beginners
Web Development
HTML
CSS
JavaScript
Bootstrap
ReactJS
AngularJS
NodeJS
Express.js
Lodash
Web Design
Computer Science
GATE CS Notes
Operating Systems
Computer Network
Software Engineering
Engineering Maths
:
Python
Django Tutorial
Python Projects
Python Tkinter
Pandas Tutorial
NumPy Tutorial
NLP Tutorial
DevOps
Git
AWS
Docker
Kubernetes
Azure
GCP
Competitive Programming
Top 50 DP Problems
System Design
Scalability in SD
Databases in SD
Interview Corner
Company Wise Preparation
Experienced Interviews
Internship Interviews
Competitive Programming
Aptitude Preparation
Puzzles
GfG School
English Grammar
Commerce
Accountancy
Business Studies
Economics
Management
Income Tax
Finance
UPSC
Polity Notes
Geography Notes
History Notes
Economics Notes
SSC/ BANKING
SSC CGL Syllabus
SBI PO Syllabus
IBPS PO Syllabus
Improve an Article
Internships
Open InSanchhaya
@GeeksforGeeks, App Education Private Limited, All rights reserved
: