C Programming Interview Questions

Programming languages or concepts
0
 Top Most Asked Interview Question in Infosys Interview Round


1. What is C language?
         C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. The Procedural programming language is also known as the structured programming language is a technique in which large programs are broken down into smaller modules, and each module uses structured code.

2. When was C language developed?
           C language was developed in 1972 at bell laboratories of AT&T. 

3. What are the features of the C language?
        The main features of C language are given below:
  •     Simple: C is a simple language because it follows the structured approach, i.e., a program is broken into parts.
  • Portable: C is highly portable means that once the program is written can be run on any machine with little or no modifications.
  • Mid Level: C is a mid-level programming language as it combines the low- level language with the features of the high-level language.
  • Structured: C is a structured language as the C program is broken into parts.
  • Fast Speed: C language is very fast as it uses a powerful set of data types and operators.
  • Extensible: C is an extensible language as it can adopt new features in the future.
  • Memory Management: C provides an inbuilt memory function that saves the memory and improves the efficiency of our program.

4. What is the use of a static variable in C?

      Following are the uses of a static variable:
  • A variable which is declared as static is known as a static variable. The static variable retains its value between multiple function calls.
  • Static variables are used because the scope of the static variable is available in the entire program. So, we can access a static variable anywhere in the program.
  • The static variable is initially initialized to zero. If we update the value of a variable, then the updated value is assigned.
  • The static variable is used as a common value which is shared by all the methods.
  • The static variable is initialized only once in the memory heap to reduce the memory usage.

5. What is the use of the function in C?

    Uses of C function are:

  • C functions are used to avoid the rewriting the same code again and again in our program.
  • C functions can be called any number of times from any place of our program.

  • When a program is divided into functions, then any part of our program can easily be tracked.
  • C functions provide the reusability concept, i.e., it breaks the big task into smaller tasks so that it makes the C program more understandable.


6. What is an array in C?

    An Array is a group of similar types of elements. It has a contiguous memory location. It makes the code optimized, easy to traverse and easy to sort. The size and type of arrays cannot be changed after its declaration.

Arrays are of two types:

One-dimensional array: One-dimensional array is an array that stores the elements one after the another.

Syntax:

data_type array_name[size];  

Multidimensional array: Multidimensional array is an array that contains more than one array.

Syntax:

data_type array_name[size];  


7. What is recursion in C?

    When a function calls itself, and this process is known as recursion. The function that calls itself is known as a recursive function.

Recursive function comes in two phases:

1. Winding phase

2. Unwinding phase

Winding phase: When the recursive function calls itself, and this phase ends when the condition is reached.

Unwinding phase: Unwinding phase starts when the condition is reached, and the control returns to the original call.


8. What is a pointer in C?
    A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast. Whenever a variable is declared inside a program, then the system allocates some memory to a variable. The memory contains some address number. The variables that hold this address number is known as the pointer variable.

For example:
Data_type *p;  

The above syntax tells that p is a pointer variable that holds the address number of a given data type value.

            
9. What is a NULL pointer in C?
    A pointer that doesn't refer to any address of value but NULL is known as a NULL pointer. When we assign a '0' value to a pointer of any type, then it becomes a Null pointer

10. What is the usage of the pointer in C?
     Accessing array elements: Pointers are used in traversing through an array of integers and strings. The string is an array of characters which is terminated by a null character '\0'.
Dynamic memory allocation: Pointers are used in allocation and deallocation of memory during the execution of a program.
Call by Reference: The pointers are used to pass a reference of a variable to other function.
Data Structures like a tree, graph, linked list, etc.: The pointers are used to construct different data structures like tree, graph, linked list, etc.


11. What is a far pointer in C?
    A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer. A far pointer is a 32-bit pointer that obtains information outside the memory in a given section.


12. What is dangling pointer in C?
     If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory occupied by the first pointer while the first pointer still points to that memory location, the first pointer will be known as a dangling pointer. This problem is known as a dangling pointer problem.
    Dangling pointer arises when an object is deleted without modifying the value of the pointer. The pointer points to the deallocated memory.


13. What is pointer to pointer in C?

    In case of a pointer to pointer concept, one pointer refers to the address of another pointer. The pointer to pointer is a chain of pointers. Generally, the pointer contains the address of a variable. The pointer to pointer contains the address of a first pointer. 

