Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C Interview Question And Answer for fresher

C Interview Questions And Answers for Freshers

“C Interview Questions And Answers for Freshers” is given on this page.

PrepInsta provides you Top C Interview Questions and Answers for Freshers . You will also get information related to Most commonly asked C Interview Questions and Answers 2020 for Freshers.

Technical Interview Round is considered as one of the toughest round during the whole recruitment process. You will be judged on the basis of your Technical knowledge.

A list of Top 25 frequently asked C programming interview questions and answers are given below. Click on the button first to learn more about C.

C Interview Question And Answer for fresher

All About C Language

C is a general purpose high-level language most popular amongst coders, it is the most compatible, easy to learn and effective programming language. It is a successor of B language that came out way back in the 70s and has been the most popular programming language ever since.

Use of C Language-

  • We can develop compilers in C language.
  • Network Drivers are also created using C language.
  • Text Editors are developed using the C language like Notepad++,gedit e.t.c.
  • Database Management System are developed using the C language like oracle database.

To know more about C click on the button below.

Commonly Asked C Interview Questions and Answers for Freshers

1. Why C is a Middle level Language?

Solution:- A middle level language is one that binds the gap between machine level language and high level language. C language is one such programming language.  It finds its application in both, system programming (like as operating system) as well as application programming (like as spreadsheet). Middle level language are more related to machine as well as human language. So that’s why it is called “Middle level language”.

  2. What are the key features in the C programming language?

Solution:-

Features are as follows:

  • Portability: C language has no dependency on the platform.
  • Modularity: Scope of breaking bigger programs to smaller reusable modules.
  • Flexibility: The possibility of a programmer to control the language.
  • Speed: C compiles and executes with high speed when compared with other high-level languages as it comes with support for system programming and hence it .
  • Extensibility: Possibility to add new features by the programmer.

3. How will you print “Hello World” without a semicolon?

Solution:-

#include 
int main(void)
{
if(printf("Hello World")){
}
}

 4. Why is C called the Mother of all Languages?

Solution:-

Many core concepts and data structures like arrays, lists, functions, strings, etc. were introduced through C Language. Many of the new programming languages have been developed on the basic concepts of C Language.

5. How Do You Print An Address?

Solution:-

Using the %P specification in the printf() ( or fprintf() or sprintf() ) statement. This prints a void pointer (void*). This format might change from compiler to compiler. 

If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*:

printf (“%Pn”, (void*) buffer);

 6. What is the equivalent code of the following statement in WHILE LOOP format?

for (int a=1; a<=100; a++)

printf ("%d\n", a * a);

Solution:-

int a=1;

while (a<=100) {

printf ("%d\n", a * a);

a++;

}

 7. How is the null pointer different from a void pointer?

Solution:-

null pointer is basically a null value assigned to a pointer of any data type whereas a void pointer is a data type which remains void as long as an address of a data type is not assigned to it.

The data type of the pointer is nothing but the type of data stored at the memory location where the pointer is pointed. When you are not sure about the type of data that is going to store at a particular memory location, you need to create the void pointer.

Null pointer does not contain a reference of any variable/value. Hence we may state that it has a NULL as its value making it a null pointer.

void pointer is always of type void *

#include #include 
void main()

    int a = 22;
    int *notnullpointer = &a;
    int *nullpointer1;                                                       // Null because there is no initialization.
    int *nullpointer2 = 0;                                                // Null because initialized with 0.
    if (notnullpointer == 0) printf ("\nNot null pointer is null.");
    else printf ("\nNot null pointer is not null.");
    if (nullpointer1 == 0) printf ("\nNull pointer 1 is null.");
    else printf ("\nNull pointer 1 is not null.");
    if (nullpointer2 == 0) printf ("\nNull pointer 2 is null.");
    else printf ("\nNull pointer 2 is not null.");
    printf ("\nNot null pointer has stored the address %d.", &notnullpointer);
    printf ("\nNull pointer 1 has no valid address.");
    printf ("\nNull pointer 2 has no valid address.");
}

Output:

Not null pointer is not null.
Null pointer 1 is null.
Null pointer 2 is null.
Not null pointer has stored the address 2054804456.
Null pointer 1 has no valid address.
Null pointer 2 has no valid address.

Unlike NULL pointers, void pointers are general-purpose pointers that do not have any data type associated with them. Void pointers can contain the address of any type of variable. So, the data type that a void pointer points to can be anything.

8.What Is The Heap?

Solution:-

