Advanced
Data Structures Algorithms & System Design(HLD+LLD)
by Logicmojo

Cracking FAANG companies interviews with a few months of preparation

Learn Advanced Data Structures, Algorithms & System Design

Online live classes from 4 to 7 months programs

Get job assistance after course completion

Download Course Brochure

C Interview Questions C Interview Questions

C Programming Interview Questions

Most technical rounds that employers perform include C programming interview questions. The purpose of probing a candidate on C programming is to see how well he or she understands programming and the fundamental ideas of the language. This article contains a variety of C programming interview questions aimed to provide you with a basis on which to develop. Because of its structure, high-level abstraction, machine independence, and other features, C is one of the most popular computer languages today. Because the C programming language was created to design the UNIX operating system, it is closely associated with UNIX, which is one of the most widely used network operating systems today and the brains behind the internet data superhighway.

To improve your chances of performing well in the interviews, go through all of the questions. The questions will focus on C's fundamentals and core concepts.

So, let’s dive deep into the surplus of useful interview questions on C.


Learn More️

C Interview Questions and Answers

What is the C programming language?

C is a procedural and mid-level programming language. The structured programming language, often known as the procedural programming language, is a strategy for breaking down big programmes into smaller modules, each of which employs structured code. This method reduces the chances of misinterpretation and inaccuracy. More information is available.


C is referred to as a mid-level programming language give reason?

C is referred to as a mid-level programming language because it bridges the gap between low-level and high-level programming. We can utilise the C programming language for both system programming and application programming to create a menu-driven customer-driven billing system.

What separates the C programming language?

The following are the primary characteristics of the C programming language:

Simple: C is a simple language because it employs a structured approach, in which a programme is divided into sections.

Portable: C is highly portable, which means that once a programme is written, it may be run on almost any machine with minimal changes.

C is a mid-level programming language because it combines the advantages of a low-level and a high-level language.

Structured: Because C programmes are divided into pieces, it is a structured language.

Memory Management: C includes an inbuilt memory function that saves memory and improves the efficiency of our software.

C is extensible, which means it can be expanded in the future to provide new features.

What data types are the most popular in C?

int: Represent the number (integer)

float: Number that are in fraction part

Double: Double-precision floating-point value

Char: Single Character

Void: Special type without any value

What is a built-in function in C?

Scanf(), printf(), strcpy, strlwr, strcmp, strlen, strcat, and many more are among the most often used built-in functions in C.Built-in functions, often known as library functions, are given by the system to aid developers in performing certain commonly performed predefined activities. In C, for example, we use printf() to print output or your programme to the terminal.

What are the functions printf() and scanf() used for?

printf() is a function that prints integer, character, float, and string values on the screen.

The format specifiers are as follows:

%d: It's a format specifier that tells the printer how to print an integer value.

%s: It's a format specifier that tells the printer how to print an string.

%c: It's a format specifier that tells the printer how to print an character value.

%f: It's a format specifier that tells the printer how to print an floating value.

scanf(): The scanf() function is used to receive user input.

What is the purpose of the #line in C?

#line is a preprocessor in C that is used to re-set the line number in code that takes a parameter as a line number. Here's an illustration of what I'm referring to.

#include <stdio.h>	

int main()
{	
	
	printf("Hello world\n"); // this is line 6
	printf("Line: %d\n",__LINE__);	// printing line number, line 7

	#line 23	//reseting to 23, although next line number is line 10. 
	printf("Line: %d\n",__LINE__);	// printing line number
	printf("Line: %d\n",__LINE__);	
	printf("Line: %d\n",__LINE__);

	// let's try with filename (main.c) as well. 
	
	printf( "Line: %d, File: %s\n", __LINE__, __FILE__ );
	// now we use line to reset filename to "new_filename.c"
	// line number is set to 83
	#line 83 "new_filename.c"
	printf( "Line: %d, File: %s\n", __LINE__, __FILE__ );
	
	return 0;
}

Define stdio.h.

It's a C header file including prototypes and definitions for commands like scanf and printf.

What's the distinction between a local and a global variable in C?

The distinctions between a local variable and a global variable are as follows:

