C Programming Basic Interview Questions….
C is a general-purpose, procedural programming language developed by Denis
Ritche in the early 1970s. It is simple, easy to learn, and efficient for computer
programs. While C has been superseded by more modern languages, like Java and
Python, it remains popular due to its versatility and capability of running on
different types of hardware platforms, including embedded systems. In this blog,
we will look at some C interview questions for freshers, intermediates, and
experienced individuals.
1. What is C programming language?
C is a mid-level programming language, also known as the structured
programming language. It divides large programs into smaller modules, where
each module employs structured code.
2. How is C related to other programming languages?
As the majority of compilers and JVMs are written in C, languages created
after C have substantially drawn from it, including C++, Python, Rust,
Javascript, etc. It presents fundamental ideas to these languages, including
arrays, functions, and file management.
3. Why is C called a mid-level programming language?
Due to its ability to combine low-level and high-level programming, C is
referred to as a mid-level programming language. This is because programs
written in C are converted into assembly code but are machine-
independent, which is a high-level language tendency.
4. When was the C language developed? Name its
prominent features.
The C language was developed in 1972 at Bell Laboratories of AT&T by Dennis
Ritchie. Some of its prominent features are:
It is easy and effective.
It is machine-independent or portable.
It is a programming language with structure and dynamic memory management.
It can be expanded and its library is functionally rich.
In C, pointers can be used.
5. What is the difference between local and global
variables in C?
Local variables Global variables
They are declared inside a block or They are declared outside a block or
function. function.
They are stored inside a stack, The compiler decides the storage
unless a different storage location is location of global variables
specified by the programmer.
They function until the block or They function until the execution of
function ends. the program.
To use them in other functions, They do not require any parameter
parameter passing is required. passing and are visible throught the
program.
6. What is an array?
A collection of similar-type components is known as an array. It has an
uninterrupted memory space. It optimizes the code and makes it simple to
sort and navigate through.
7. What is a token?
The individual elements of a program are called tokens. The following 6 types of
tokens are available in C:
Identifiers
Keywords
Constants
Operators
Special Characters
Strings
8. What is the use of printf() and scanf() functions?
The printf() and scanf() functions are in-built library functions used for input and
output purposes in the C language. The printf() function is used to print the output
on the display and the scanf() is used to read formatted data from the keyboard.
9. Explain some data type format specifiers.
o %d: It is used for printing and scanning an integer value.
o %s: It is used for printing and scanning a string.
o %c: It is used for displaying and scanning a character value.
o %f: It is used to display and scan a float value.
10. What is a pointer and NULL pointer in C?
A pointer is a variable that points to a value’s address. It speeds up
performance and optimizes the code. A NULL pointer is a pointer that does
not point to any address of value other than NULL. Any pointer that receives
a value of ‘0’ is converted to a null pointer.
11. What are some of the uses of pointers in C?
The pointers are used in C language for the following purposes:
Save memory space and execution time.
To access array elements.
To pass arguments by reference.
To implement data structures.
For dynamic memory allocation.
For system-level programming that requires memory addresses.
To return multiple values.
12. What is a built-in function?
The built-in functionality, usually referred to as library functions, is a feature
offered by the system that helps developers complete a number of
frequently carried out predefined operations. Some of the commonly used
built-in functions are scanf(), printf(), strcpy, strlwr, strcmp, etc.
13. What is a preprocessor?
A preprocessor is a software program that processes a source file before sending it
to be compiled. It is also known as macro processor as it allows you to define
macros or brief abbreviations for longer constructs. It also allows conditional
compilation, line control, and inclusion of header files.
14. What is the difference between calloc() and
malloc()?
calloc() and malloc() are dynamic memory allocating functions. The sole distinction
between them is that malloc() does not load a value into any of the designated
memory addresses, but calloc() does.
15. Can a C program be compiled or executed in the
absence of a main()?
Although the program won’t run, it will be compiled. Any C program must
have main() in order to run because this function acts as the entry point of
the program where the execution begins. By default, the return type of
main() function is int.
16. Can we use int datatype to store the 32768 value?
No, the integer data type will support the range between -32768 and 32767.
Any value exceeding that will not be stored.
17. . What is pointer to pointer in C?
A pointer can also be used in C to store another pointer’s address. The first pointer
contains the address of a variable, whereas the second pointer contains the
address of the first pointer.
18. What are enumerations?
Enumeration is a user-defined data type, often known as enum in C. It is
made up of constant integrals or integers with user-assigned names. You can
learn more about enumeration through this in-depth C programming
course.
19. How is a function defined in C language?
The function in C language is declared as:
return_type function_name(formal parameter list)
{
Function_Body;
}
20. What is the difference between getc(), getch(),
getche(), and getchar()?
The following is the difference between these four functions in C:
getc(): It reads a single character from the input. If it succeeds, it returns an integer
value. Otherwise, it returns end-of-file (EOF).
getch(): It reads a single character and does not use any buffer. The entered
character is returned immediately without the need to press enter. It is a non-
standard function and is mostly used by MS-DOS compilers.
getche(): It reads a character from the keyboard and displays it on the output
screen. It does not wait for the enter key.
getchar(): It can read a single character from a standard input stream and return it.
You can find this basic function in the stdio.h header file.
21. What is typecasting?
The typecasting process is the change of one data type into another. This
conversion is done either manually or automatically. The programmer can
manually convert the data type, while the compiler does the automatic
conversion
22. What is dynamic memory allocation?
The process of assigning memory to a program and its variables during execution is
known as dynamic memory allocation. Three functions are used in the dynamic
memory allocation process to allocate memory and one function is used to release
memory that has been used.
23. What are r-value and i-value?
A data value kept in memory at a specific location is referred to as an “r-value”.
A value cannot be assigned to an expression that has an r-value. Hence, this
expression can only occur on the right side of the assignment operator (=).
The term “i-value” describes a memory address used to identify an object.
Either the left or right side of the assignment operator (=) contains the i-
value. In many cases, i-value is used as an identification
24. What is the difference between struct and union
in C?
A struct is a collection of intricate data structures kept together in memory
and given independent memory locations so that they can all be accessed
simultaneously. In contrast, all of the member variables in a union are kept
in the same location in memory, which means that changing the value of
one member would also modify the values of all the other members.
25. What is a simple example of structure in C
language?
Structure or struct in C language is a user-defined data type used to group
different variables together in one place. Here is a simple example of structure in
C.
#include <stdio.h>
struct employee {
char name[10];
int age;
} e1;
int main() {
printf("Enter the name: ");
scanf("%s", [Link]);
printf("\n");
printf("Enter the age: ");
scanf("%d", &[Link]);
printf("\n");
printf("Name and age of the employee: %s, %d", [Link], [Link]);
return 0;
}
The output of the code will depend on the values you input during runtime. For
example, if you enter name as John and age as 25, the result will be as follows:
Name and age of the employee: John, 25
26. What is Call by reference?
Call by reference is the term used when the calling function calls a function
without using the addresses of the given parameters. Because all actions in
call by reference are conducted on the value stored in the address of real
parameters, each operation made on formal parameters has an impact on
the value of actual parameters.
27. What is Pass by reference in C?
The callee receives the address in Pass by reference and copies the address
of an argument into the formal parameter. The address is used by the callee
function to retrieve the actual argument and perform manipulations. The
caller function will also be aware of any modifications made to the value
referenced at the given address by the callee function.
28. Explain toupper() with an example.
Toupper() is a function designed to convert lowercase words/characters into
uppercase. Here is an example of this function.
#include <stdio.h>
#include <ctype.h>
int main()
{
char c;
c = 'a';
printf("%c after conversion: %c\n", c, toupper(c));
c = 'B';
printf("%c after conversion: %c\n", c, toupper(c));
return 0;
}
The following is the output for the above program:
a after conversion: A
B after conversion: B
The toupper() function does not modify the characters that are not in lowercase.
So ‘B’ remains ‘B’ and only ‘a’ changes to ‘A’.
29. What is typedef?
typedef provides an alias name to the existing complex type
definition. With typedef, you can simply create an alias for any type.
Whether it is a simple integer to complex function pointer or
structure declaration, typedef will shorten your code.
30. Write a program to swap two numbers
without using the third variable.
Here is a program to swap two numbers without using the third variable:
#include <stdio.h>
#include <conio.h>
int main() {
int a = 10, b = 20;
clrscr();
printf("Before swapping a=%d b=%d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping a=%d b=%d\n", a, b);
getch();
return 0;
}
Output:
Before swapping a=10 b=20
After swapping a=20 b=10
31. Which structure is used to link the program and
the operating system?
The operating system and the program are joined by the file
structure. The file is defined by the “stdio.h” header file (standard
input/output header file). It includes details about the file, including
its size and placement in memory. It also includes a character pointer
that directs the user to the currently opened character. When a file is
opened, the connection is made between the program and the
operating system.
32. What are the two types of loops used in C
programming?
Loops in C programming are used for repeated execution of a block of statements.
The statement is repeated and executed ‘n’ number of times until the given
condition is met. There are two types of loops in C programming. These are:
Entry-Controlled Loops: Here, the test condition is checked before entering the
main body of the loop. Examples: For and While loops.
Exit-Controlled Loops: Here, the test condition is checked at the end of the loop
body. Example: do-while loop.
33. What is the difference between source code and
object code?
Some of the key differences between source code
and object code are:
Source code Object code
A programmer generates the source A compiler or another translator
code. generates the object code.
It can be easily modified. It cannot be modified.
It acts as the input for the compiler or It is the output from the compiler or
translator. translator.
It is a high-level code and humans can It is low-level code and humans cannot
easily understand it. easily understand it.
34. What are reserved keywords?
Each keyword in C programming is related to a specific task in a program. These
are called reserved keywords. They have a defined meaning and you cannot use
them for other purposes than already defined. C language has 32 keywords. Some
of these are int, auto, switch, else, if, etc.
35. Can a program be compiled without the main()
function?
Yes, you can compile a program without the main() function. Instead, you can use
macro, which is a small code written to make the code readable by removing
repetitive expressions.
#include<stdio.h>
#define abc main
int abc ()
{
printf("Hello World ");
return 0;
}
The output for the above-given program is:
Hello World
36. How do you override a macro?
The #ifdef and #undef preprocessors can be used to override a declared macro .
37. What are the limitations of scanf() and how is it
avoided?
The scanf() function cannot be used to insert a multi-word string into a single
variable. The gets() method is used to prevent this.
38. Write a program to check whether a number is in
binary format or not.
#include <stdio.h>
int main() {
int num;
printf("Please enter a number: ");
scanf("%d", &num);
while (num > 0) {
int j = num % 10;
if (j != 0 && j != 1) {
printf("The number is not binary\n");
break;
}
num = num / 10;
if (num == 0) {
printf("The number is binary\n");
}
}
return 0;
}
The output of this program will vary depending on the number you enter.
For example, if you enter the number 1010, the output will be:
Please enter a number: 1010
The number is binary
39. Write a C program to find out a sum of numbers
using recursion.
Recursion in C language is a process in which the program repeats a certain section
of code in a similar way. When a program allows you to call a function inside the
same function, it is called a recursive call of the function. Here is an example of
how this function is used to find out a sum of numbers:
#include<stdio.h>
#include<conio.h>
int sumOfDigits(int num, int sum) {
int rem;
sum = sum + (num % 10);
rem = num / 10;
if (rem > 0) {
return sumOfDigits(rem, sum);
}
return sum;
}
int main() {
int num;
printf("Please enter a number: ");
scanf("%d", &num);
printf("Sum of digits of the number = %d", sumOfDigits(num, 0));
getch();
return 0;
}
The output will depend on the number you input during runtime. For example, if
you enter the number ‘123’, the output will be:
Please enter a number: 123
Sum of digits of the number = 6
40. What is the use of a semicolon (;) at the end of a
program?
The semicolon in C serves as a border between two sets of instructions. It is a key
factor in how the compiler analyses (or parses) the complete code and separates it
into a series of instructions (or statements).
41. What is the difference between macros and
functions?
Macros Functions
It is preprocessed. It is compiled.
Length of code is increased. Code length remains the same.
Execution is faster. Execution is comparatively slower.
Useful when a short section of code Useful when a lengthy piece of code
needs to be used repeatedly across a needs to be repeated.
program.
42. Write a program to find the nth Fibonacci number.
The following is the code to calculate nth Fibonacci number using a
recursive approach. It defines a function ‘fib()’ that takes an integer ‘n’
as input and returns the corresponding Fibonacci number.
#include <stdio.h>
// Function to find the nth Fibonacci number
int fib(int n) {
if (n <= 1) {
return n;
}
int a = 0, b = 1, temp;
for (int i = 2; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return b;
}
int main() {
int n = 8;
printf("nth Fibonacci number is %d", fib(n));
return 0;
}
The following is the output for the above code:
nth Fibonacci number is 21
The output indicates that the 8th Fibonacci number is 21. It is based on
the Fibonacci sequence F(n) = F(n-1) + F(n-2) with base values F(0) = 0
and F(1) = 1.