0% found this document useful (0 votes)
17 views9 pages

C Interview Questions For Experienced Professionals

The document provides an introduction to the C programming language, highlighting its popularity and versatility in various applications. It includes a list of commonly asked interview questions for experienced professionals, covering topics such as memory allocation, variable scope, and function behaviors. Each question is accompanied by a brief solution or explanation, making it a useful resource for C language interview preparation.

Uploaded by

arjunsai7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

C Interview Questions For Experienced Professionals

The document provides an introduction to the C programming language, highlighting its popularity and versatility in various applications. It includes a list of commonly asked interview questions for experienced professionals, covering topics such as memory allocation, variable scope, and function behaviors. Each question is accompanied by a brief solution or explanation, making it a useful resource for C language interview preparation.

Uploaded by

arjunsai7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C Language Introduction

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.

More About C Language

Commonly Asked C Interview Questions for Experienced


Professionals

1. What is scope of a variable? How are variables scoped in C?


Solution:- The scope of a variable refers to the area of the program where the variable can be accessed directly. All identifiers in
C have a lexical (or statically) scope.

2.What is the difference between “calloc(…)” and “malloc(…)”?


Solution:-

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(…).

3. Can you tell me how to check whether a linked list is circular?

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.

4. What are the different storage classes in C?


Solution:- Automatic, static, and allocated storage are the three forms of storage available in C. The storage length of a variable
with block scope and no static specifier is automatically set.
Static scope applies to variables with block scope and static specifiers.
Static scope applies to global variables with or without the static specifier. The allocated storage class includes memory accessed
from malloc(), alloc(), and realloc() calls.

5. What are the valid places to have keyword “Break”?


Solution:- The Break keyword’s aim is to remove control from the currently running code block. Only looping and turn
statements will contain it.

6. Differentiate between call by value and call by reference.


Solution:-

Call by Value

Actual arguments cannot be changed and remain safe.


Separate memory loCopy of actual arguments are sent
cations are created for actual and formal arguments.

Call by Reference

Operations are performed on actual arguments, hence not safe.


Actual and Formal arguments share the same memory space.
Actual arguments are passed.
7. Differentiate between getch() and getche().

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

Please enter a character


Your entered character is x
Please enter another character z
Your new character is z

8. Write a code to generate random numbers in C Language

Solution:- Random numbers in C Language can be generated as follows:

#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

9. What do you mean by Memory Leak?

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

Enter the number of elements: 5


Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,

10. Explain Local Static Variables and what is their use?

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

Before swapping a=10 b=20


After swapping a=20 b=10

12. How can you remove duplicates in an array?

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:-

Enter no. of elements in array. 5


Enter 5 integers
12
11
11
10
4
Array obtained after removing duplicate elements
12
11
10
4

13. Can I use int datatype to store 32768 value?


Solution:- No, the Integer datatype does not support the range -32768 to 32767. Any value greater than that would be
discarded. We have the choice of using float or long int.

14. What is an array?


Solution:-

The array is a simple data structure that allows you to store multiple elements of the same datatype in a reserved and
sequential order.

One Dimensional Array


Two Dimensional Array
Multi-Dimensional Array

15. When should a far pointer be used?


Solution:-

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.

16.What is the difference between declaring a variable and defining a


variable?
Solution:-

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.

17. What is rvalue and lvalue?


Solution:-

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.”

18. Write a simple example of a structure in C Language.

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).

23. What is the return values of printf and scanf?


Solution:- After a full return, the printf function returns the number of characters printed in the output unit. As a result,
printf(“A”) returns 1. The scanf function returns the number of successfully matched and allocated input objects, which may be
less than the format specifiers supplied. In the event of an early match loss, it may also return zero.

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)).

25. How to call a function before main()?

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”);
}

The output of the above program will be –


Output :
In fun
In main

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

You might also like