C Interview Questions For Experienced Professionals
C Interview Questions For Experienced Professionals
Below you will below get all the C Interview Questions for experienced Professionals.
C is a general-purpose programming language that is extremely popular, simple and flexible. It is machine-
independent, structured programming language which is used extensively in various applications.
C was the basic language to write everything from operating systems (Windows and many others) to complex
programs like the Oracle database, Git, Python interpreter and more.
1. calloc(…) allocates a memory block for an array of elements with a specific size. The block is initially set to 0 by default.
(number of elements * size) will be the total amount of memory allocated.
malloc(…) takes just one argument, which is the amount of memory required in bytes. Like calloc(…), malloc(…) allocated
bytes of memory rather than blocks of memory.
2. malloc(…) Returns a void pointer to the allocated space, or NULL if insufficient memory is available.
calloc(…) Returns a pointer to the allocated space after allocating an array of memory with elements set to 0. To use
the C++ set new mode function to set the new handler mode, calloc(…) calls malloc(…).
Solution:-
Create two pointers, and set both to the start of the list. Update each as follows:
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circular");
}}
If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before
that. Either way, its either 1 or 2 jumps until they meet.
Call by Value
Call by Reference
Solution:-
The only difference between the two roles is that one reads characters from the keyboard and the other does not.
getch() is a function that reads characters from the keyboard without using any buffers. As a result, no data is shown on the
computer.
getche() uses a buffer to read characters from the keyboard. As a result, information is shown on the projector.
//Example
#include
#include
int main()
{
char ch;
printf("Please enter a character ");
ch=getch();
printf("nYour entered character is %c",ch);
printf("nPlease enter another character ");
ch=getche();
printf("nYour new character is %c",ch);
return 0;
}
Output
#include
#include
int main()
{
int a,b;
for(a=1;a<=10;a++)
{
b=rand();
printf("%dn",b);
}
return 0;
}
OUTPUT
1987384758
2057844389
3475398489
2247357398
1435983905
Solution:-Memory Leak occurs when a the programmer allocates dynamic memory to a program but fails to free or erase the
used memory after the code has been completed. If daemons and servers are included in the software, this is dangerous.
#include
#include
int main()
{
int* ptr;
int n, i, sum = 0;
n = 5;
printf("Enter the number of elements: %dn", n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL)
{
printf("Memory not allocated.n");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.n");
for (i = 0; i<= n; ++i)
{
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i<=n; ++i)
{
printf("%d, ", ptr[i]);
}
}
return 0;
}
OUTPUT
Solution:- A local static variable is one whose existence does not end when it is declared in a function call. It is valid for the
duration of the entire curriculum. The function’s calls all share the same copy of local static variables.
#include
void fun()
{
static int x;
printf("%d ", x);
x = x + 1;
}
int main()
{
fun();
fun();
return 0;
}
11. Write a program to swap two numbers without using the third variable.
Solution:-
#include<stdio.h>
#include<conio.h>
main()
{
int a=10, b=20;
clrscr();
printf("Before swapping a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("nAfter swapping a=%d b=%d",a,b);
getch();
}
Output
Solution:- The following program will help you to remove duplicates from an array.
#include
int main()
{
int n, a[100], b[100], calc = 0, i, j,count;
printf("Enter no. of elements in array.n");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i<n; i++)
{
for (j = 0; j<calc; j++)
{
if(a[i] == b[j])
break;
}
if (j== calc)
{
b[count] = a[i];
calc++;
}
}
printf("Array obtained after removing duplicate elementsn");
for (i = 0; i<calc; i++)
{
printf("%dn", b[i]);
}
return 0;
}
OUTPUT:-
The array is a simple data structure that allows you to store multiple elements of the same datatype in a reserved and
sequential order.
You can often get away with using a limited memory model for the majority of a program. There might be a few items in your
small data and code segments that don’t suit you. When this occurs, you can access the remaining memory by using explicit far
pointers and function declarations. A far function may exist outside of the 64KB section that most functions are crammed into in
order to fit into a small-code model. You can often get away with using a limited memory model for the majority of a program.
There might be a few items in your small data and code segments that don’t suit you. When this occurs, you can access the
remaining memory by using explicit far pointers and function declarations. A far function may exist outside of the 64KB section
that most functions are crammed into in order to fit into a small-code model.
Declaring a variable means telling the compiler its type but not allocating any memory for it. Declaring a variable and allocating
space to keep it are both part of the definition process. A variable may also be initialised when it is specified.
In an assignment, lvalue is the left side operant, while rvalue is the right. You can also remember lavlue as a place. As a result,
lvalue refers to a place where any value can be stored. For example, in the statement I = 20, the value 20 is to be stored in the
variable i’s position or address. Rvalue is 20 in this case. Then the argument 20 = I is invalid. As 20 does not reflect any
position, it will result in the compilation error “lvalue needed.”
Solution:- Structure is defined as a user-defined data type that is designed to store multiple data members of the different data
types as a single unit. A structure will consume the memory equal to the summation of all the data members.
struct employee
{
char name[10];
int age;
}e1;
int main()
{
printf("Enter the name");
scanf("%s",e1.name);
printf("n");
printf("Enter the age");
scanf("%d",&e1.age);
printf("n");
printf("Name and age of the employee: %s,%d",e1.name,e1.age);
return 0;
}
19.What is indirection?
Solution:-It is direct access in C when we use the variable name to access the value. When we use a pointer to get the value of
a variable, we are using indirection.
20. What is the difference between void foo(void) and void foo()?
Solution:-
In C, void foo() denotes a function that takes an unspecified number of unspecified type arguments, while void foo(void)
denotes a function that takes no arguments. In the case of void foo(), the compiler will not raise any errors if we call it
foo(1,2,3). In the case of void foo, however, the compiler will generate an error (void).
When calling a function in C, the caller moves all of the arguments into the stack in reverse order before calling the callee. The
compiler will not evaluate the arguments passed to foo if you use foo(). In the case of foo(void), the compiler will check the
number of arguments before calling the function and will raise an error if the number of arguments is incorrect.
21. Can I declare the same variable name to the variables which have different scopes?
Solution:-Yes, Same variable name can be declared to the variables with different variable scopes as the following example.
int var;
void function()
{
int variable;
}
int main()
{
int variable;
}
22. What will be the output of printf(“%d”)?
Solution:- The compiler will generate a warning but will not trigger an error if only percent d is used in printf without any
variables. In this case, the compiler calculates the correct offset based on %d, but since the actual data variable is not in that
measured memory location, printf will fetch the integer size value and print whatever is there (which is garbage value to us).
24. How to free a block of memory previously allocated without using free?
Solution:-The memory will be released if the pointer keeping the memory address is transferred to realloc with the size
argument set to zero (like realloc(ptr, 0)).
Solution:- To call a function pragma startup directive should be used. Pragma startup can be used like this –
#pragma startup fun
void fun()
{
printf(“In fun\n”);
}
main()
{
printf(“In main\n”);
}
But this pragma directive is compiler dependent. Gcc does not support this. So, it will ignore the startup directive and will
produce no error. But the output in that case will be –
Output :
In main