C Interview Questions – Set 01

Write a program to swap two numbers without using the third variable?

#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();
}

Write a program to print factorial of given number using recursion?

#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 %ldn”, number, fact);
getch(); //It reads a character from the keyword.
}

What is the acronym for ANSI?

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.

What is recursion in C?

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.

What is dangling pointer in C?

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

What is a union?

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.

Who is the founder of C language?

Dennis Ritchie.

Write a program to check Armstrong number in C?

#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.
}

Write a program to print Fibonacci series without using recursion?

#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();
}

What is typecasting?

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;