C Interview Q & A

Top 30 C Programming Interview Questions And Answers

In the Technology world ,programming is very important because it has changed the world.So ‘C’ is the base programming language.Generally interviewer asked firstly ‘C’ Questions.

1 What is C Language ?

# C is a structural and middle level programming language developed by Dennis Ritchie at Bell Laboratories in 1972.

#In 1998,ANSI(American National Standard Institute) gave a definition “ANSI C”

#It is also known as modular programming language and mother language.

2 What is the Features of C Language ?

#Portability-Platform independent language

#Modularity-Large program is broken into small program

#Poweful-It provides wide variety of data types,functions and loop control statement

#Mid-Level-It support the features of both low level and high level programming language

#Simple-It is very simple,easy to understand

3 What is structure of writing c program

#include<stdio.h>//preprocessor directives

int a =10;//global variable//function declaration

void main()

{
printf(“a value will be=”,a)

}

Output:

a value will be=10

4 What is the difference between local variable and global variable in C ?

Local variable:

# When variables are declared inside the funcation or block is known as local variable

#The life-time of variable will be valid only inside the funcation or block

Global variable:

#When variables are declared outside the funcation or block is known as global variable.

#The life-time of variable will be valid outside of the funcation or block

Example:

#include<stdio.h>

int a=10;//This is global variable

void main( )

{
int b=15;// This is local variable

printf(“a=%d b=%d”,a,b);

}

Output:

a=10 b=15

5 What is use of scanf( ) and printf( ) funcation in C ?

Scanf( )-Scanf( ) funcation is used to read the input which is given by user.

printf( )-Printf( ) funcation is used to display the output/write the output on the screen

Some formate specifier:

%d-It is used to print integer value

%s-It is used to print string

%d-It is used to double

Example:

void main()

{
int n;

Printf(“Enter the number\n”);

Scanf(“%d”,&n); //Here read the value of n which is entered by the user

Printf(“Entered number is=”,n);//Here display the value of n

}

Output:

Enter the number

5

Entered number is=5

6.What do you mean by ternary operator or conditional operator ?

This operator works on the three oprands.

Syntax:

(Expression ? statement1 : statement2)

The first “Expression” is evaluated

If “Expression” is true ,statement1 is evaluated

if “Expression” is false,statement2 is evaluated

Example:


int a=7,b=8;

(a>b?printf(“hello suman”):printf(“hii suman”))

output:

hii suman

7.What is incriment and decriment operator in C ?

Incriment operator means adding 1 to the value

Example:

x=4,x++ returns 5

It is also can do by simple way x=x+1=4+1=5

Decriment operator means subtracting 1 from the value

Example:

x=4,x– returns 3

It is also can do by simple way x=x-1=4-1=3

8.Write a program of factorial number in C ?

Factorial number of 5 ie !5=5*4*3*2*1=120

#include<stdio.h>

void main( )

{

int i=1,number,fact=1;

printf(“Enter the number\n”); // \n is used for next line

scanf(“%d”,&number);

for(i=1;i<=number;i++)

{

fact=fact*i;

}

printf(” factorial =%d”,fact);

}

Output:

Enter the number

5

factorial=120

9.What is the difference between = and == operators in C ?

‘=’ is the assignment operator which is used assign value to the variable.

Example:

int number=10;

‘==’ is the comparison operator which is used to compare the value or value on left hand side expression and right hand side expression.

Example:

int a=10,b=20;

if(a==b)

{

printf(“Hello suman”);

}

else

{
printf(“Hii suman”);

}

Output

when compare (a==b) will return 0 because condition is false.So output will be “Hii suman”.

10 What is modular programming in C ?

This is the process of dividing large program into small modules.

This small modules are executed individually

At the end,All modules are merged into single unit is known as main program

11.What do you by Keywords,and Variables in C ?

Keywords:

Keywords are the basic building blocks in C programming language.

Each keywords has special meaning

There are the 32 keywords in C programming language

It is also known as “Reserved” word.

Example: int,float,double,char etc.

Variables:

Variables are the data name that is used to store data value.

Only one value can store at a time.

The value can be changed during the program execution

Each variable has associated with the data types.

Example :

int y ; //Here “y” is varable

int suman=30; //Here “suman” is variable

12 What do you mean by “Identifiers” in C programming language ?

Identifiers are the user defined words and are used as variables name.

Rules for the identifiers:

1.The names should be only alphabet ,digit,underscore sign .

2.First character should be alphabet or underscore sign.

Example : _suman ,temp etc

3.The name should not be keyword

4.The length of the identifiers should be 31.

13 What are the data types in C language ?

Data types tells that what kind of data we use in our program.

There are the three types of “data types”.

1.Fundamental data type

2.Drived data type

3.User defined data type

1.Fundamental data type

These are the in-built data types which is used directly in the program.

It is also called primary data type

Example: integer,character,float,double etc

2.Drived data type

Drived data types are those those data types that are construct with the help of fundamental data types.

Example:Array,String,pointer etc

3.User defined data type

This data types are defined by the user.

Example: Enum,Typedef etc