14. What is static memory allocation?

     In case of static memory allocation, memory is allocated at compile time, and memory can't be increased while executing the program. It is used in the array.

  • The lifetime of a variable in static memory is the lifetime of a program.
  • The static memory is allocated using static keyword.
  • The static memory is implemented using stacks or heap.
  • The pointer is required to access the variable present in the static memory.
  • The static memory is faster than dynamic memory.
  • In static memory, more memory space is required to store the variable.

For example:  
                            int a[10];  

The above example creates an array of integer type, and the size of an array is fixed, i.e., 10.


15. What is dynamic memory allocation?
     In case of dynamic memory allocation, memory is allocated at runtime and memory can be increased while executing the program. It is used in the linked list.
The malloc() or calloc() function is required to allocate the memory at the runtime.
An allocation or deallocation of memory is done at the execution time of a program.
No dynamic pointers are required to access the memory.
The dynamic memory is implemented using data segments.
Less memory space is required to store the variable.

For example  

        int *p= malloc(sizeof(int)*10);  

The above example allocates the memory at runtime.

16. What functions are used for dynamic memory allocation in C language?


1. malloc()
The malloc() function is used to allocate the memory during the execution of the program.
It does not initialize the memory but carries the garbage value.
It returns a null pointer if it could not be able to allocate the requested space.
Syntax
ptr = (cast-type*) malloc(byte-size) // allocating the memory using malloc() function.  

2. calloc()
The calloc() is same as malloc() function, but the difference only is that it initializes the memory with zero value.
Syntax
ptr = (cast-type*)calloc(n, element-size);  // allocating the memory using calloc() function.  

3. realloc()
The realloc() function is used to reallocate the memory to the new size.
If sufficient space is not available in the memory, then the new block is allocated to accommodate the existing data.
Syntax
ptr = realloc(ptr, newsize); // updating the memory size using realloc() function.  
In the above syntax, ptr is allocated to a new size.

4. free():
        The free() function releases the memory allocated by either calloc() or malloc() function.
Syntax
free(ptr); // memory is released using free() function.  
The above syntax releases the memory from a pointer variable ptr.


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

    

calloc()

malloc()

Description

The malloc() function allocates

 a single block of requested memory.

The calloc() function allocates

 multiple blocks of requested

 memory.

Initialization

It initializes the content of the

 memory to zero.

It does not initialize the 

content of memory, so

 it carries the garbage value.

Number of 

arguments

It consists of two arguments.

It consists of only one argument.

Return value

It returns a pointer pointing to

 the allocated memory.

It returns a pointer pointing

 to the allocated memory.


18. What is the difference between call by value and call by reference in C?

Call by value

Call by reference

Description

When a copy of the value is passed

 to the function, then the original

 value is not modified.

When a copy of the value is passed 

to the function, then the original 

value is modified.

Memory location

Actual arguments and formal 

arguments are created in separate memory locations.

Actual arguments and formal 

arguments are created in the same 

 memory location.

Safety

In this case, actual arguments 

remain safe as they cannot

 be modified.

In this case, actual arguments 

are not reliable, as they are modified.

Arguments

The copies of the actual arguments 

are passed to the formal arguments.

The addresses of actual arguments 

are passed to their 

 respective formal arguments.


19. What is the difference between the local variable and global variable in C?

Basis for comparison

Local variable

Global variable

Declaration

A variable which is declared 

inside function or block is known as a local variable.

A variable which is declared outside

 function or block is known as a global variable.

Scope

The scope of a variable is 

available within a function in which they are declared.

The scope of a variable is 

available throughout the program.

Access

Variables can be accessed only by 

those statements inside a function in which they are declared.

Any statement in the entire

 program can access variables.

Life

Life of a variable is created when 

the function block is entered and destroyed on its exit.

Life of a variable exists until 

the program is executing.

Storage

Variables are stored in a stack

 unless specified.

The compiler decides the 

storage location of a variable.














   

 20. What is the structure?

The structure is a user-defined data type that allows storing multiple types of data in a single unit. It occupies the sum of the memory of all members.

The structure members can be accessed only through structure variables.

Structure variables accessing the same structure but the memory allocated for each variable will be different.

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


21. What is a union?

     The union is a user-defined data type that allows storing multiple types of data in a single unit. However, it doesn't occupy the sum of the memory of all members. It holds the memory of the largest member only.

In union, we can access only one variable at a time as it allocates one common space for all the members of a union.

Syntax of union

union union_name  

{  

Member_variable1;  

Member_variable2; 


Member_variable n;  

}[union variables];  



22. What is an auto keyword in C?

       In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.