Local VariableGlobal Variable
A variable declared within a function or block is known as a local variable.A global variable is a variable that is declared outside of a function or block.
A variable's scope is available within the function in which it is declared.The scope of a variable can be accessed at any time during the programme.
Variables can only be retrieved from within the function in which they are declared.Variables can be accessed by any statement in the programme.
A variable's life begins when the function block is entered and ends when it is exited.A variable's life lasts till the programme is run.

What are static variables and functions?

Static Variables and Static Functions are variables and functions that are declared using the keyword Static. The scope of variables declared with the Static keyword is limited to the function in which they are declared.

What is the difference between a static and a global integer declaration?

The distinction lies in the scope of the two. A really global variable is one that has a global scope and can be seen across your programme.

#include <stdio.h> 
 
int my_global_var = 0; 
 
int 
main(void) 
 
{ 
  printf("%d\n", my_global_var); 
  return 0; 
} 


If you have a multi-file project, you'll need a "extern int global temp; " in other source files to make global temp available to everything in your programme.

The variables of a static variable have a local scope, but they are not allocated in the stack region of memory. Although it, like global variables, exists in the.bss part of your produced binary, it can have less than global scope.

#include <stdio.h> 
 
int 
myfunc(int val) 
 
{ 
    static int my_static_var = 0; 
 
    my_static_var += val; 
    return my_static_var; 
} 
 
int 
main(void) 
 
{ 
   int myval; 
 
   myval = myfunc(1); 
   printf("first call %d\n", myval); 
 
   myval = myfunc(10); 
 
   printf("second call %d\n", myval); 
 
   return 0; 
}


What are static variables and functions?

Static variables are sometimes known as Class Variables since they make only one copy of them when they are declared. They keep the value that was assigned in the previous scope and do not re-initialize the variables in the current scope.

During the execution of the code, static variables remain in memory.

In C, static functions essentially limit the method's scope to the relevant file. These functions can be called even if the object hasn't been initialised.

Static functions frequently improve the code's usability, allowing it to be reused multiple times.

What are the types of decision control statements?

Every statement written in a programme is performed one by one from top to bottom. Depending on the situation, control statements are used to execute/transfer control from one portion of the programme to another.

if-else statement

nested if-else statement

switch statement

What is call by reference in functions?

Call by reference occurs when a caller function makes a function call without specifying the addresses of the actual parameters being provided. Because all actions on the value stored in the address of real parameters effect the value of actual parameters in incall by reference, the operation done on formal parameters affects the value of actual parameters.

What is pass by reference in functions?

The callee receives the address and copies the address of an argument into the formal parameter in Pass by reference. The address is used by the callee function to obtain the actual argument (to do some manipulation). If the callee function modifies the value addressed at the supplied address, the caller function will be notified.

What is the difference between calling a function by value and calling a function by reference in C?

Call by valueCall by reference
The original value is not changed when a copy of the value is supplied to the function.The original value is updated when a copy of the value is supplied to the function.
Arguments are constructed in different memory locations for actual and formal arguments.Arguments are created in the same memory location as formal arguments.
Actual arguments are safe in this scenario because they can't be changed.Actual arguments aren't reliable in this scenario because they've been tampered with.
The formal arguments receive copies of the actual arguments.Actual argument addresses are supplied to their respective formal arguments.



Example of call by value

#include <stdio.h>  
void change(int,int);  
int main()  
{  
    int a=10,b=20;  
    change(a,b); //calling a function by passing the values of variables.  
    printf("Value of a is: %d",a);  
    printf("\n");  
    printf("Value of b is: %d",b);  
    return 0;  
}  
void change(int x,int y)  
{  
    x=13;  
    y=17;  
}  


Example of call by reference

#include <stdio.h>  
void change(int*,int*);  
int main()  
{  
    int a=10,b=20;  
    change(&a,&b); // calling a function by passing references of variables.  
    printf("Value of a is: %d",a);  
    printf("\n");  
    printf("Value of b is: %d",b);  
    return 0;  
}  
void change(int *x,int *y)  
{  
    *x=13;  
    *y=17;  
}  

What is the best way to store a negative integer in C?

The steps below must be followed in order to store a negative integer. Calculate the two's complement of a positive integer that is the same.

