C Interview Questions And Answers for fresher _ PrepInsta
C Interview Questions And Answers for fresher _ PrepInsta
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.
Read more
Use of C Language-
About C
Solution:-
#include
int main(void)
{
if(printf("Hello World")){
}
}
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.
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*:
6. What is the equivalent code of the following statement in WHILE LOOP format?
Solution:-
int a=1;
while (a<=100) {
a++;
Solution:-
A 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.
#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.", ¬nullpointer);
printf ("\nNull pointer 1 has no valid address.");
printf ("\nNull pointer 2 has no valid address.");
}
Output:
Solution:-
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.
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:
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:
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
comparison Local variable Global variable
Declaration A variable that is declared inside a function or block is A variable that is declared outside function or
known as a local variable. block is known as a global variable.
Scope The scope of a variable is within a function in which they The scope of a variable is throughout the
are declared. program.
Access Variables can be accessed only by those statements Any statement in the entire program can access
inside a function in which they are declared. variables.
Life Life of a variable is created when the function block is Life of a variable exists until the program is
entered and destroyed on its exit. executing.
Storage Variables are stored in a stack unless specified. The compiler decides the storage location of a
variable.
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.
Solution:-
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.
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.
void f()
{
int* ptr = (int*)malloc(sizeof(int));
/* Do some work */
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
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.
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 ec sion in 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.
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:
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
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,
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.
Syntax:
Syntax:
Syntax:
Syntax:
free(ptr);
There are a number of functions to convert numbers to many from one format to another. Some of the functions are listed
below:
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.
Solution:-
printf(): This function helps in printing the integer, character, float, and strings.
scanf(): The scanf() function helps in obtaining the input from the user.
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.
g
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, When a copy of the value is passed to the function,
then the original value is not modified. then the original value is modified.
Memory Actual arguments and formal arguments are created Actual arguments and formal arguments are
Location in separate memory locations. created in the same memory location.
Safety In this case, actual arguments remain safe as they In this case, actual arguments are not reliable, as
cannot be modified. they are modified.
Arguments The copies of the actual arguments are passed to the The addresses of actual arguments are passed to
formal arguments. their respective formal arguments.
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.
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;
ptr=&a;
pptr=&ptr;
printf("value of a is:%d",a);
printf("\n");
printf("\n");
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.