14.What are the operators in C language ?

An operator is a symbol which tells the computer/compiler to do a certain mathematical or logical manipulation.

List of operators:

1.Arithmatic operator

2.Logical operator

3.Conditional operator

4.Assignment operator

5.Incriment or Decriment operator

6.Relational operator

7.Bitwise operator

8.Modulo operator

9.Sizeof operator

15.What do you mean by “modulo” and “sizeof” operator in C ?

Modulo operator(%)

Modulo operator is used to find out the remainder value

It works only with the integer

Example: Let a=20,b=20

then a%b=20%20=0.

Sizeof operator

It is special type operator it gives the size of the variable.

Example: sizeof(int)=2 ,sizeof(float)=4 ,sizeof(char)=1

16.what do you mean byFunction” in C ?

A function in ‘C’ can perform particular task and support the modular programming technique.

It reduce the code length if code lenght is large.

Every function has a unique name this name is used to call function from main function.

A function may take argument variable

int function_name(int x);

17 What is an infinite loop in C ?

A loop which runs infinite times is known as infinite loop.

Infinite while loop

while(1)
{
//code will execute here
}


Infinite for loop

for(;;)
{
//code will execute here
}

Infinite do while loop

do
{
//code will execute here
}while(1);

18 What is an array in C ?

An array is a group of same data types or An array is a variable which store multiple values into the single variable.

It has contiguous memory location

Array size cannot be changed after the declaration

Syntax:

data_type array_name[size];

Example of Array

#include<stdio.h>
void main()
{
int i, a[5]={1,2,3,4,5};//Here array consist five integer value
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
}

Output:

1 2 3 4 5

19 Write a program of sum of five number using Array in C

Here we have enter five integer 1,2,3,4 and 5.

#include<stdio.h>
void main()
{
int i,sum=0,a[5]={1,2,3,4,5};
for(i=0;i<5;i++)
{
sum=sum+a[i];
}
printf("sum=%d",sum);
}
}

Output:

sum=15

20 What do you mean by command line argument in C ?

When the argument passed to the main function while executing the program then it is known as command line argument.

Example:

main(int sum,char *argu[])
{
//write code here
}

21 What do you mean by nested loop in C ?

A loop which is running inside the another loop is called nested loop.The first loop is called outer loop and inside the loop is called inner loop.

22 What is general form of function declaration in C ?

It contains four major declaration:

return_type function_name(parameter list)
{
//write code here or function body
}

return_type:It returns the value of function. It may be void,int,double etc.

function_name:keep the name of function according to the action such as sum( ),mul( ) etc

parameters: Input values for the function to perform the action.

function body:Collection of statements are performed in this block

23 What is recursion in C ?

Recursion is a process which allow to call the function within the same function

When the function calls itself then this function is known as recursive function.

Example:

#include<stdio.h>
int factorial_number(int);
int main()
{
int n=4,fact;
fact=factorial_number(n);//calling the function
printf("factorial number=%d",fact);
return 0;
}
int factorial_number(int x)
{
if(x>0) //checking entered number is positive
{
if(x==1)
{
return 1;
}
else
{
return x*factorial_number(x-1);//calling the function recursively
}
}
}

Output:

factorial number=24

24 What is pointer in C ?

A pointer is a variable which store the address of another variable

It optimized and make fast the code

Syntax:

data_type *p;

Here p is a pointer variable which store the address of given data type value.

Pointer Example:

#include<stdio.h>
void main()
{
int x=10,*p;
p=&x;
printf("address=%u",p);
}

Output:

address=1235345

25 What is static memory allocation ?

When the memory is allocating at the compile time then it is known as static memory allocation.

We can not increase the size after the declaration.

It is faster then the dynamic memory allocation

It is an implementing using the stack and heap.

Syntax:

int name[10];//This is static memory declaration of size 10

26 What is dynamic memory allocation in C ?

When the memory is allocating at the run time then it is known as dynamic memory allocation.

We can increase the size after the declaration

We can increase the size with the help of malloc and calloc function

For example:
int *p=malloc(sizeof(int*)*10);

27 What is the correct code of the given below output in C ?

1
12
123
1234
12345
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(k=1;k<=i;k++)
{
printf("%d",k);
}
printf("\n");
}
}


28 Write the program of “reverse” number in C .

#include<stdio.h>
void main()
{
int n=1234,rem,rev=0;
if(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf("reverse number=%d",rev);
}

Output:

reverse number=4321

29 Write a program of “prime” number in C .

#include<stdio.h>
void main()
{
int n,i;
printf("Enter the number\n");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
printf("Number is not prime");
break;
}
}
if(i==n)
{
printf("Number is prime");
}
}

Output:

Enter the number
5
Number is prime

30 Write a program of Floyd’s triangle in C.

1
2 3
4 5 6
7 8 9 10
It clear that it goes nth term
#include<stdio.h>
void main()
{
int n,k,i,s=1;
printf("Enter the number of rows of floyd's");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=i;k++)
{
printf("%d",s);
s++;
}
printf("\n");
}
}

Conclusion:

Best Regard By “Placementpreparation.com”.