Eg: 1011 (-5)
Step-1 βˆ’ One’s complement of 5: 1010
Step-2 βˆ’ Add 1 to the above to get 1011, which is a -5.

In C, what is the purpose of a static variable?

A static variable is a variable that has been declared as static. The value of the static variable is preserved across numerous function calls.

Because the scope of a static variable is available throughout the programme, it is used. As a result, a static variable can be accessed from anywhere in the programme.

The static variable is set to zero by default. When we change the value of a variable, the new value is assigned to it.

To save memory, the static variable is only initialised once in the memory heap.

What is a pointer in C?

A pointer is a variable that represents a value's address. It optimises the code and speeds up the performance. When a variable is declared in a programme, the system allocates memory for that variable. Some address number is stored in the memory. The pointer variable refers to the variables that store the address number.

Example: Data_type *p;

The syntax above indicates that p is a pointer variable that stores the address number of a data type value.

What is the usage of the pointer in C?

Accessing array elements: When traversing an array of numbers and strings, pointers are utilised. The string is a character array that ends with the null character '0'.

Dynamic memory allocation: During the execution of a programme, pointers are utilised to allocate and deallocate memory.

Pointers are used to pass a reference to a variable to another function via call by reference.

Data Structures such as a tree, graph, linked list, and so on: Pointers are used to build various data structures such as trees, graphs, linked lists, and so on.

Difference between const char* p and char const* p?

A pointer to a const char is const char* p.

A pointer to a char const is char const* p.

It's the same since const char and char const are the same.

Make a distinction between actual and formal parameters.

Actual parameters are those that are sent from the main function to the subdivided function, while Formal parameters are those that are declared at the Subdivided function's end.

What is a NULL pointer in C?

A NULL pointer is a pointer that does not refer to any address other than NULL. When we give a pointer of any type the value '0,' it becomes a Null pointer.

What is Preprocessor?

A preprocessor directive is a built-in predefined function or macro that acts as a directive to the compiler and is run before the real C programme.

What is a far pointer in C?

A far pointer is a pointer that can reach all 16 segments (full residence memory) of RAM. A far pointer is a 32-bit pointer that gets data from outside memory in a certain area.

What is dangling pointer in C?

When a pointer points to any memory location, but another pointer deletes the memory occupied by the first pointer while the first pointer continues to point to that memory location, the first pointer is referred to as a dangling pointer. A dangling pointer problem is the name for this situation. When an object is destroyed without changing the value of the pointer, a dangling pointer occurs. The pointer points to the memory that has been deallocated.

Let's take an example

#include<stdio.h>  
        void main()  
        {  
                int *ptr = malloc(constant value); //allocating a memory space.  
                free(ptr); //ptr becomes a dangling pointer.  
        }  
        



In the preceding example, memory is first allocated to the pointer variable ptr and then released from the pointer variable. ptr, the pointer variable, is now a dangling pointer.

The problem of a dangling pointer can be solved by giving the dangling pointer a NULL value. Let's look at an example to help you understand:

#include<stdio.h>  
              void main()  
              {  
                      int *ptr = malloc(constant value); //allocating a memory space.  
                      free(ptr); //ptr becomes a dangling pointer.  
                      ptr=NULL; //Now, ptr is no longer a dangling pointer.  
              }  
        



After deallocating memory from a pointer variable, ptr is assigned to a NULL value in the example above. This signifies that ptr doesn't point to anything in memory. As a result, the pointer is no longer dangling.

What is pointer to pointer in C?

One pointer refers to the address of another pointer in a pointer to pointer notion. A chain of pointers is a pointer to pointer. The address of a variable is usually stored in the pointer. The address of a first pointer is contained in the pointer to pointer.

Let's take an example:

#include <stdio.h>  
 int main()  
{  
    int a=10;  
    int *ptr,**pptr; // *ptr is a pointer and **pptr is a double pointer.  
    ptr=&a;  
    pptr=&ptr;  
    printf("value of a is:%d",a);  
    printf("\n");  
    printf("value of *ptr is : %d",*ptr);  
    printf("\n");  
    printf("value of **pptr is : %d",**pptr);  
    return 0;  
}  