23. What is the purpose of sprintf() function?

       The sprintf() stands for "string print." The sprintf() function does not print the output on the console screen. It transfers the data to the buffer. It returns the total number of characters present in the string.

Syntax

int sprintf ( char * str, const char * format, ... );  



24. Can we compile a program without main() function?

    Yes, we can compile, but it can't be executed.

But, if we use #define, we can compile and run a C program without using the main() function. For example:

#include<stdio.h>    

#define start main    

void start() {    

   printf("Hello");    

}    


25. What is a token?

    The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest individual unit in a program. C has the following tokens:

Identifiers: Identifiers refer to the name of the variables.

Keywords: Keywords are the predefined words that are explained by the compiler.

Constants: Constants are the fixed values that cannot be changed during the execution of a program.

Operators: An operator is a symbol that performs the particular operation.

Special characters: All the characters except alphabets and digits are treated as special characters.


26. What is command line argument?
        The argument passed to the main() function while executing the program is known as command line argument. For example:
main(int count, char *args[]){  
//code to  be executed  
}  

27. What is the difference between getch() and getche()?

    The getch() function reads a single character from the keyboard. It doesn't use any buffer, so entered data will not be displayed on the output screen.

The getche() function reads a single character from the keyword, but data is displayed on the output screen. 


28. What is typecasting?

The typecasting is a process of converting one data type into another is known as typecasting. If we want to store the floating type value to an int type, then we will convert the data type into another data type explicitly.

Syntax

(type_name) expression;  


29. What is a built-in function in C?

                The most commonly used built-in functions in C are sacnf(), printf(), strcpy, strlwr, strcmp, strlen, strcat, and many more.
Built-function is also known as library functions that are provided by the system to make the life of a developer easy by assisting them to do certain commonly used predefined tasks. For example, if you need to print output or your program into the terminal, we use printf() in C.


30. How can a string be converted to a number?

                he function takes the string as an input that needs to be converted to an integer.
int atoi(const char *string)
Return Value:
On successful conversion, it returns the desired integer value
If the string starts with alpha-numeric char or only contains alpha-num char, 0 is returned.


31. Why doesn’t C support function overloading?

                After you compile the C source, the symbol names need to be intact in the object code. If we introduce function overloading in our source, we should also provide name mangling as a preventive measure to avoid function name clashes. Also, as C is not a strictly typed language many things(ex: data types) are convertible to each other in C. Therefore, the complexity of overload resolution can introduce confusion in a language such as C.
When you compile a C source, symbol names will remain intact. If you introduce function overloading, you should provide a name mangling technique to prevent name clashes. Consequently, like C++, you'll have machine-generated symbol names in the compiled binary.
Additionally, C does not feature strict typing. Many things are implicitly convertible to each other in C. The complexity of overload resolution rules could introduce confusion in such kind of language


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

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

33. What are the advantages of Macro over function?

                Macro on a high-level copy-paste, its definitions to places wherever it is called. Due to which it saves a lot of time, as no time is spent while passing the control to a new function and the control is always with the callee function. However, one downside is the size of the compiled binary is large but once compiled the program comparatively runs faster.

34. Specify different types of decision control statements?

                    All statements written in a program are executed from top to bottom one by one. Control statements are used to execute/transfer the control from one part of the program to another depending on the condition.
If-else statement.
normal if-else statement.
Else-if statement
nested if-else statement.
Switch statement.


35. What is call by reference in functions?

            When we caller function makes a function call bypassing the addresses of actual parameters being passed, then this is called call by reference. In incall by reference, the operation performed on formal parameters affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters.


36. What is pass by reference in functions?

                In Pass by reference, the callee receives the address and makes a copy of the address of an argument into the formal parameter. Callee function uses the address to access the actual argument (to do some manipulation). If the callee function changes the value addressed at the passed address it will be visible to the caller function as well.


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

                C is a language known for its low-level control over the memory allocation of variables in DMA there are two major standard library malloc() and free. The malloc() function takes a single input parameter which tells the size of the memory requested It returns a pointer to the allocated memory. If the allocation fails, it returns NULL. The prototype for the standard library function is like this:
void *malloc(size_t size);
The free() function takes the pointer returned by malloc() and de-allocates the memory. No indication of success or failure is returned. The function prototype is like this: 
void free(void *pointer);
There are 4 library functions provided by C defined under <stdlib.h> header file to facilitate dynamic memory allocation in C programming. They are:
malloc()
calloc()
free()
realloc()

Post a Comment

0Comments

Post a Comment (0)
close