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:
The 5 key features are as follows:
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:
In any programming language, reserved words are those words that have been pre-defined and designed for particular use cases. You can't change the meaning of any reserved word and use it as an identifier name like function name or variable name. C programming language has over 30 reserved keywords, visible in the following table. For example, "long" is the keyword to define the data type, so you can't use it in the codes.
auto | break | case | char | const | int | float |
do | continue | switch | long | double | default | return |
short | signed | sizeof() | static | struct | typedef | unsigned |
void | while | if | goto | for | else | enum |
Library functions or built-in functions are functions that are pre-defined by the programming language. You can use those functions for different purposes. C language is enriched with hundreds of library functions. The main() is also a built-in function responsible for the code program execution.
Check the following table, which shows some popular library functions of header files "stdio.h" and "math.h"
printf() | It prints the output on the screen. |
scanf() | It takes input from users. |
getchar() | It takes character input from the users. |
gets() | It reads the line. |
pow() | It calculates the power of a number. |
sin() | It calculates the sine of a number. |
cos() | It calculates the cosine of a number. |
exp() | It calculates the exponent of a number. |
sqrt() | It calculates the square root of a number. |
In C or any other programming language, tokens are nothing but individual program elements. Here is a list of standard tokens:
The word "static" defines "fix", and you can't change. In C programming, static functions are fixed to a single file in which it is defined. You can restrict the function scope using static function as in C programming, all functions are by default global.
Check this link to get multiple examples of static functions in C programming.
= operator | == operator |
The operator " = ez-toc-section" > 8. What are 3 main drawbacks of C language? |
Even after having many features, C language has 3 main drawbacks, which are as follows:
It could be defined as either the LHS or RHS of the (=) operator. When it is on the LHS, it holds data value. Like int a; a = 1;
But it is on the RHS, it meant modifiable l-values like expression.
a = 10; // int object & expression
It should be defined at the RHS of the (=) operator.
d = 2; //d has r-value 2.
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!
Function refers to a group of statements to perform a specific task. It is useful in any programming language, especially in C programming because it not only provides large sets of built-in functions but also user-defined functions for executing many tasks. Here are other reasons why it is important:
Function prototype of prototype function refers to the declaration of any function with the following information:
The function prototype provides the assurance that the function call matches the return type and returns the correct number of arguments of the called function. Simply put, it helps prevent the user from making mistakes while using multiple functions in the program.
return_type function_name( datatype argument1, datatype argument 2, …);
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.
Errors are bugs that interrupt the program executions or don't generate accurate outcomes. You can consider them unexpected issues during code design and implementation. You can face 5 types of errors in C languages.
In C programming language, a function pointer refers to a variable storing a function's memory addresses. The main aim of this concept is to store the function's memory address and call the function in the program without writing long code of the function again and again.
You have to use the asterisk symbol (*) to declare the function pointer and function parameters along with return type, and function arguments like this:
return type *function pointer (datatype argument 1, datatype argument 2, …)
Let's understand it with this dummy code:
int demo(int, int); //function
int (*demp_ptr) (int, int);
demo_ptr = demo // assigning the function address to function pointer
//you can call the function as per requirement
In any of these cases, you can create flexible functions and select the appropriate function behaviours.
Click this link to get different example codes for function pointers!
The header file is one of the essential features in the C language. It refers to a file or place which consists of multiple functions to perform many tasks. The header file has the extension ".h" and contains 3 elements – function declaration, data type definitions, and macros.
You can't run any code without including header files. You have to use the preprocessor directive "#include" for this. Like this,
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.
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 can give a driving licence if a person is 18+. To solve such problems, you need to use decision-making processes.
Options | Feature |
If statement | Helps you to make a simple decision-making process |
If-else statement | You can use it to check conditions and execute the required codes with alternative options. |
Nested-if statement | Sometimes, you need to check conditions within conditions. And in that case, 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. |
Image credit: Nerdyelectronics
In C or any other programming language, you declare the required function in "function declaration" and define the function in "function definition".
In other words, you have to provide every detail of the function in the "function definition".
Section name | Use |
Function Header | It should be identical to the function declaration except for the semicolon. You 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 Return type | What would be the return value of the function |
Int sum(int x, int y); // Function Declaration int sum(int a, int b) //Function Header; //function name – sum // Return type - int < int c; c = a + b; printf(“Sum = %d”, c); >Whenever a function, sum() is called, users need to pass two integer numbers and it would return the sum of those number.
In C programming language, modifiers are keywords that change data types' meaning to modify the allocated memory for variables.
Size modifiers: These modify the size of the data type. It has subcategories: short and long
Sign modifiers: It changes the type of data type. It has subcategories: signed and unsigned
Default modifier of datatypes: int & char
You can use it for storing positive or negative value.
Data type | Storage (bytes) | Format specifier |
short int | 2 | %hd |
unsigned short int | 2 | %hu |
unsigned int | 4 | %u |
int | 4 | %u |
long int | 4 | %ld |
unsigned long int | 4 | %lu |
long long int | 8 | %lld |
unsigned long long int | 8 | %llu |
char | 1 | %c |
unsigned char | 1 | %c |
float | 4 | %f |
double | 8 | %lf |
Long double | 12 | %Lf |
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:
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:
In C programming, sizeof() is a unary operator, not a function. However, it looks like a function but plays the operator's role, which has the objective of computing the size of operands. You can use it with any data type.
sizeof(a+b) = 4 (as sum of a and b is 2.0 which is float so it returns 4)
C programming is the foundation. You should also be aware of the trends. Find out what the latest frontend development trends are by clicking this link.
#include int main() < int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); printf("Pyramid Design\n"); for (i = 1; i printf("\n"); > return 0; > Output: Enter the number of rows: 5 Pyramid Design * * * * * * * * * * * * * * *
In C programming language, preprocessor refers to the macro processor to define the macros (a small chunk of code or a kind of brief abbreviations). The C compiler calls it automatically to transform the program before compilation. You can use the preprocessor in the following ways:
Some examples: #define, #include, #if
Yes, it is possible to convert a string to a number and vice versa. The C language provides a built-in function, atoi(), that converts the string to an integer. To use this function, you must include the header file, stdlib.h.
int atoi (const char *string)
// you have to pass the string to the function, which will return the integer on successful execution. But, it returns 0 if the string is alphanumeric characters or starts with an alphanumeric character.
//In another case, if the string starts with a numeric character followed by an alphanumeric, the string is converted to an integer until the occurrence of the first alphanumeric character.
Recursion is a concept which is often used in a programming language. It allows you to call a function itself continuously. You have to put conditions to terminate the recursive function calls; otherwise, it will go to a loop.
In the following dummy code, you can see that a function, fact() is a recursive calling itself. And the main() function has declared it.
void fact() // recursive function which is calling itself < fact(); . . >void main() // main function to define and declare the recursive function
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.
Yes, executing a program without main() function in C is possible. You can use the preprocessor directive, #define, with arguments for this. This directive does not need any source code and you can run the program without the primary function.
Please refer to the following dummy code to understand it is done:
#include #define demo main int demo(void)
Here, #define preprocessor directive is defining macro, which is replaced by the value of the macro.
Macro | Function |
Macro is a small chunk of code which gets replaced whenever respective macros have been called. | A function is a group of one or more statements to perform a specific job. |
It is called a preprocessor directive, and it requires pre-processing. | It could be predefined or user-defined, and it needs compilation. |
It does not require any type-checking. | It requires type-checking. |
The increased number of macros increases the size of the whole program. | The function length does not impact the 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 multiple times. | Use functions when executing large codes and different tasks in a program. |
#include 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: Before swap a=162 b=243 After swap a=243 b=162
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 ( 29. What are the different file opening modes in C programming language?
C language allows to open files in the following modes:
Mode | Functionality |
r | open a file in read mode |
w | open or create a text file in write mode |
a | open a file in append mode |
r+ | open a file in both read and write mode |
w+ | open a file in both read and write mode |
a+ | open a file in both read and append mode |
#include #include enum ; 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.
The basic data types in C are:
It is used to store whole numbers, may have below types depending on their sign and size:
They are used to store real numbers(with decimal points). Floating point data types in C have below types based on size:
They are used to store character values and can have below types based on sign:
The void data type is used to depict “No value” when a function returns nothing to the calling function, it is specified using Void.
Processor | Size of int | Range |
16-Bit | 2 Bytes | -32,768 to 32,767 |
32-Bit | 4 Bytes | -2,147,483,648 to 2,147,483,647 |
The enumerated data type is specified using keyword-’enum’. It is used for creating user-defined data types which have integral constants as their elements.
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’.
C provides many string functions in its standard library header file- ‘string.h’. The most commonly used string functions are:
String Function | Description |
strlen(string_source) | Returns length of the string_source |
strcpy(string_destination, string_source) | Copies string_source into string_destination. |
strcat(string_destination, string_source) | Joins both the strings and stores the output in string_destination. |
strcmp(string_destination, string_source) | Compares both strings, if found equal returns 0(zero). |
strrev(string_source) | Reverses the string |
strlwr(string_source) | Returns the string into lowercase |
strupr(string_source) | Returns the string into uppercase |
Typecasting refers to converting a variable’s data type into another data type explicitly by the programmer.
Type conversion can be performed in two ways:
Performed by the C compiler automatically, without any manual/programmer indication.
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.
It is deliberately performed by the programmer in the code by mentioning the required data type and this is known as typecasting in C.
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’.
Local Variable | Global Variable |
They are declared inside a function, their scope is limited to that particular function. | They are declared outside a function, their scope is throughout the program. |
When uninitialised, they store any garbage value. | When uninitialised, they store the default value as zero. |
To pass the value of local variables from one function to another, parameter passing is required. | Parameter passing is not required, because the scope is throughout the program, in all functions. |
If any changes are made to a local variable the changes are not reflected in other functions. | If any changes are made to a global variable the changes are reflected in all functions, at all places in the program. |
Formal Parameters | Actual Parameters |
These parameters are a list of variables. Here we define the data type of parameters which will be accepted by that function. | It is not a definition of parameters, here we provide the actual variables(parameters). |
They are mentioned in the function definition. | They are mentioned during the actual function call. |
The data type of the formal parameters is mentioned in the function definition. | Data types are not mentioned during making a call through actual parameters. |
To derive a negative integer we calculate two’s complement of the positive number.
This can be done in a two-step process.
Below are the differences between an array and a pointer in storage:
Array | Pointer |
It is a collection of elements stored in memory continuously. | It stores the address of a variable. |
Memory allocation is sequential. | Memory allocation is dynamic. |
The memory allocation is static, once allocated cannot be changed in size or released. | The memory allocation is dynamic, the allocated memory can be resized or released. |
Memory is allocated at compile time. | Memory is allocated at runtime. |
Call by value | Call by reference |
During function call when we pass variables by copying their values it is known as call by value. | During function call when we pass the address of the variables it is known as call by reference. |
If any changes are made to the values they are not reflected in the actual variables. | If any changes are made they are reflected in the actual variables because addresses were passed. |
strcmp() can be used to compare two strings character by character and return an integer. It starts comparing the two strings character by character until the end of the string is reached which is null(‘\0’) or there is a mismatch.
Below are the differences between a String and Array in C:
String | Array |
It is a sequence of characters, whose end is denoted by NULL characters ('\0'). | It is a data structure, whose elements are of the same data type and accessed via index. |
String size can be changed if it is a char pointer. | The size of the array cannot be changed later. |
It can contain characters only. | It can contain characters, integers, floats, etc. |
Prototype: char * itoa ( int value, char * str, int base ); Where : value is the integer to be converted to a string str is the resulting char array base is the numerical base of the integer value Returns a pointer to the char string.
Prototype: int atoi (const char * str); Where str is a string constant which is received Returns the integer if the string is successfully converted, otherwise returns zero.
strcat() | strncat() |
Syntax: strcat(source, destination) | Syntax: strncat(source, destination, number_of_characters) |
Concatenates destination string at the end of the source string. | Concatenates destination string at the end of the source string up to the number_of_characters mentioned. |
Find code snippets describing the differences between strncat() and strcat() below:
strcat(source, destination);Example: #include #include #include int main()
There are 6 types of operators in C language:
To perform arithmetic operations we use arithmetic operators.
To perform relational operations between operands.
To perform logical operations between operands.
To perform bit-by-bit operations on bits.
To assign value to a variable.
These are special or miscellaneous operators.
Read in detail about the operators present in the C language here.
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.
i=5; i++; printf(“%d”, i); Output is : 6
i=5; ++i; printf(“%d”, i); 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.
i=5; j=i++; 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.
i=5; j=++i; 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.
Below are the bitwise operators in C with examples:
Let's assume A = 60, in binary : 0011 1100
B =13, in binary : 0000 1101
Operator | Description | Example |
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) = 12, i.e., 0000 1100 |
| | Binary OR Operator copies a bit if it exists in either operand. | (A | B) = 61, i.e., 0011 0001 |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) = 49, i.e., 0011 0001 |
~ | Binary One's Complement Operator is unary and has the effect of 'flipping' bits. | (~A) = ~(60), i.e., 1100 0011 |
> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 = 15, i.e., 0000 1111 |
While writing an application, the developer has allocated some memory which he missed to deallocate after use. This results in unused allocated memory - and such phenomenon is referred to as a memory leak in C. Unused allocated memory could affect the system's performance and cause the speed to be slower. As a best practise, it is important to release all allocated memory after usage
malloc() and calloc() are functions to allocate memory dynamically at runtime. Below are the differences between the two:
malloc() | calloc() |
It allocates a single block of memory dynamically. | Allocates multiple blocks of memory dynamically. |
Memory allocated contains garbage values initially. | Memory allocated contains default value zero. |
Syntax: ptr = (cast_type *) malloc (byte_size); | Syntax: ptr = (cast_type *) calloc (n, size); |
Faster | Slower |
You may read more about pointers here.
A dangling pointer in C is a normal pointer which is pointing to a memory location that is either deleted or deallocated.
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.
Static memory allocation | Dynamic memory allocation |
Memory is allocated during compile time. | Memory is allocated during the runtime of the program. |
Once allocated we cannot change the memory size. | We can change the memory size according to need. |
Faster | Slower |
Memory remains allocated till the complete program runs. | We can release the memory in between the program run. |
Example: Array | Example: Tree |
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 any data type currently. | A void is a pointer type that remains void until an address is assigned to it. |
It can be used to point data types int, float, char, etc. according to its own type. | It is a generic pointer which can point to any data type. |
Function name | Description of the function |
fopen() | opens a new or an existing file |
fprintf() | write data into a file |
fscanf() | reads data from a file |
fputc() | writes a character into a file |
fgetc() | reads a character from a file |
fclose() | closes a file |
fseek() | sets the file pointer to a given position |
fputw() | writes an integer to a file |
fgetw() | reads an integer from a file |
ftell() | returns the current position of the file pointer |
rewind() | sets the file pointer to the beginning of a file |
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.
Below are the different file opening modes in C:
File Mode | Description |
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 for reading and writing, overwriting a file |
a+ | Open for reading and writing, appending to file |
Rewind() is used to set the file pointer to the beginning of the file.
void rewind(FILE *stream);
This function does not return any value. We should include the ‘stdio.h’ header file in the program to use this function.
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.