pptr is a double pointer referring to the address of the ptr variable, and ptr points to the address of the 'a' variable in the example above.

What are the advantages of Macro over function?

Macro on a high-level copy-paste, its definitions to wherever it's called As a result, it saves a lot of time because the control is always with the callee function and no time is spent passing it to a new function. However, one disadvantage is that the built binary is enormous in size, but once compiled, the application runs much faster.

What is Dynamic memory allocation in C? Name the dynamic allocation functions?

In DMA, there are two key standard libraries malloc() and free. C is a language noted for its low-level control over memory allocation of variables. The malloc() function requires a single input parameter that specifies the amount of memory to allocate. It gives you a pointer to the allocated RAM. It returns NULL if the allocation fails. This is the prototype for the standard library function:

void *malloc(size_t size);

The free() function de-allocates memory using the reference returned by malloc(). There's no way of knowing if you're going to be successful or not. This is how the function prototype looks:

void free(void *pointer);

To help with dynamic memory allocation in C programming, C provides four library functions that are defined in the stdlib.h header file. They are as follows:

malloc()
calloc()
free()
realloc()

What is static memory allocation?

Memory is assigned at compile time in the case of static memory allocation, and it cannot be increased while the application is running. It's a component of the array.

A program's lifespan is equal to the lifetime of a variable in static memory.

The static keyword is used to allocate static memory.

To access the variable in the static memory, you'll need a pointer.

Dynamic memory is slower than static memory.

For example:
int a[10];

What is typedef?

typedef is a C keyword that is used to define aliases/synonyms for existing C types. We employ typedefs to simplify the existing type declaration syntax in most circumstances. Or to give a kind specific descriptive names.

typedef (existing-type) (new-type-identifiers);

The existing complicated type definition is given an alias name via typedef. You may easily construct an alias for any type with typedef. Typedef will shorten your code, whether it's a simple integer, a sophisticated function pointer, or a structure declaration.

What is the difference between malloc() and calloc()?

calloc()malloc()
The calloc() function allocates many memory blocks to a single variable.The malloc() function allocates a single memory block of a given size.
calloc() only accept two argument.Malloc() only accepts one argument.
calloc() is slower than malloc.malloc() is faster than calloc.
Calloc() allocates a memory block with a value of zero.Malloc() allocates a memory block with a garbage value.


What is the structure?

The structure is a user-defined data type that allows many types of information to be stored in a single unit. It takes up the total amount of RAM available to all members.

Structure variables are the sole way to get to the members of the structure.

Although structural variables will access the same structure, the memory allocated to each variable will differ.

Syntax:

struct structure_name  
{  
  Member_variable1;  
 Member_variable2  
.  
.  
}[structure variables];  


What is a union?

The union is a user-defined data type that allows many types of data to be stored in the same unit. It does not, however, take up the total amount of RAM available to all members. It only has the memories of the eldest member.

Because it allocates one common place for all members of a union, we can only access one variable at a time in union.

Syntax:

union union_name  
{  
Member_variable1;  
Member_variable2;  
.  
.  
Member_variable n;  
}[union variables];  



What is the difference between struct and union in C?

A struct is a collection of complicated data structures stored in a memory block, each with its own memory location to make them all available at the same time.

In contrast, in the union, all of the member variables are kept in the same memory address, which means that changing the value of one member variable will change the value of all other members.

/* struct & union definations*/
struct bar {
	int a;	// we can use a & b both simultaneously
	char b;
}	bar;

union foo {
	int a;	// we can't use both a and b simultaneously
	char b;
}	foo;

/* using struc and union variables*/

struct bar y;
y.a = 3;	// OK to use
y.b = 'c'; // OK to use

union foo x;
x.a = 3; // OK
x.b = 'c'; // NOl this affects the value of x.a!



What is the difference between #include "..." and #include <...>?

The difference in practise is the position where the preprocessor looks for the included file.

The C preprocessor looks for #include filename> in the predefined list of system directories first, then in the user-specified directories (we can use -I option to add directories to the mentioned predefined list).

For #include "filename" the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the #include "filename" form. This method is normally used to include programmer-defined header files.

What is the difference between the = symbol and == symbol?

