C Interview Questions | Eklavya Online

# C Interview Questions

#include
#include
main()
{
int a=10, b=20; //declaration of variables.
clrscr(); //It clears the screen.
printf(“Before swap a=%d b=%d”,a,b);

a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)

printf(“\nAfter swap a=%d b=%d”,a,b);
getch();
}

#include
#include
long factorial(int n) // function to calculate the factorial of a given number.
{
if (n == 0)
return 1;
else
return(n * factorial(n-1)); //calling the function recursively.
}
void main()
{
int number; //declaration of variables.
long fact;
clrscr();
printf(“Enter a number: “);
scanf(“%d”, &number);
fact = factorial(number); //calling a function.
printf(“Factorial of %d is %ld\n”, number, fact);
getch(); //It reads a character from the keyword.
}

The ANSI stands for ” American National Standard Institute.” It is an organization that maintains the broad range of disciplines including photographic film, computer languages, data encoding, mechanical parts, safety and more.

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:

• Winding phase
• 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.
• 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.

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.

Dennis Ritchie.

#include
#include
main()
{
int n,r,sum=0,temp; //declaration of variables.
clrscr(); //It clears the screen.
printf(“enter the number=”);
scanf(“%d”,&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf(“armstrong number “);
else
printf(“not armstrong number”);
getch(); //It reads a character from the keyword.
}

#include
#include
void main()
{
int n1=0,n2=1,n3,i,number;
clrscr();
printf(“Enter the number of elements:”);
scanf(“%d”,&number);
printf(“\n%d %d”,n1,n2);//printing 0 and 1

for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(” %d”,n3);
n1=n2;
n2=n3;
}
getch();
}

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;

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

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];

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. Press Alt+f5 to see the entered character.

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.

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.

#include
#include
main()
{
int n, reverse=0, rem; //declaration of variables.
clrscr(); // It clears the screen.
printf(“Enter a number: “);
scanf(“%d”, &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf(“Reversed Number: %d”,reverse);
getch(); // It reads a character from the keyword.
}

The fopen() function is used to open file whereas fclose() is used to close file.

#include
#include
void printFibonacci(int n) // function to calculate the fibonacci series of a given number.
{
static int n1=0,n2=1,n3; // declaration of static variables.
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf(“%d “,n3);
printFibonacci(n-1); //calling the function recursively.
}
}
void main(){
int n;
clrscr();
printf(“Enter the number of elements: “);
scanf(“%d”,&n);
printf(“Fibonacci Series: “);
printf(“%d %d “,0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
getch();
}

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.

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.
Memory Management: C provides an inbuilt memory function that saves the memory and improves the efficiency of our program.
Extensible: C is an extensible language as it can adopt new features in the future.

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.

The new line escape sequence is represented by “\n”. It inserts a new line on the output screen.

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

Yes, by holding the base address of array into a pointer, we can access the array using a pointer.

#include
#include
void main()
{
int n,i,m=0,flag=0; //declaration of variables.
clrscr(); //It clears the screen.
printf(“Enter the number to check prime:”);
scanf(“%d”,&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf(“Number is not prime”);
flag=1;
break; //break keyword used to terminate from the loop.
}
}
if(flag==0)
printf(“Number is prime”);
getch(); //It reads a character from the keyword.
}

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

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.

printf(): The printf() function is used to print the integer, character, float and string values on to the screen.

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 is used to take input from the user.
• 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.

C is a mid-level and procedural programming language. 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. This technique minimizes error and misinterpretation. More details.

A loop running continuously for an indefinite number of times is called the infinite loop.

Infinite For Loop:

for(;;){
//code to be executed
}
Infinite While Loop:

while(1){
//code to be executed
}
Infinite Do-While Loop:

do{
//code to be executed
}while(1);

#include
#include
main()
{
int n,r,sum=0,temp;
clrscr();
printf(“enter the number=”);
scanf(“%d”,&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf(“palindrome number “);
else
printf(“not palindrome”);
getch();
}

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.

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.

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.

A virtual address is composed of the selector and offset.

near pointer doesn’t have explicit selector whereas far, and huge pointers have explicit selector. When you perform pointer arithmetic on the far pointer, the selector is not modified, but in case of a huge pointer, it can be modified.

These are the non-standard keywords and implementation specific. These are irrelevant in a modern platform.

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

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.

C is known as a mother language because most of the compilers and JVMs are written in C language. Most of the languages which are developed after C language has borrowed heavily from it like C++, Python, Rust, javascript, etc. It introduces new core concepts like arrays, functions, file handling which are used in these languages.

#include
void main(){
if(printf(“hello world”)){} // It prints the ?hello world? on the screen.
}

#include
#include
void main(){
int i,fact=1,number;
clrscr();
printf(“Enter a number: “);
scanf(“%d”,&number);

for(i=1;i<=number;i++){
fact=fact*i;
}
printf(“Factorial of %d is: %d”,number,fact);
getch();
}

The argument passed to the main() function while executing the program is known as command line argument.

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.

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.

It is 32 characters ideally but implementation specific.

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

C is called a mid-level programming language because it binds the low level and high -level programming language. We can use C language as a System programming to develop the operating system as well as an Application programming to generate menu driven customer driven billing system