The heap is where malloc(), calloc(), and realloc() get memory.

Accessing memory from the heap is far more slower that accessing it from a stack. But the other point being that heap is a bit more flexible than a stack.

Getting memory from the heap is much slower than getting it from the stack. On the other hand, the heap is much more flexible than the stack. Memory can be allocated at any time and deallocated in any order. Such memory isn’t deallocated automatically; you have to call free ().

Recursive data structures are almost always implemented with memory from the heap. Strings often come from there too, especially strings that could be very long at runtime. If you can keep data in a local variable (and allocate it from the stack), your code will run faster than if you put the data on the heap. Sometimes you can use a better algorithm if you use the heap—faster, or more robust, or more flexible. It’s a tradeoff.

If memory is allocated from the heap, it’s available until the program ends. That’s great if you remember to deallocate it when you’re done. If you forget, it’s a problem. A “memory leak” is some allocated memory that’s no longer needed but isn’t deallocated. If you have a memory leak inside a loop, you can use up all the memory on the heap and not be able to get any more. (When that happens, the allocation functions return a null pointer.) In some environments, if a program doesn’t deallocate everything it allocated, memory stays unavailable even after the program ends.

9. Is It Better To Use Malloc () Or Calloc ()?

Solution:-

Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big:

void *malloc( size_t size );

calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory at least big enough to hold them all:

void *calloc( size_t numElements,size_t sizeOfElement );

There’s one major difference and one minor difference between the two functions. The major difference is that malloc () doesn’t initialize the allocated memory. The first time malloc () gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you’re going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you’re going to use as a pointer is set to all zero bits. That’s usually a null pointer, but it’s not guaranteed. Anything you’re going to use as a float or double is set to all zero bits; that’s a floating-point zero on some types of machines, but not on all.

The minor difference between the two is that calloc () returns an array of objects; malloc () returns one object. Some people use calloc () to make clear that they want an array.

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

Solution:-

Basis for comparisonLocal variableGlobal variable
DeclarationA variable that is declared inside a function or block is known as a local variable.A variable that is declared outside function or block is known as a global variable.
ScopeThe scope of a variable is within a function in which they are declared.The scope of a variable is throughout the program.
AccessVariables can be accessed only by those statements inside a function in which they are declared.Any statement in the entire program can access variables.
LifeLife 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.
StorageVariables are stored in a stack unless specified.The compiler decides the storage location of a variable.

11. What are the command line Arguments

Solution:-

A command-line argument is a parameter supplied to the program when it is invoked. The command-line argument is an important concept in C programming. It is mostly used when you need to control your program from the outside. Command-line arguments are passed to the main() method. The parameters are always strings held in the second argument (below in args) of the function which is an array of character pointers. The first argument represents the count of arguments (below in count) and updated automatically by the operating system.

main( int count, char *args[]) {
}

12.What is the general form of function in C?

Solution:-

The function in C contains consists of four main sections.


<return_type>
<function_name>( <parameter list> )
{
<body of the function>
}
  • Return Type: Every return value has a data type which is defined as Return type.
  • Function Name: The name of the function is important. It must have a meaningful name that describes the operation that the function does.
  • Parameters: The input values for the function that are used to perform the required action.
  • Function Body: All the task commands compiled for performing a special task for which the function was made.

13.What is memory leak? Why it should be avoided?

Solution:-

A case when the programmer creates a memory in the heap and forgets to delete it then leads to a memory leak. Memory leaks are particularly serious issues for programs.

/* Function with memory leak */
#include

void f()
{
int* ptr = (int*)malloc(sizeof(int));

/* Do some work */

return; /* Return without freeing ptr*/
}

14. What is a sequential access file?

Solution:-

In this type of file, data is kept in sequential order if we want to read the last record of the file, we need to read all records before that record so it takes more time. When writing programs that will store and retrieve data in a file, it is possible to designate that file into different forms. To access a particular data within the sequential access file, data has to be read one data at a time, until the right one is reached.

15. Compare arrays with pointers in the C programming language?

Solution:-

Following are the various differences between arrays and pointers in C:

  • Definition – A pointer is a variable that stores or points to the memory address of some other variable whereas an array is a form of data structure that stores multiple, homogeneous elements at contiguous memory locations. 
  • Initialization – An array can be initialized at the definition whereas the pointer can’t be done in the same way.
  • Size – an array is capable of storing multiple elements whereas a pointer can store the address of only a single variable. The total number of elements stored by arrays is determined by the array size.

16. List out some of the C compilers.