In mathematical procedures, the = symbol is frequently employed. It's used to give a variable a name and a value. The == symbol, on the other hand, is a relational operator that compares two values. It is sometimes known as "equal to" or "equivalent to."

When is the β€œvoid” keyword used in a function?

When you declare functions, you must decide whether they will return a value or not. If the function will not return a value, such as when the function's purpose is to display certain outputs on the screen, "void" should be inserted in the function header's leftmost section. When a return value is expected after a function is executed, the return value's data type is used instead of "void."

What is an auto keyword in C?

Every function's local variable is referred to as an automatic (auto) variable in C. Local variables are variables that are declared within the function block. A local variable is also referred to as an auto variable. The inclusion of an auto keyword before a variable's data type is optional. The local variable has a garbage value if no data is placed in it.

What is a token?

The Token serves as a unique identifier. It could be a constant, a keyword, or a string literal, for example. In a programme, a token is the smallest individual unit. The tokens in C are as follows:

Identifiers: The names of the variables are referred to as identifiers.

Keywords: The compiler defines keywords as preset words that are described.

Operator: An operator is a symbol that performs a certain task.

Special Characters: Except for alphabets and numerals, all characters are classified as special characters.

What is typecasting?

Typecasting is the process of transforming one data type to another. We must explicitly transform the data type into another data type if we wish to store the floating type value as an int type.

Syntax: (type_name) expression;

What is a memory leak? How to avoid it?

When we assign a variable, it takes up space in our RAM (either heap or RAM) based on the size of the data type; however, if a programmer uses memory available on the heap and forgets to delta it, all of the memory available on the ram will be occupied at some point, leaving no memory available, which can result in a memory leak.

int main()
{
    char * ptr = malloc(sizeof(int));
    
    /* Do some work */
    /*Not freeing the allocated memory*/
    return 0;
}



You can avoid memory leaks by tracing all of your memory allocations and planning ahead where you want to destroy (in a good sense) that memory and inserting delete there. Another option is to link GNU compilers to C++ smart pointers in C.

Differentiate Source Codes from Object Codes

The difference between Source Code and Object Code is that Source Code is a collection of computer instructions written in a human-readable programming language, whereas Object Code is a sequence of machine-readable statements that is produced after the compiler or assembler converts the Source Code.

The next aspect to mention regarding Object Code is how changes are reflected. When the Source Code is changed, it must be compiled each time to reflect the changes in the Object Code.

When is the "void" keyword used in a function

The term "void" refers to a data type that contains no data at all. A function that returns nothing is the most obvious application of this:

void PrintHello() 
{ 
	printf("Hello\n"); 
	return;		// the function does "return", but no value is returned 
} 


We've defined a function here, and every function has a return type. We've said the return type is "void" in this example, which indicates "no data at all" is returned.

A void pointer is another use for the void keyword. A void pointer points to a memory address where the data type is unknown when the variable is defined. You can also define a function with a return type of void* or void pointer, which means "we don't know what it will return at compile time." Let's have a look at an example of this.

void MyMemCopy(void* dst, const void* src, int numBytes) 
{ 
	char* dst_c = reinterpret_cast<char*>(dst); 
	const char* src_c = reinterpret_cast<const char*>(src); 
	for (int i = 0; i < numBytes; ++i) 
		dst_c[i] = src_c[i]; 
} 



What is l-value and r-value?

Because of their appearance in the statement, the terms l-value and r-value were coined. The L-value appears on the assignment operator's left side. In general, a case variable is thought of as an L-value to which we will assign certain values. On the right side of the assignment operator, R-value appeared. Example: In the expression "x = 15," x represents the l-value and 15 represents the r-value.

Difference b/w Entry controlled and Exit controlled loop?

The loop condition is checked first in an Entry controlled loop, then the body of the loop is run, but in an Exit controlled loop, the body of the loop is executed first, then the condition is checked.

Entry Controlled Loops are : for, while
Exit Controlled Loop is : do while

What's the difference between the functions puts() and printf()?

PutsPrintf()
You can only print a string on the console output screen with the puts() function.On the console output screen, the printf() function can print strings as well as mixed sorts of variables.
The puts() function prints a string and adds one new character, which is a new line character, at the end of it (\n).The printf() function prints text, string, text+ value, and so on, without appending any extra characters.
It is easier to implement than printf.When compared to puts, it is more difficult to implement.


