At Bell Labs, Dennis Ritchie developed the C programming language between 1971 and 1973. C is a mid-level structured-oriented programming and general-purpose programming. It is one of the old and most popular programming languages. There are many applications in which C programming language is used, including language compilers , operating systems , assemblers, network drivers, text editors, print spoolers, modern applications, language interpreters, databases , and utilities.
C Programming language is one of the languages that are both complex yet important to learn for strengthening your programming skills. Interview questions can be categorized into two parts:
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.
Features of C Programming language
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:
Data Types in C
For more information, refer to the article – 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:
For more information, refer to the article – Tokens in C
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:
For more information, refer to the article – Scope in C
In C preprocessor directives are considered the built-in predefined functions or macros that act as a directive to the compiler and are executed before the program execution. There are multiple steps involved in writing and executing a program in C. Main types of Preprocessor Directives are Macros , File Inclusion, Conditional Compilation, and Other directives like #undef, #pragma, etc.
For more information, refer to the article – Preprocessor Directives in C
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.
Initial value of static variable 0 Initial value of variable without static 0
For more information, refer to the article – Static Variables in C
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.
Parameter | Malloc() | Calloc() |
---|---|---|
Definition | It is a function that creates one block of memory of a fixed size. | It is a function that assigns more than one block of memory to a single variable. |
Number of arguments | It only takes one argument. | It takes two arguments. |
Speed | malloc() function is faster than calloc(). | calloc() is slower than malloc(). |
Efficiency | It has high time efficiency. | It has low time efficiency. |
Usage | It is used to indicate memory allocation. | It is used to indicate contiguous memory allocation. |
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.
The string for the num is 32.230000
For more information, refer to the article – sprintf() in C
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.
For more information, refer to the article – Recursion
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:
Working of Pointer
For more information, refer to the article – Pointer Uses in C.
In C programming, typedef is a keyword that defines an alias for an existing type. Whether it is an integer variable, function parameter, or structure declaration, typedef will shorten the name.
Syntax:
Example:
typedef long long ll
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.
Below is the program for infinite loop in C:
For more information, refer to the article – Loops in C.
C language has numerous libraries which contain predefined functions to make programming easier. Header files contain predefined standard library functions. All header files must have a ‘.h’ extension. Header files contain function definitions, data type definitions, and macros which can be imported with the help of the preprocessor directive ‘#include’. Preprocessor directives instruct the compiler that these files are needed to be processed before the compilation.
There are two types of header files i.e, User-defined header files and Pre-existing header files . For example, if our code needs to take input from the user and print desired output to the screen then ‘stdio.h’ header file must be included in the program as #include. This header file contains functions like scanf() and printf() which are used to take input from the user and print the content.
For more information, refer to the article – Header Files in C
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:
For more information, refer to the article – Functions in C
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 |
---|---|
Macros are preprocessed. | Functions are compiled. |
Code length is increased using macro. | Code length remains unaffected using function. |
Execution speed using a macro is faster. | Execution speed using function is slower. |
The macro name is replaced by the macro value before compilation. | Transfer of control takes place during the function call. |
Macro doesn’t check any Compile-Time Errors. | Function check Compile-time errors. |
For more information, refer to the article – Macro vs Functions
In C we have 2 main methods to convert strings to numbers i.e, Using string stream, Using stoi() library Function or using atoi() library function.
For more information, refer to the article – String to Numbers in C
Every keyword is meant to perform a specific task in a program. Their meaning is already defined and cannot be used for purposes other than what they are originally intended for. C Programming language supports 32 keywords. Some examples of reserved keywords are auto, else, if, long, int, switch, typedef, etc.
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:
Name: Kamlesh_Joshi Roll_No: 27 Address: Haldwani Branch: Computer Science And Engineering
For more information, refer to the article – Structure in C
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:
For more information, refer to the article – Union in C
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). 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:
Here, val is the ‘l-value’, and 20 is the ‘r-value’.
For more information, refer to the article – r-value and l-value in C
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.
For more information, refer to the article – sleep() Function in C
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.
enum enumeration_name;
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.
For more information, refer to the article – Enumeration (or enum) in C
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.
Output:
Please Enter number of Elements: 5
Fibonacci Series with the help of Recursion:
0 1 1 2 3
Fibonacci Series without Using Recursion:
0 1 1 2 3
For more information, refer to the article – Fibonacci Numbers
Number Prime or not
For more information, refer to the article – Prime or Not
Source Code | Object Code |
---|---|
Source code is generated by the programmer. | object code is generated by a compiler or another translator. |
High-level code which is human-understandable. | Low-level code is not human-understandable. |
Source code can be easily modified and contains less number of statements than object code. | Object code cannot be modified and contains more statements than source code. |
Source code can be changed over time and is not system specific. | Object code can be modified and is system specific. |
Source code is less close to the machine and is input to the compiler or any other translator. | Object code is more close to the machine and is the output of the compiler or any other translator. |
Language translators like compilers, assemblers, and interpreters are used to translate source code to object code. | Object code is machine code so it does not require any translation. |
For more information, refer to the article – Source vs Object Code.
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.
For more information, refer to the article – Pass By Reference
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:
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.
For more information, refer to the article – Storage Classes in C
For more information, refer to the article – Hello World in C
Swap Two Number
Values before swap are var1 = 50 and var2 = 60 Values after swap are var1 = 60 and var2 = 50
abba is a Palindrome
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:
Factorial of a Number
Factorial of 5 is 120
Enter Number 0 is an Armstrong number
Output:
Enter Number to be reversed :
Number After reversing digits is: 321
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:
scanf() method, reads the values from the console as per the data type specified.
Syntax:
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().
For more information, refer to the article – Format Specifier in C
In C programming Basic File Handling Techniques provide the basic functionalities that programmers can perform against the system.
File Operations in C
Merge two sorted linked lists
next is the place to add new <span other list */ */ 10->15, b: 2->3->20 */Merged Linked List is: 2 3 5 10 15 20