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 without doubt one of the outdated and hottest programming languages. There are numerous functions through which C programming language is used, together with language compilers, working techniques, assemblers, community drivers, textual content editors, print spoolers, fashionable functions, language interpreters, databases, and utilities.

On this article, you’re going to get the ceaselessly and most requested C programming interview questions and solutions on the brisker and skilled ranges.
C Programming Interview Questions – For Freshers
1. Why is C referred to as a mid-level programming language?
Attributable to its skill to assist each low-level and high-level options, C is taken into account a middle-level language. It’s each an assembly-level language, i.e. a low-level language, and a higher-level language. Packages which are written in C are transformed into meeting code, they usually assist pointer arithmetic (low-level) whereas being machine-independent (high-level). Subsequently, C is also known as a middle-level language. C can be utilized to write down working techniques and menu-driven shopper billing techniques.
2. What are the options of the C programming language?
For extra info, seek advice from the article – Options of C programming language.
3. What are primary knowledge varieties supported within the C Programming Language?
Every variable in C has an related knowledge kind. Every knowledge kind requires completely different quantities of reminiscence and has some particular operations which could be carried out over it. It specifies the kind of knowledge that the variable can retailer like integer, character, floating, double, and so forth. In C knowledge varieties are broadly categorised into 4 classes:
- Primitive knowledge varieties: Primitive knowledge varieties could be additional categorised into an integer, and floating knowledge varieties.
- Void Varieties: Void knowledge varieties come beneath primitive knowledge varieties. Void knowledge varieties present no consequence to their caller and don’t have any worth related to them.
- Person Outlined knowledge varieties: These knowledge varieties are outlined by the consumer to make this system extra readable.
- Derived knowledge varieties: Knowledge varieties which are derived from primitive or built-in knowledge varieties.
For extra info, seek advice from the article – Knowledge Varieties in C.
4. What are tokens in C?
Tokens are identifiers or the smallest single unit in a program that’s significant to the compiler. In C we have now the next tokens:
- Key phrases: Predefined or reserved phrases within the C programming language. Each key phrase is supposed to carry out a particular activity in a program. C Programming language helps 32 key phrases.
- Identifiers: Identifiers are user-defined names that include an arbitrarily lengthy sequence of digits or letters with both a letter or the underscore (_) as a primary Character. Identifier names can’t be equal to any reserved key phrases within the C programming language. There are a algorithm which a programmer should comply with as a way to identify an identifier in C.
- Constants: Constants are regular variables that can’t be modified in this system as soon as they’re outlined. Constants seek advice from a set worth. They’re additionally known as literals.
- Strings: Strings in C are an array of characters that finish with a null character (‘ ). Null character signifies the tip of the string;
- Particular Symbols: Some particular symbols in C have some particular which means and thus, they can’t be used for some other objective in this system. # = {} () , * ; [] are the particular symbols in C programming language.
- Operators: Symbols that set off an motion when they’re utilized to any variable or some other object. Unary, Binary, and ternary operators are used within the C Programming language.
For extra info, seek advice from the article – Tokens in C
5. What do you imply by the scope of the variable?
Scope in a programming language is the block or a area the place an outlined variable may have its existence and past that area, the variable is mechanically destroyed. Each variable has its outlined scope. In easy phrases, the scope of a variable is the same as its life in this system. The variable could be declared in three locations These are:
- Native Variables: Inside a given perform or a block
- World Variables: Out of all capabilities globally inside this system.
- Formal Parameters: In perform parameters solely.
For extra info, seek advice from the article – Scope in C
6. What are preprocessor directives in C?
In C preprocessor directives are thought-about the built-in predefined capabilities or macro that act as a directive to the compiler and are executed earlier than this system execution. There are a number of steps concerned in writing and executing a program in C. Primary kinds of Preprocessor Directives are Macros, File Inclusion, Conditional Compilation, and Different directives like #undef, #pragma, and so forth.
For extra info, seek advice from the article – Preprocessor Directives in C
7. What’s the usage of static variables in C?
Static variables within the C programming language are used to protect the information values between perform calls even after they’re out of their scope. Static variables protect their values of their scope they usually can be utilized once more in this system with out initializing once more. Static variables have an preliminary worth assigned to 0 with out initialization.
C
|
|
Preliminary worth of static variable 0 Preliminary worth of variable with out static 0
For extra info, seek advice from the article – Static Variables in C
8. What’s the distinction between malloc() and calloc() within the C programming language?
calloc() and malloc() library capabilities are used to allocate dynamic reminiscence. Dynamic reminiscence is the reminiscence that’s allotted in the course of the runtime of this system from the heap phase. “stdlib.h” is the header file that’s used to facilitate dynamic reminiscence allocation within the C Programming language.
| Parameter | Malloc() | Calloc() |
|---|---|---|
| Definition | It’s a perform that creates one block of reminiscence of a set measurement. | It’s a perform that assigns a couple of block of reminiscence to a single variable. |
| Variety of arguments | It solely takes one argument. | It takes two arguments. |
| Pace | malloc() perform is quicker than calloc(). | calloc() is slower than malloc(). |
| Effectivity | It has excessive time effectivity. | It has low time effectivity. |
| Utilization | It’s used to point reminiscence allocation. | It’s used to point contiguous reminiscence allocation. |
For extra info, seek advice from the article – Dynamic Reminiscence Allocation in C utilizing malloc(), calloc(), free() and realloc()
9. What do you imply by dangling pointers and the way are dangling pointers completely different from reminiscence leaks in C programming?
Pointers pointing to deallocated reminiscence blocks in C Programming are often called dangling pointers i.e, every time a pointer is pointing to a reminiscence location and In case the variable is deleted and the pointer nonetheless factors to that very same reminiscence location then it is named a dangling pointer variable.
In C programming reminiscence leak happens once we allocate reminiscence with the assistance of the malloc() or calloc() library perform, however we neglect to free the allotted reminiscence with the assistance of the free() library perform. Reminiscence leak causes this system to make use of an undefined quantity of reminiscence from the RAM which makes it unavailable for different working applications this causes our program to crash.
10. Write a program to transform a quantity to a string with the assistance of sprintf() perform within the C library.
C
|
|
The string for the num is 32.230000
For extra info, seek advice from the article – sprintf() in C
11. What’s recursion in C?
Recursion is the method of constructing the perform name itself straight or not directly. A recursive perform solves a selected drawback by calling a duplicate of itself and fixing smaller subproblems that sum up the unique issues. Recursion helps to cut back the size of code and make it extra comprehensible. The recursive perform makes use of a LIFO ( Final In First Out ) construction like a stack. Each recursive name in this system requires additional house within the stack reminiscence.
For extra info, seek advice from the article – Recursion
12. What’s the distinction between the native and world variables in C?
Native variables are declared inside a block or perform however world variables are declared outdoors the block or perform to be accessed globally.
|
Native Variables |
World Variables |
|---|---|
| Declared inside a block or a perform. | Variables which are declared outdoors the block or a perform. |
| By default, variables retailer a rubbish worth. | By default worth of the worldwide worth is zero. |
| The lifetime of the native variables is destroyed after the block or a perform. | The lifetime of the worldwide variable exists till this system is executed. |
| Variables are saved contained in the stack until they’re specified by the programmer. | The storage location of the worldwide variable is determined by the compiler. |
| To entry the native variables in different capabilities parameter passing is required. | No parameter passing is required. They’re globally seen all through this system. |
13. What are pointers and their makes use of?
Pointers are used to retailer the handle of the variable or a reminiscence location. Pointer will also be used to refer to a different pointer perform. The principle objective of the pointer is to save lots of reminiscence house and enhance execution time. Makes use of of pointers are:
- To move arguments by reference
- For accessing array parts
- To return a number of values
- Dynamic reminiscence allocation
- To implement knowledge buildings
- To do system-level programming the place reminiscence addresses are helpful
For extra info, seek advice from the article – Pointer Makes use of in C.
14. What’s typedef in C?
In C programming, typedef is a key phrase that defines an alias for an current kind. Whether or not it’s an integer variable, perform parameter, or construction declaration, typedef will shorten the identify.
Syntax:
typedef <existing-type> <alias-name>
Right here,
- current kind is already given a reputation.
- alias identify is the brand new identify for the present variable.
Instance:
typedef lengthy lengthy ll
15. What are loops and the way can we create an infinite loop in C?
Loops are used to execute a block of statements repeatedly. The assertion which is to be repeated can be executed n occasions contained in the loop till the given situation is reached. There are two kinds of loops Entry managed and Exit managed loops in C programming language. An Infinite loop is a chunk of code that lacks a useful exit. So, it repeats indefinitely. There could be solely two issues when there may be an infinite loop in this system. One it was designed to loop endlessly till the situation is met inside the loop. One other could be incorrect or unhappy break situations in this system.
Beneath is this system for infinite loop in C:
C
|
|
For extra info, seek advice from the article – Loops in C.
16. What’s the distinction between kind casting and kind conversion?
|
Kind Casting |
Kind Conversion |
|---|---|
| The information kind is transformed to a different knowledge kind by a programmer with the assistance of a casting operator. | The information kind is transformed to a different knowledge by a compiler. |
| It may be utilized to each appropriate knowledge varieties in addition to incompatible knowledge varieties. | Kind conversion can solely be utilized to solely appropriate knowledge varieties. |
| In Kind casting as a way to forged the information kind into one other knowledge kind, a caste operator is required | In kind conversion, there isn’t any want for a casting operator. |
| Kind casting is extra environment friendly and dependable. | Kind conversion is much less environment friendly and fewer dependable than kind casting. |
| Kind casting takes place throughout this system design by the programmer. | Kind conversion is finished at compile time. |
| Syntax: destination_data_type = (target_data_type) variable_to_be_converted; |
Syntax: int a = 20; float b; b = a; // a = 20.0000 |
For extra info, seek advice from the article – Kind Casting and Kind Conversion.
17. What are header recordsdata and their makes use of?
C language has quite a few libraries which include predefined capabilities to make programming simpler. Header recordsdata include predefined normal library capabilities. All header recordsdata should have a ‘.h’ extension. Header recordsdata include perform definitions, knowledge kind definitions, and macros which could be imported with the assistance of the preprocessor directive ‘#embody’. Preprocessor directives instruct the compiler that these recordsdata are wanted to be processed earlier than the compilation.
There are two kinds of header recordsdata i.e, Person-defined header recordsdata and Pre-existing header recordsdata. For instance, if our code must take enter from the consumer and print desired output to the display screen then ‘stdio.h’ header file have to be included in this system as #embody<stdio.h>. This header file comprises capabilities like scanf() and printf() that are used to take enter from the consumer and print the content material.
For extra info, seek advice from the article – Header Information in C
18. What are the capabilities and their varieties?
The perform is a block of code that’s used to carry out a activity a number of occasions moderately than writing it out a number of occasions in our program. Capabilities keep away from repetition of code and enhance the readability of this system. Modifying a program turns into simpler with the assistance of perform and therefore reduces the probabilities of error. There are two kinds of capabilities:
- Person-defined Capabilities: Capabilities which are outlined by the consumer to cut back the complexity of huge applications. They’re constructed solely to fulfill the situation through which the consumer is going through points and are generally often called “tailored capabilities”.
- Constructed-in Capabilities: Library capabilities are supplied by the compiler bundle and include particular capabilities with particular and completely different meanings. These capabilities give programmers an edge as we will straight use them with out defining them.
For extra info, seek advice from the article – Capabilities in C
19. What’s the distinction between macro and capabilities?
A macro is a reputation that’s given to a block of C statements as a pre-processor directive. Macro is outlined with the pre-processor directive. Macros are pre-processed which signifies that all of the macros could be preprocessed earlier than the compilation of our program. Nevertheless, capabilities usually are not preprocessed however compiled.
| Macro | Operate |
|---|---|
| Macros are preprocessed. | Capabilities are compiled. |
| Code size is elevated utilizing macro. | Code size stays unaffected utilizing perform. |
| Execution velocity utilizing a macro is quicker. | Execution velocity utilizing perform is slower. |
| The macro identify is changed by the macro worth earlier than compilation. | Switch of management takes place in the course of the perform name. |
| Macro doesn’t test any Compile-Time Errors. | Operate test Compile-time errors. |
For extra info, seek advice from the article – Macro vs Capabilities
C Programming Interview Questions – Intermediate Stage
20. Methods to convert a string to numbers in C?
In C we have now 2 important strategies to transform strings to numbers i.e, Utilizing string stream, Utilizing stoi() library Operate, and utilizing atoi() library perform.
- sscanf(): It reads enter from a string moderately than normal enter.
- atoi(): This perform takes a string literal or a personality array as an argument and an integer worth is returned.
For extra info, seek advice from the article – String to Numbers in C
21. What are reserved key phrases?
Each key phrase is supposed to carry out a particular activity in a program. Their which means is already outlined and can’t be used for functions aside from what they’re initially meant for. C Programming language helps 32 key phrases. Some examples of reserved key phrases are auto, else, if, lengthy, int, change, typedef, and so forth.
For extra info, seek advice from the article – Variables and Key phrases in C
22. What’s a construction?
The construction is a key phrase that’s used to create user-defined knowledge varieties. The construction permits storing a number of kinds of knowledge in a single unit. The construction members can solely be accessed by the construction variable.
Instance:
struct pupil
{
char identify[20];
int roll_no;
char handle[20];
char department[20];
};
Beneath is the C program to implement construction:
C
|
|
Title: Kamlesh_Joshi Roll_No: 27 Tackle: Haldwani Department: Laptop Science And Engineering
For extra info, seek advice from the article – Construction in C
23. What’s union?
A union is a user-defined knowledge kind that permits customers to retailer a number of kinds of knowledge in a single unit. Nevertheless, a union doesn’t occupy the sum of the reminiscence of all members. It holds the reminiscence of the most important member solely. Because the union allocates one frequent house for all of the members we will entry solely a single variable at a time. The union could be helpful in lots of conditions the place we wish to use the identical reminiscence for 2 or extra members.
Syntax:
union name_of_union
{
data_type identify;
data_type identify;
};
For extra info, seek advice from the article – Union in C
24. What’s an r-value and worth?
An “l-value” refers to an object with an identifiable location in reminiscence (i.e. having an handle). An “l-value” will seem both on the proper or left facet of the task operator(=). An “r-value” is a knowledge worth saved in reminiscence at a given handle. An “r-value” refers to an object with out an identifiable location in reminiscence (i.e. with out an handle). An “r-value” is an expression that can’t be assigned a price, due to this fact it could possibly solely exist on the proper facet of an task operator (=).
Instance:
int val = 20;
Right here, val is the ‘l-value’, and 20 is the ‘r-value’.
For extra info, seek advice from the article – r-value and l-value in C
25. What’s the distinction between name by worth and name by reference?
|
Name by worth |
Name by Reference |
|---|---|
| Values of the variable are handed whereas perform calls. | The handle of a variable(location of variable) is handed whereas the perform name. |
| Dummy variables copy the worth of every variable within the perform name. | Dummy variables copy the handle of precise variables. |
| Adjustments made to dummy variables within the referred to as perform don’t have any impact on precise variables within the calling perform. | We are able to manipulate the precise variables utilizing addresses. |
| A easy method is used to move the values of variables. | The handle values of variables have to be saved in pointer variables. |
For extra info, seek advice from the article – Name by Worth and Name by Reference
26. What’s the sleep() perform?
sleep() perform in C permits the customers to attend for a present thread for a given period of time. sleep() perform will sleep the current executable for the given period of time by the thread however different operations of the CPU will perform correctly. sleep() perform returns 0 if the requested time has elapsed.
For extra info, seek advice from the article – sleep() Operate in C
27. What are enumerations?
In C, enumerations (or enums) are user-defined knowledge varieties. Enumerations permit integral constants to be named, which makes a program simpler to learn and keep. For instance, the times of the week could be outlined as an enumeration and can be utilized wherever in this system.
enum enumeration_name{constant1, constant2, ... };
C
|
|
Within the above instance, we declared “day” because the variable, and the worth of “Wed” is allotted to day, which is 2. So in consequence, 2 is printed.
For extra info, seek advice from the article – Enumeration (or enum) in C
28: What’s a risky key phrase?
Risky key phrase is used to stop the compiler from optimization as a result of their values can’t be modified by code that’s outdoors the scope of present code at any time. The System at all times reads the present worth of a risky object from the reminiscence location moderately than conserving its worth in a short lived register on the level it’s requested, even when earlier instruction is requested for the worth from the identical object.
29. Write a C program to print the Fibonacci collection utilizing recursion and with out utilizing recursion.
C
|
|
Output: Please Enter variety of Parts: 5 Fibonacci Sequence with the assistance of Recursion: 0 1 1 2 3 Fibonacci Sequence with out Utilizing Recursion: 0 1 1 2 3
For extra info, seek advice from the article – Fibonacci Numbers
30. Write a C program to test whether or not a quantity is prime or not.
C
|
|
For extra info, seek advice from the article – Prime or Not
31. How is supply code completely different from object code?
| Supply Code | Object Code |
|---|---|
| Supply code is generated by the programmer. | object code is generated by a compiler or one other translator. |
| Excessive-level code which is human-understandable. | Low-level code will not be human-understandable. |
| Supply code could be simply modified and comprises much less variety of statements than object code. | Object code can’t be modified and comprises extra statements than supply code. |
| Supply code could be modified over time and isn’t system particular. | Object code could be modified and is system particular. |
| Supply code is much less near the machine and is enter to the compiler or some other translator. | Supply code is extra near the machine and is the output of the compiler or some other translator. |
| Language translators like compilers, assemblers, and interpreters are used to translate supply code to object code. | Object code is machine code so it doesn’t require any translation. |
For extra info, seek advice from the article – Supply vs Object Code.
32. What’s static reminiscence allocation and dynamic reminiscence allocation?
- Static reminiscence allocation: Reminiscence allocation which is finished at compile time is named static reminiscence allocation. Static reminiscence allocation saves working time. It’s quicker than dynamic reminiscence allocation as reminiscence allocation is finished from the stack. This reminiscence allocation methodology is much less environment friendly as in comparison with dynamic reminiscence allocation. It’s principally most popular within the array.
- Dynamic reminiscence allocation: Reminiscence allocation accomplished at execution or run time is named dynamic reminiscence allocation. Dynamic reminiscence allocation is slower than static reminiscence allocation as reminiscence allocation is finished from the heap. This reminiscence allocation methodology is extra environment friendly as in comparison with static reminiscence allocation. It’s principally most popular within the linked listing.
For extra info, seek advice from the article – Static and Dynamic Reminiscence Allocation in C
33. What’s pass-by-reference in capabilities?
Cross by reference permits a perform to change a variable with out making a duplicate of the variable. The Reminiscence location of the handed variable and parameter is identical, so any adjustments accomplished to the parameter can be mirrored by the variables as properly.
C
|
|
For extra info, seek advice from the article – Cross By Reference
34. What’s a reminiscence leak and the way to keep away from it?
At any time when a variable is outlined some quantity of reminiscence is created within the heap. If the programmer forgets to delete the reminiscence. This undeleted reminiscence within the heap is known as a reminiscence leak. The Efficiency of this system is lowered because the quantity of accessible reminiscence was lowered. To keep away from reminiscence leaks, reminiscence allotted on the heap ought to at all times be cleared when it’s now not wanted.
For extra info, seek advice from the article – Reminiscence Leak
35. What are command line arguments?
Arguments which are handed to the principle() perform of this system within the command-line shell of the working system are often called command-line arguments.
Syntax:
int important(int argc, char *argv[]){/*code which is to be executed*/}
For extra info, seek advice from the article – Command Line Arguments in C
36. What’s an auto key phrase?
Each native variable of a perform is named an computerized variable within the C language. Auto is the default storage class for all of the variables that are declared inside a perform or a block. Auto variables can solely be accessed inside the block/perform they’ve been declared. We are able to use them outdoors their scope with the assistance of pointers. By default auto key phrase include a rubbish worth.
For extra info, seek advice from the article – Storage Lessons in C
37. Write a program to print “Howdy-World” with out utilizing a semicolon.
C
|
|
For extra info, seek advice from the article – Howdy World in C
38. Write a C program to swap two numbers with out utilizing a 3rd variable.
C
|
|
Values earlier than swap are var1 = 50 and var2 = 60 Values after swap are var1 = 60 and var2 = 50
39. Write a program to test whether or not a string is a palindrome or not.
C
|
|
abba is a Palindrome
40. Clarify modifiers.
Modifiers are key phrases which are used to vary the which means of primary knowledge varieties in C language. They specify the quantity of reminiscence that’s to be allotted to the variable. There are 5 knowledge kind modifiers within the C programming language:
- lengthy
- brief
- signed
- unsigned
- lengthy lengthy
C Programming Interview Questions – Skilled Stage
41. Write a program to print factorial of a given quantity with the assistance of recursion.
C
|
|
Factorial of 5 is 120
42. Write a program to test an Armstrong quantity.
C
|
|
Enter Quantity 0 is an Armstrong quantity
43. Write a program to reverse a given quantity.
C
|
|
Output:
Enter Quantity to be reversed : Quantity After reversing digits is: 321
44. What’s the usage of an extern storage specifier?
The extern key phrase is used to increase the visibility of the C variables and capabilities within the C language. Extern is the brief identify for exterior. It’s used when a selected file must entry a variable from some other file. Extern key phrase will increase the redundancy and variables with extern key phrase are solely declared not outlined. By default capabilities are seen all through this system, so there isn’t any have to declare or outline extern capabilities.
45. What’s the usage of printf() and scanf() capabilities in C Programming language? Additionally, clarify format specifiers.
printf() perform is used to print the worth which is handed because the parameter to it on the console display screen.
Syntax:
print(“%X”,variable_of_X_type);
scanf() methodology, reads the values from the console as per the information kind specified.
Syntax:
scanf(“%X”,&variable_of_X_type);
In C format specifiers are used to inform the compiler what kind of knowledge can be current within the variable throughout enter utilizing scanf() or output utilizing print().
- %c: Character format specifier used to show and scan character.
- %d, %i: Signed Integer format specifier used to print or scan an integer worth.
- %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 Tackle Printing.
For extra info, seek advice from the article – Format Specifier in C
46. What’s close to, far, and big pointers in C?
- Close to Pointers: Close to pointers are used to retailer 16-bit addresses solely. Utilizing the close to pointer, we cannot retailer the handle with a measurement higher than 16 bits.
- Far Pointers: A far pointer is a pointer of 32 bits measurement. Nevertheless, info outdoors the pc’s reminiscence from the present phase will also be accessed.
- Large Pointers: Large pointer is often thought-about a pointer of 32 bits measurement. However bits positioned outdoors or saved outdoors the segments will also be accessed.
47. Point out file operations in C.
In C programming Primary File Dealing with Methods gives the essential functionalities that programmers can carry out towards the system.
48. Write a Program to test whether or not a linked listing is round or not.
C
|
|
For extra info, seek advice from the article – Round Linked Checklist
49. Write a program to Merge two sorted linked lists.
C
|
|
Merged Linked Checklist is: 2 3 5 10 15 20
For extra info, seek advice from the article – Merge Two Sorted Linked Checklist
50. What’s the distinction between getc(), getchar(), getch() and getche().
- getc(): The perform reads a single character from an enter stream and returns an integer worth (sometimes the ASCII worth of the character) if it succeeds. On failure, it returns the EOF.
- getchar(): Not like getc(), gechar() can learn from normal enter; it’s equal to getc(stdin).
- getch(): It’s a nonstandard perform and is current in ‘conio.h’ header file which is generally utilized by MS-DOS compilers like Turbo C.
- getche(): It reads a single character from the keyboard and shows it instantly on the output display screen with out ready for enter key.
For extra info, seek advice from the article – Distinction between getc(), getchar(), getch(), getche()