Solution:-

C programming is a very old programming language and has a wide range of compilers available in the market. They are available in multiple operating systems. Some of them are listed below:

  • AMPC
  • CCS C Compiler
  • ch
  • clang
  • Cygwin
  • Digital mars
  • GCC compiler
  • MikroC Compiler
  • Portable C Compiler, Power C, QuickC, Ritchie C Compiler, Small-C

17.What is recursion in C?

Solution:-

In some special cases, a function can call itself and this process is called recursion. In this process, this function is called 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.

Example of recursion

#include 
int calculate_fact(int);
int main()
{
int n=5,f;
f=calculate_fact(n); // calling a function
printf("factorial of a number is %d",f);
return 0;
}
int calculate_fact(int a)
{
if(a==1)
{
return 1;
}
else
return a*calculate_fact(a-1); //calling a function recursively.
}

Output:

factorial of a number is 120

18. When Would You Use A Pointer To A Function?

Solution:-

Function pointers can be useful when you want to create a callback mechanism and need to pass the address of a function to another function. They can also be useful when you want to store an array of functions, to call dynamically

A function that takes function pointers says, in effect, “Part of what I do can be customized. Give me a pointer to a function, and I’ll call it when that part of the job needs to be done. That function can do its part for me.” This is known as a “callback.” It’s used a lot in graphical user interface libraries, in which the style of a display is built into the library but the contents of the display are part of the application.

As a simpler example, say you have an array of character pointers (char*s), and you want to sort it by the value of the strings the character pointers point to. The standard qsort() function uses function pointers to perform that task. qsort() takes four arguments,

  • a pointer to the beginning of the array,
  • the number of elements in the array,
  • the size of each array element, and,
  •  a comparison function, and returns an int.

19. What is Dynamic Memory allocation? Mention the syntax. 

Solution:-

Allocating memory to the program and its variables in runtime is the process of Dynamic Memory Allocation.

The dynamic Memory Allocation process involves three functions for allocating memory and one function to free the used memory.

malloc() – Allocates memory

Syntax:

ptr = (cast-type*) malloc(byte-size);

calloc() – Allocates memory

Syntax:

ptr = (cast-type*)calloc(n, element-size);

realloc() – Allocates memory

Syntax:

ptr = realloc(ptr, newsize);

free() – Deallocates the used memory

Syntax:

free(ptr);

20.How To Convert A Number To A String?

Solution:-

There are a number of functions to convert numbers to many from one format to another. Some of the functions are listed below:

Function Name Purpose

  • iota():    Converts an integer value to a string.
  • ltoa ():   Converts a long integer value to a string.
  • ultoa (): Converts an unsigned long integer value to a string.

The following functions can be used to convert floating-point values to strings:
Function Name Purpose

  • ecvt() :   Converts a double-precision floating-point value to a string without an embedded decimal point.
  • fcvt():      Same as ecvt(), but forces the precision to a specified number of digits.
  • gcvt():     Converts a double-precision floating-point value to a string with an embedded decimal point.
  • strtod():   Converts a string to a double-precision floating-point value and reports any “leftover” numbers that could not be converted.
  • strtol():    Converts a string to a long integer and reports any “leftover” numbers that could not be converted.
  • strtoul():  Converts a string to an unsigned long integer and reports any “leftover” numbers that could not be converted.

21. What is the use of printf() and scanf() functions?

Solution:-

printf(): This function helps in printing the integer, character, float, and strings.

Following are the format specifier:

  • %d: It is a format specifier used to print an integer value.
  • %s: It is a format specifier used to print a string.
  • %c: It is a format specifier used to display a character value.
  • %f: It is a format specifier used to display a floating point value.

scanf(): The scanf() function helps in obtaining the input from the user.

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

Solution:-

A static variable is used for a common value which is shared by all the methods and its scope is till the lifetime of the whole program. In the C programming language, static is used with global variables and functions to set their scope to the containing file.

Following are some more properties of 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 that is shared by all the methods.
  • The static variable is initialized only once in the memory heap to reduce memory usage.

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

Solution:- Following are the differences between a call by value and call by reference are:
Basis for comparison 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.

24. What is a pointer in C?

Solution:- A pointer is a variable that refers to the address of a value. It makes the code optimised 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.

25. What is pointer to pointer in C?

Solution:- 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. Let’s understand this concept through an example:

 
#include   

 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;  

}  

In the above example, pptr is a double pointer pointing to the address of the ptr variable and ptr points to the address of ‘a’ variable.