What is the difference between ++a and a++?

The prefix increase is '++a.' This increments the first value of the variable "a" before assigning it to the variable a. "a++," on the other hand, is known as a post increment. Following the execution of the line, the value stored in the variable "a" is increased.

What is the difference between getch() and getche() functions?

Both routines will accept a user-supplied character input value. The key that was pushed will not appear on the screen when using getch(); instead, it will be immediately recorded and allocated to a variable. When you call getche(), the user's pushed key will be displayed on the screen while also being set to a variable.

What is the use of a semicolon (;) at the end of every program statement?

It has to do with the code parsing and compilation process. A semicolon serves as a delimiter, allowing the compiler to determine where each statement terminates and divide it into smaller components for syntax testing.

How do you search data in a data file using random access method?

To do random access input/output on a file, use the fseek() method. After the fopen() function has opened the file, the fseek() function will need three parameters to work: a file pointer to the file, the number of bytes to search, and the file's point of origin.

Which Is Better To Use A Macro Or A Function?

The answer is contingent on the context for which you are writing code. Because their equivalent code is put directly into your source code at the point when the macro is invoked, macros have the distinct advantage of being more efficient (and faster) than functions. There is no overhead when utilising a macro compared to calling a function. Macros, on the other hand, are often small and incapable of handling massive, complicated coding constructions. For this type of case, a function is more appropriate.

Macros are also expanded inline, which implies that the code is duplicated for each occurrence of the macro. As a result, when you use macros instead of functions, your code may be slightly longer. As a result, determining whether to use a macro or a function boils down to weighing the benefits of faster programme speed versus smaller programme size. In general, macros should be used to replace tiny, repeating code parts, while functions should be used for bigger coding jobs requiring several lines of code.

What Is The Benefit Of Using An Enum Rather Than A #define Constant?

The use of an enumeration constant (enum) over the usual symbolic constant approach of #define offers a number of advantages. Lower maintenance requirements, enhanced programme readability, and improved debugging capabilities are just a few of the benefits.

Write a program to print "hello world" without using a semicolon?

#include<stdio.h>      
void main(){      
 if(printf("hello world")){} // It prints the ?hello world? on the screen.  
}     

What is the difference between a null pointer and void pointer?

When a pointer is declared without a value, it refers to null, which indicates that it does not point to a valid place. The term "void pointer" refers to a pointer that has no data type associated with it. Any type of variable can be referred to by this term.

What do you mean by while (0) and while (1)?

While(0) indicates that the loop will not be executed since it contains a false condition, while (1) indicates that the condition is always true and the loop will continue to run until an exit or break statement is encountered. In condition, every non-zero number is a true condition, but only 0 is a false condition.

Can you explain the use of extern storage specifier?

This specifier is used to declare objects that are shared across several source files. A variable defined using the extern keyword is stored externally, has a global scope, and can be used by any source file in the directory.

What do you mean by sequential access?

When data is stored in files in a sequential fashion, we must also read each file one by one to obtain the relevant information when retrieving it.

πŸš€Conclusion

This page has finally reached its conclusion. With the information on this page, you should be able to construct your own programmes with some research, and modest projects are actually encouraged for honing your programming skills. There's no way to cover everything you need to know to be a successful programmer in a single course. In truth, whether you're a seasoned professional developer or a complete beginner, programming is a never-ending learning process.

TEST YOUR KNOWLEDGE!


1. What is #include (stdio.h)?




2. Which of the following return-type cannot be used for a function in C?




3. scanf() is a predefined function in______header file




4. How is the 3rd element in an array accessed based on pointer notation?



5. How are String represented in memory in C?




6. Which of the following is an exit controlled loop?



7. Which of the following are not standard header files in C?




8. In which of the following languages is function overloading not possible?




9. Which of the following will occur if we call the free() function on a NULL pointer?




10. Which data structure is used to handle recursion in C?






HAVE A QUESTION? GIVE US A CALL OR CONTACT US - WE'D LOVE TO HEAR FROM YOU

PHONE: +91 80889-75867

WhatsApp : Click Here...