Useful Interview Questions in C
Useful Interview Questions in C
C Programming
About Us
Language Interview Questions and Answers
2023
HackerTrail
January 23, 2023
To make any programming language interview successful, you must strengthen your foundational programming
knowledge. C language is that foundation, and you must enhance this foundation to ace any coding interview.
Dennis Ritchie developed this language for designing operating systems.
C is a high-level structured programming language that helps you learn other programming languages. That's
why it is still prevalent during the interview process.
In this article, we will focus on the top commonly asked C Programming Interview Questions into the below
sections:
Advanced C Programming Language Interview Questions
Interview
1. Write the code Prep asequence
to design Practiceof
Skills Jobs Products
numbers/operators every
in which Services
row increases Sign
the object by in
one. Sign up
About Us
2. What are the top 5 applications of C programming language?
C language is a general-purpose language which is why it is used in many applications like gaming, database
designing, servers, etc. Still, the top 5 applications are as follows:
Designing and developing operating systems – Operating system is a program which manages other
application programs in computers. Unix, Microsoft Windows, and some android applications are all
outcomes of C language.
Designing Graphical User Interface (GUI) to establish interaction between the machine and human. The best
example is Adobe Photoshop editors.
Building compiler – a compiler has the main job of compiling the high-level language and making it
executable for the machines. C language helps to build compilers so you can use one program on different
platforms.
Browser development – Without browsers, you can't access the internet. Credit goes to C language,
which helps to design different browsers like Mozilla Firefox!
Designing embedded system – An embedded system is an integration of hardware and software for
implementing a particular function. C language makes it easy to create embedded systems.
Library - “studio.h”
Library - “Math.h”
= operator == operator
The operator "=" is the assignment The operator "==" is a comparison operator used for
operator used for assigning values. comparing two values.
You can assign any constant at the LHS of You can use constant values at both sides of this
this operator. operator.
{ {
int x; int a = 1; b = 2;
} }
It is an object identifier that defines memory location It is an expression that defines data value
About Us
to identify the object. which would be stored at some memory
lvalue address. rvalue
It could be defined as either the LHS or RHS of the (=) It should be defined at the RHS of the (=)
operator. When it is on the LHS, it holds data value. Like operator.
int a; a = 1;
Like
But it is on the RHS, it meant modifiable l-values like
expression.
int d;
int a, b;
d = 2; //d has r-value 2.
b = a;
10. How does const char*p differ from the char const* p
No, there is no difference between (const char* p) and (char const* p). Both refer to a constant pointer to a
constant character. It means you can't change the values in either case.
Take this quiz to see how much you know about C programming!
Syntax:
return_type function_name( datatype argument1, datatype argument 2, …);
Example
int sum(int x, int y);
This line is declaring the function "sum" which is taking two integer arguments. The function will also return the
integer.
You have to use the asterisk symbol (*) to declare the function pointer and function parameters along with
return type, and function arguments like this:
Let's understand it with this dummy code: printing without using stdio.h and printf function
int demo(int, int); //function #include <unistd.h> // Include the necessary header
for the write function
int main() {
int (*demp_ptr) (int, int); char message[] = "Hello, world!\n";
ssize_t bytes_written = write(1, message, sizeof
(message) - 1);
// Note: sizeof(message) includes the null
demo_ptr = demo // assigning the function address to function pointer terminator, so we subtract 1.
if (bytes_written == -1) {
// An error occurred during the write operation
//you can call the function as per requirement // Handle the error appropriately
return 1;
}
return 0;
}
You can't run any code without including header files. You have to use the preprocessor directive "#include" for
this. Like this,
#include
It directs the compiler to use necessary header files before compilation. For example, #include directs the
compiler to use the available functions in the file, studio.h header file.
The studio.h is a common header file in every C program code to perform input/output operations. It consists of
built-in functions including printf(), scanf(), getch(), etc.
Standard library and user-defined are two types of header files available in C programming.
Interview
16. How can you implement Practice Skillsprocesses
Prep decision-making Jobs Products
in C language? Services Sign in Sign up
The decision-making process in C programming language enables flow direction based on the conditions and
assists in making decisions. Users need to decide according to certain conditions. For example, the government
About Us
can give a driving licence if a person is 18+. To solve such problems, you need to use decision-making
processes.
Options Feature
If-else statement You can use it to check conditions and execute the required codes with
alternative options.
Nested-if Sometimes, you need to check conditions within conditions. And in that case,
statement use this option.
If-else-if ladder Use it if you have to check multiple conditions and get an alternative last option
if none of them gets true.
Switch This is also an option to implement decisions by providing all essential options.
Check the following image to learn every option through flow charts!
Section Use
name
Function It should be identical to the function declaration except for the semicolon. You
Header must define the function name, data type, arguments, and return type.
Function body It defines the set of executable statements whenever the function is called.
Function name Use the same function name as declared in the function header.
Function What
Interview Prep would be the
Practice return Jobs
Skills valueof the function
Products Services Sign in Sign up
Return type
About Us
Check the following example to understand the function definition and its sections.
Modifier Usage
short Use it when you to store small values of data type – int
long Use it when you have to increase the size of int/ double data types.
unsigned Supporting datatype is int and char but you can use it only for positive values.
unsigned int 4 %u
int 4 %u
char 1 %c
About Us
unsigned char 1 %c
float 4 %f
double 8 %lf
19. What is an array, and why does it play a major role in C programming?
Array refers to a collection of values which possess a similar data type. For example, if "age" is an array, only
age values (integer) will be stored in this array. An array doesn't permit the storage of different types of values.
An array has the following special characteristics:
Store data in contiguous memory locations
Indexing starts from zero
You can access array elements using their position
In C programming, mention array size while using array declaration to avoid errors. You get the following
benefits when you use arrays in your C programs:
You can store similar values in one place and use a variable to represent them.
You can easily access to an array of elements through their location without considering their position
You don't need to think about memory allocation because of continuous memory storage
You can utilise other data structures like stack, queue, tree, graph, etc., with the help of an array
You can create multiple arrays per requirements and apply functions to optimise codes.
float b = 1.0;
Advanced C Programming Language Interview Questions
Interview Prep Practice Skills Jobs Products Services Sign in Sign up
21. Write the code to design a sequence of numbers/operators in which every row increases the object by one.
In simple wordsAbout
– Pyramid
Us pattern.
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Pyramid Design\n");
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows: 5
Pyramid Design
*
* *
* * *
* * * *
* * * * *
In the following dummy code, you can see that a function, fact() is a recursive calling itself. And the main()
function has declared it.
fact();
.
.
}
void main() // main function to define and declare the recursive function
{
.
.
fact ();
.
.
}
It is good to use this concept to write the codes for the task, which could be divided into sub-tasks. For
example, the tasks like sorting, searching and traversing where you have to visit the list to find the specific
number or location.
Tower of Hanoi, factorial, and Fibonacci are some evergreen problems where you can use recursion.
#include<stdio.h>
#define demo main
int demo(void)
{
printf(“you can execute a program without main() function”);
return 0;
}
Here, #define preprocessor directive is defining macro, which is replaced by the value of the macro.
26. What is the difference between macros and functions C programming language?
Macro Function
Macro is a small chunk of code which gets replaced A function is a group of one or more
whenever respective macros have been called. statements to perform a specific job.
It is called aInterview
preprocessor
Prep directive,
Practice and
Skillsit requires
Jobs It could
Products be predefined
Services or user-defined,
Sign in Sign up
pre-processing. and it needs compilation.
About Us
It does not require any type-checking. It requires type-checking.
The increased number of macros increases the size of The function length does not impact the
the whole program. size of the program.
Macro execution is faster than function execution. The function execution is slower than the
macro execution.
Use macro when you need to execute small codes Use functions when executing large codes
multiple times. and different tasks in a program.
27. Write a code to swap two numbers without using a third number.
#include <stdio.h>
int main()
{
int a=162, b=243;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
Output:
28. Without using multiplication operators, how can you multiply an integer number by 2?
You can multiply or divide any integer number by 2 using bitwise operators. To perform multiplication without
using * operator, you must use the left shift operator (<<) that shifts the bits left by 1.
Check the following code to understand how it works
#include <stdio.h>
Interview Prep Practice Skills Jobs Products Services Sign in Sign up
int main()
{
About Us
int a;
printf("Enter any value for a:");
scanf("%d",&a);
printf("Value of a=%d",a);
Output:
29. What are the different file opening modes in C programming language?
C language allows to open files in the following modes:
Mode Functionality
#include <stdlib.h>
Interview Prep Practice Skills Jobs Products Services Sign in Sign up
#include <stdio.h>
About Us
enum {false, true};
int main()
{
int i = 11;
do
{
printf("Value of i = %d\n", i);
i++;
if (i < 5)
continue;
}
while (false);
getchar();
return 0;
}
Output:
Value of i = 11
The above code will print the value of variable ‘I’. Since here do-while loop has been used, which executes at
every iteration. While the loop will execute after the execution of the continue statement, since here the
condition is false, thus it will print the value of ‘I’ only once.
Prepare for your virtual technical interview by clicking here.
Integer(int)
It is used to store whole numbers, may have below types depending on their sign and size:
int/signed int int/signed int: Usually 4 bytes
unsigned int unsigned int: Usually 4 bytes
int count = 5; short int/signed short int: Usually 2 bytes
short int/ signed short int int index = 0; unsigned short int: Usually 2 bytes
unsigned short int long int/signed long int: Usually 4/8 bytes 32/64 bit
unsigned long int: Usually 4/8 bytes 32/64 bit
long int/ signed long int
Unsigned long int
Floating Point(float)
They are used to store real numbers(with decimal points). Floating point data types in C have below types
based on size:
pi = 3.14159
float 4
sqrt_2 = 1.41421356
8
double 12 or 16 bytes
long double
Character(char)
They are used to store character values and can have below types based on sign:
char/signed char
Interview Prep
unsigned char Practice Skills Jobs Products Services Sign in Sign up
Void
About Us to depict “No value” when a function returns nothing to the calling function, it is
The void data type is used
specified using Void.
32. What is the range of value that an ‘int’ data type variable can hold?
In the below example we have created an enum data type ‘week’ which consists of integral constants - Sunday,
Monday, Tuesday, etc.
In the main program, we have created a variable called ‘today’ of enum data type ‘week’. This ‘today’ variable
may store any one of the integral constants from the enum data type ’week’.
About Us
strcat(string_destination, Joins both the strings and stores the output in
string_source) string_destination.
generally we do typecasting as per the left hand side variables data type
Type conversion can be performed in two ways:
Example: It is performed by the compiler during the execution of an expression where two or more data types
are present. The small data types are converted into large data types so that data is preserved without any
loss.
Example: In the below example, we have deliberately changed the type of ‘c’ from float to int by writing the
expected data type in brackets before the variable name ’c’.
36. What is the difference between local and global variables in C language?
About Us
When uninitialised, they store any garbage When uninitialised, they store the default value as
value. zero.
To pass the value of local variables from one Parameter passing is not required, because the
function to another, parameter passing is scope is throughout the program, in all functions.
required.
If any changes are made to a local variable If any changes are made to a global variable the
the changes are not reflected in other changes are reflected in all functions, at all places in
functions. the program.
37. What is the difference between an actual parameter and a formal parameter in C language?
Formal Parameters used in fun def Actual Parameters used in func call
These parameters are a list of variables. Here we It is not a definition of parameters, here we
define the data type of parameters which will be provide the actual variables(parameters).
accepted by that function.
They are mentioned in the function definition. They are mentioned during the actual
function call.
The data type of the formal parameters is Data types are not mentioned during making
mentioned in the function definition. a call through actual parameters.
Example:
Example:
Number 6 in binary is 0110
Number 6’s one’s complement is 1001
Add one to this 1001+1 = 1010 which is -6.
About Us
It is a collection of elements stored in memory It stores the address of a variable.
continuously.
The memory allocation is static, once allocated The memory allocation is dynamic, the
cannot be changed in size or released. allocated memory can be resized or released.
40. What is the difference between call by value and call by reference in C language?
During function call when we pass variables by During function call when we pass the address of
copying their values it is known as call by the variables it is known as call by reference.
value.
If any changes are made to the values they are If any changes are made they are reflected in the
not reflected in the actual variables. actual variables because addresses were passed.
String Array
It is a sequence of characters, whose end is It is a data structure, whose elements are of the
denoted by NULL characters ('\0'). same data type and accessed via index.
String size can be changed if it is a char The size of the array cannot be changed later.
pointer.
It can contain characters only. It can contain characters, integers, floats, etc.
We can use itoa() function to convert an integer data type into a string data type.
1. Prototype:
2.
3. char *
Interview Prep char
itoa ( int value,
Practice Skills Jobs
* str, int base ); Products Services Sign in Sign up
4.
5. Where :
6.
7. value isAbout Us
the integerto be converted to a string
8.
9. str is the resulting char array
10.
11. base is the numerical base of the integer value
12.
13. Returns a pointer to the char string.
Similarly, we can use atoi() function to convert a string data type into an integer data type.
1. Prototype:
2.
3. int atoi (const char * str);
4.
5. Where
6.
7. str is a string constant which is received
8.
9. Returns the integer if the string is successfully converted, otherwise returns zero.
strcat() strncat()
Concatenates destination string at Concatenates destination string at the end of the source
the end of the source string. string up to the number_of_characters mentioned.
Find code snippets describing the differences between strncat() and strcat() below:
1. strcat(source, destination);
1. Example:
2.
3. #include<stdio.h>
4. #include<string.h>
5. #include<ctype.h>
6. int main()
7. {
8. char src[]="Bombay";
9. char city[]="+Nagpur";
10. printf("Before concatenation destiona string = %s\n", src );
11. strcat(src, city);
12. printf("After concatenation destination string = %s", src );
13. }
2. Relational
To perform relational operations between operands.
Example:
3. Logical
To perform logical operations between operands.
Example:
4. Bitwise
To perform bit-by-bit operations on bits.
Example:
Interview Prep Practice Skills Jobs Products Services Sign in Sign up
5. Assignment
Us
To assign valueAbout
to a variable.
Example:
6. Special
These are special or miscellaneous operators.
Example:
46. What is the difference between usage of the increment operator as ‘++i’ and ‘i++’ in C language?
If we are using the operator ++ only with the operand and without assigning the value anywhere then in such
cases, there is no difference in the usage and value of the operand.
Example 1
1. i=5;
2.
3. i++;
4.
5. printf(“%d”, i);
6.
7. Output is : 6
Example 2
1. i=5;
2.
3. ++i;
4.
5. printf(“%d”, i);
6.
7. Output again is: 6
The difference arises when we assign the operand into another variable after using the increment operator. This
happens because operator precedence works on the expression and provides different output.
Example 3
1. i=5;
2.
3. j=i++;
4.
5. printf(“%d”, i);
Now, in this case, due to the fact that ‘=’ has higher precedence than postfix ‘++’, first the assignment is
executed. So j is assigned the value of i, i.e. 5. After this assignment ‘++’ is executed, so the value of i is now
6.
Example 4
1. i=5;
2.
3. j=++i;
4.
5. printf(“%d”, i);
Now, in this case, due to the fact that the prefix ‘++’ has higher precedence than ‘=’, first the ‘++’ is executed.
So i is incremented first and the value of i is 6. Now the next operation of assignment is executed, j is assigned
the value of i , i.e. 6.
^ Binary XOR Operator copies the bit if it is set in one operand (A ^ B) = 49, i.e.,
but not both. 0011 0001
~ Binary One's Complement Operator is unary and has the effect (~A) = ~(60), i.e.,
of 'flipping' bits. 1100 0011
<< Binary Left Shift Operator. The left operands value is moved left A << 2 = 240, i.e.,
by the number of bits specified by the right operand. 1111 0000
>> Binary Right Shift Operator. The left operands value is moved A >> 2 = 15, i.e.,
right by the number of bits specified by the right operand. 0000 1111
49. Explain the difference between calloc() and malloc() functions in C language
malloc() and calloc() are functions to allocate memory dynamically at runtime. Below are the differences
between the two:
malloc() calloc()
Memory allocated contains garbage values Memory allocated contains default value zero.
initially.
Syntax: ptr = (cast_type *) malloc (byte_size); Syntax: ptr = (cast_type *) calloc (n, size);
Faster Slower
In the below example the pointer ‘ptr’ is pointing to location ‘a’ which has address ‘5000’. Now later in the
program, we released ‘a’ because it is a local variable of a function, but the ‘ptr’ is still pointing to ‘a’. Hence,
‘ptr’ is a dangling pointer now.
52. What is the difference between static and dynamic memory allocation in the C language?
Memory is allocated during compile time. Memory is allocated during the runtime of the
program.
Once allocated we cannot change the memory We can change the memory size according to
size. need.
Faster Slower
Memory remains allocated till the complete We can release the memory in between the
program runs. program run.
53. What is the difference between NULL and VOID pointers in C language?
A null pointer is a pointer to any data type which is assigned a NULL value.
Void pointer is a data type itself, which remains void until some address reference is assigned to it. When we do
not know the data type of the value which the pointer will point to, then we use a Void pointer.
NULL VOID
A null pointer is a pointer that is not pointing to A void is a pointer type that remains void until
any data type currently. an address is assigned to it.
It can be used to point data types int, float, char, It is a generic pointer which can point to any
etc. according to its own type. data type.
Interview Prep Practice Skills Jobs Products Services Sign in Sign up
About Us
fputw()
fprintf(file, "%d", number); writes an integer to a file
fgetw()
fscanf(file, "%d", &number) reads an integer from a file
55. What is the difference between Random Access File and Sequential File in C language?
When we want to access data from a sequential file, we need to traverse the data sequentially from the start till
we reach the required data record.
However, in a random access file, we can access the data randomly, without traversing the whole file.
Therefore, random access files are faster to access, read and modify data.
File Description
Mode
r Open a file for reading. If a file is in reading mode, then no data is deleted if a file is
already present on a system.
w Open a file for writing. If a file is in writing mode, then a new file is created if a file
doesn't exist at all. If a file is already present on a system, then all the data inside the file
is truncated, and it is opened for writing purposes.
a Open a file in append mode. If a file is in append mode, then the file is opened. The
content within the file doesn't change.
r+ Open for reading and writing from beginning
w+ Open forPrep
Interview reading
and writing,
Practice Skills overwriting
Jobs aProducts
file Services Sign in Sign up
Final Thoughts
It doesn’t matter whether a company is a big IT one or a small software firm, they all seek software
professionals with C coding skills. With these questions, you can sharpen yourbasic programming knowledge.
Join our platform to practise coding skills, take quizzes, and prepare for interviews. Share your questions in the
comment section.
Related Articles
NumPy Interview Questions and Answers 2023 PHP Interview Questions and Answers 2023
HackerTrail
1
HackerTrail January 27, 2023
January 9, 2023