C Programming Interview Questions
C Programming Interview Questions
C Programming Interview Questions
Interview Question
Ans :
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.
Ans :
©Topperworld
Q 3. What are basic data types supported in the C Programming
Language?
Ans :
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 3 categories:
Primitive data types: Primitive data types can be further classified into
integer, and floating data types.
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.
©Topperworld
Q 4. What are tokens in C?
Ans : Tokens are identifiers or the smallest single unit in a program that is
meaningful to the compiler. In C we have the following tokens:
©Topperworld
➢ Strings: Strings in C are an array of characters that end with a null
character (‘\0). Null character indicates the end of the string;
➢ Operators: Symbols that trigger an action when they are applied to any
variable or any other object. Unary, Binary, and ternary operators are
used in the C Programming language.
Ans :
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:
) Local Variables: Inside a given function or a block
) Global Variables: Out of all functions globally inside the program.
) Formal Parameters: In-function parameters only.
©Topperworld
Main types of Preprocessor Directives are Macros, File Inclusion, Conditional
Compilation, and Other directives like #undef, #pragma, etc.
Example:
©Topperworld
Output:
Ans :
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.
©Topperworld
Parameter Malloc() Calloc()
Ans :
⚫ Pointers pointing to deallocated memory blocks in C Programming are
known as dangling pointers i.e, whenever a pointer is pointing to a
memory location and In case the variable is deleted and the pointer still
points to that same memory location then it is known as a dangling
pointer variable.
⚫ In C programming memory leak occurs when we allocate memory with
the help of the malloc() or calloc() library function, but we forget to free
the allocated memory with the help of the free() library function. Memory
leak causes the program to use an undefined amount of memory from
the RAM which makes it unavailable for other running programs this
causes our program to crash.
Ans :
©Topperworld
Example:
// 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 :
Ans : 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.
©Topperworld
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.
Q 12. What is the difference between the local and global variables
in C?
Ans :
Local variables are declared inside a block or function but global variables
are declared outside the block or function to be accessed globally.
©Topperworld
Q 13. What are pointers and their uses?
Ans : 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:
➢ To pass arguments by reference
➢ For accessing array elements
➢ To return multiple values
➢ Dynamic memory allocation
➢ To implement data structures
➢ To do system-level programming where memory addresses are useful
©Topperworld
Whether it is an integer variable, function parameter, or structure
declaration, typedef will shorten the name.
Syntax:
Here,
• existing type is already given a name.
• alias name is the new name for the existing variable.
Q 15. What are loops and how can we create an infinite loop in C?
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.
©Topperworld
Another can be wrong or unsatisfied break conditions in the program.
Example :
// Driver code
int main()
{
for (;;) {
printf("Infinite-loop\n");
}
while (1) {
printf("Infinite-loop\n");
}
do {
printf("Infinite-loop\n");
} while (1);
return 0;
}
©Topperworld
Q 16. What is the difference between type casting and type
conversion?
Ans :
Syntax: Syntax:
destination_data_type = int a = 20; float b; b = a; // a =
(target_data_type) 20.0000
©Topperworld
Type Casting Type Conversion
variable_to_be_converted;
Ans : 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.
©Topperworld
Modifying a program becomes easier with the help of function and hence
reduces the chances of error.
There are two types of functions:
Ans :
A macro is a name that is given to a block of C statements as a pre-processor
directive.
Macro is defined with the pre-processor directive. Macros are pre-processed
which means that all the macros would be preprocessed before the
compilation of our program.
However, functions are not preprocessed but compiled.
Macro Function
©Topperworld
Macro Function
slower.
©Topperworld
Q 22. What is a structure?
struct student
{
char name[20];
int roll_no;
char address[20];
char branch[20];
};
Example:
#include <stdio.h>
#include <string.h>
int main()
©Topperworld
{
struct student obj;
strcpy(obj.name, "Kamlesh_Joshi");
obj.roll_no = 27;
strcpy(obj.address, "Haldwani");
strcpy(obj.branch, "Computer Science And
Engineering");
printf("Name: %s\n", obj.name);
printf("Roll_No: %d \n", obj.roll_no);
printf("Address: %s\n", obj.address);
printf("Branch: %s", obj.branch);
return 0;
}
Output :
Name: Kamlesh_Joshi
Roll_No: 27
Address: Haldwani
Branch: Computer Science And Engineering
©Topperworld
Q 23. What is union?
Ans : 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;
};
Ans:
• An “l-value” refers to an object with an identifiable location in
memory (i.e. having an address).
• An “l-value” will appear either on the right or left side of the
assignment operator(=).
• An “r-value” is a data value stored in memory at a given address. An
“r-value” refers to an object without an identifiable location in
memory (i.e. without an address).
©Topperworld
• An “r-value” is an expression that cannot be assigned a value,
therefore it can only exist on the right side of an assignment operator
(=).
Example:
Ans :
©Topperworld
Q 26. What is the sleep() function?
Ans :
• 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.
Example:
©Topperworld
Output
2
Ans :
#include <stdio.h>
void Fibonacci(int num, int first, int second, int
third)
{
if (num > 0) {
third = first + second;
first = second;
second = third;
printf("%d ", third);
©Topperworld
Fibonacci(num - 1, first, second, third)
}
}
int main()
{
int num;
printf("Please Enter number of Elements: ");
scanf("%d", &num);
printf(
"Fibonacci Series with the help of
Recursion:\n");
printf("%d %d ", 0, 1);
Fibonacci(num - 2, 0, 1, 0);
printf("\nFibonacci Series without Using
Recursion:\n");
int first = 0, second = 1, third = 0;
printf("%d %d ", 0, 1);
for (int i = 2; i < num; i++) {
third = first + second;
first = second;
second = third;
}
return 0;
}
©Topperworld
Output:
Example:
#include <math.h>
#include <stdio.h>
int main()
{
int num;
int check = 1;
printf("Enter a number: \n");
scanf("%d", &num);
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
check = 0;
break;
}
}
©Topperworld
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;
}
Ans :
Source code can be easily modified and Object code cannot be modified
contains less number of statements and contains more statements
than object code. than source code.
Source code can be changed over time Object code can be modified and is
©Topperworld
Source Code Object Code
Source code is less close to the Source code is more close to the
machine and is input to the compiler or machine and is the output of the
any other translator. compiler or any other translator.
Ans :
➢ Static memory allocation: Memory allocation which is done at compile
time is known as static memory allocation.
Static memory allocation saves running time. It is faster than dynamic
memory allocation as memory allocation is done from the stack.
This memory allocation method is less efficient as compared to dynamic
memory allocation. It is mostly preferred in the array.
©Topperworld
Q 33. What is pass-by-reference in functions?
Ans :
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.
Ans :
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:
int main(int argc, char *argv[]){/*code which
is to be executed*/}
©Topperworld
Q 36. What is an auto keyword?
Ans :
Every local variable of a function is known as an automatic variable in the C
language.
Auto is the default storage class for all the variables which are declared
inside a function or a block.
Auto variables can only be accessed within the block/function they have
been declared. We can use them outside their scope with the help of pointers.
By default auto keywords consist of a garbage value.
Ans :
// C program to print hello-world
// without using semicolon
#include <stdio.h>
// Driver code
int main()
{
if (printf(“Hello - World”)) {
}
return 0;
}
©Topperworld
Q 38. Write a C program to swap two numbers without using a
third variable.
Ans :
#include <stdio.h>
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;
}
©Topperworld
Output
Ans :
#include <stdio.h>
#include <string.h>
void Palindrome(char s[])
{
int start = 0;
int end = strlen(s) - 1;
while (end > start) {
if (s[start++] != s[end--]) {
printf("%s is not a Palindrome \n", s);
return;
}
}
printf("%s is a Palindrome \n", s);
}
int main()
{
Palindrome("abba");
return 0;
}
©Topperworld
Output
abba is a Palindrome
Ans :
Modifiers are keywords that are used to change the meaning of basic data
types in C language.
They specify the amount of memory that is to be allocated to the variable.
There are five data type modifiers in the C programming language:
⚫ long
⚫ short
⚫ signed
⚫ unsigned
⚫ long long
©Topperworld
Q 41. Write a program to print the factorial of a given number with
the help of recursion.
Ans :
#include <stdio.h>
int main()
{
int num = 5;
printf("Factorial of %d is %d", num,
factorial(num));
return 0;
}
Output :
Factorial of 5 is 120
©Topperworld
Q 42. Write a program to check an Armstrong number.
Ans :
#include <stdio.h>
int main()
{
int n;
printf("Enter Number \n");
scanf("%d", &n);
int var = n;
int sum = 0;
while (n > 0) {
int rem = n % 10;
sum = (sum) + (rem * rem * rem);
n = n / 10;
}
if (var == sum) {
printf("%d is an Armstrong number \n", var);
}
else {
printf("%d is not an Armstrong number", var);
}
return 0;
}
©Topperworld
Output :
Enter Number
0 is an Armstrong number
// Driver code
int main()
{
int n, rev = 0;
printf("Enter Number to be reversed : ");
scanf("%d", &n);
int r = 0;
while (n != 0)
{
r = n % 10;
rev = rev * 10 + r;
n /= 10;
}
©Topperworld
Output:
Ans :
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.
Ans :
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.
©Topperworld
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().
• %c: Character format specifier used to display and scan character.
• %d, %i: Signed Integer format specifier used to print or scan an integer
value.
• %f, %e, or %E: Floating-point format specifiers are used for printing or
scanning float values.
• %s: This format specifier is used for String printing.
• %p: This format specifier is used for Address Printing.
Ans :
➢ 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.
Ans :
In C programming Basic File Handling Techniques provide the basic
functionalities that programmers can perform against the system.
©Topperworld
C file operations refer to the different possible operations that we can
perform on a file in C such as:
1. Creating a new file – fopen() with attributes as “a” or “a+” or
“w” or “w+”
2. Opening an existing file – fopen()
3. Reading from file – fscanf() or fgets()
4. Writing to a file – fprintf() or fputs()
5. Moving to a specific location in a file – fseek(), rewind()
6. Closing a file – fclose()
Ans :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int isCircular(struct Node* head)
{
©Topperworld
while (ptr != NULL && ptr != head) {
ptr = ptr->next;
}
return (ptr == head);
}
struct Node* newnode(int data)
{
struct Node* first;
first = (struct Node*)malloc(sizeof(struct Node));
first->data = data;
first->next = NULL;
return first;
}
int main()
{
struct Node* head = newnode(10);
head->next = newnode(12);
head->next->next = newnode(14);
head->next->next->next = newnode(16);
head->next->next->next->next = head;
if (isCircular(head)) {
printf("Linked List is Circular\n");
}
else {
printf("Linked List is Not Circular\n");
}
return 0;
}
©Topperworld
Q 49 . Write a program to Merge two sorted linked lists.
Ans :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* mergeSortedLists(struct Node* a, struct Node* b) {
struct Node dummy = {0, NULL}, *tail = &dummy;
while (a && b)
if (a->data <= b->data) tail = tail->next = a, a = a->next;
else tail = tail->next = b, b = b->next;
tail->next = a ? a : b;
return dummy.next;
}
void printList(struct Node* head) {
while (head) printf("%d -> ", head->data), head = head->next;
printf("NULL\n");
}
int main() {
struct Node *list1, *list2;
struct Node* mergedList = mergeSortedLists(list1, list2);
printList(mergedList);
return 0;
}
©Topperworld
50. What is the difference between getc(), getchar(), getch() and
getche().
Ans :
⚫ 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.
ABOUT US
➢ Our Vision
❖ Our vision is to create a world where every college student can easily
access high-quality educational content, connect with peers, and achieve
their academic goals.
❖ We believe that education should be accessible, affordable, and engaging,
and that's exactly what we strive to offer through our platform.
©Topperworld
➢ Unleash Your Potential
❖ Education is not just about textbooks and lectures; it's also about forming
connections and growing together.
❖ TopperWorld encourages you to engage with your fellow students, ask
questions, and share your knowledge.
❖ We believe that collaborative learning is the key to academic success.
©Topperworld
“Unlock Your
Potential”
With- Topper World
Explore More
topperworld.in
Follow Us On
E-mail
topperworld.in@gmail.com