Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

C programming Interview questions and answers

C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions
recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci
series factorial palindrome code programs examples on c++ tutorials and pdf

C tutorial C Programming Questions C Interview Questions C Programs C Test C programming pdf Program of c++ Sql Server


Start Download Search
Convert Any File to a PDF. Get the Free FromDoctoPdf Toolbar!
Ads by Google

► Download PDF
► Free PDF Books
Start Download ► Java PDF
Convert Any File to a PDF. Get the Free FromDoctoPdf Toolbar!
Ads by Google

► PDF File to Word
► eBook Free Download
C in depth pdf ► VB Net PDF

Page 1 of 25 Ads by Google

► PDF PDF
► com PDF
8/26/2011 ► Upload PDF

C QUESTIONS AND ANSWERS

C program examples

C interview questions and answers

Data type questions
C PROGRAMMING QUESTIONS AND
C
Variable naming rule questions
ANSWER
Operators questions

Control flow questions

Switch case questions
Looping questions

Pointer questions

String questions

Printf,Scanf questions

Preprocessor questions

Structure questions

Commad line argument

C questions in Linux http://cquestionbank.blogspot.com | Ritesh kumar

C online test

C mixed practice sets

C tricky questions

Example of recursion in c

C programming forums
Page 2 of 25 ,

(1) What will be output if you will compile and execute
the following c code?

#include<stdio.h>

struct marks{
int p:3;
int c:3;
int m:2;
}; LABELS
Advancen c (14)
int main(){
Array in c (27)
struct marks s={2,­6,5};
printf("%d %d %d",s.p,s.c,s.m); C programs (48)
return 0; C++ (21)
Data types (55)
}
Exact (6)
(a) 2 ­6 5 File Handling (30)
(b) 2 ­6 1 Function tutorial in c (78)
(c) 2 2 1 Java (53)
(d) Compiler error
(e) None of these linux questions (4)
Looping in c (6)
Answer: (c) Memory Mapping (15)
Explanation: Operators (19)

Binary value of 2: 00000010 (Select three two bit) pdf (11)
Binary value of 6: 00000110 Pointers (31)
Binary value of ­6: 11111001+1=11111010 Pointers on c (147)
(Select last three bit) Preprocessor (24)
Binary value of 5: 00000101 (Select last two bit)
SQL (6)

Complete memory representation:
PAGEVIEWS LAST MONTH

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
5 4 5 1 9 6
Page 2
C LOVER COMMUNITY

Join this site
with Google Friend Connect

Members (3386)  More »
Page 3 of 25

Already a member? Sign in

SUBSCRIBE VIA EMAIL
C TUTORIAL
(2) What will be output if you will compile and execute
Memory mapping tutorial in c Enter your email address:
the following c code?

Variables tutorial in c
#include<stdio.h>
Data types tutorial in c Subscribe

Storage classes tutorial in c int main(){
Delivered by FeedBurner
int huge*p=(int huge*)0XC0563331;
Looping tutorial in c int huge*q=(int huge*)0xC2551341;
*p=200;
Pointers tutorial in c
printf("%d",*q);
STANDARD OF QUESTIONS
?
Function tutorial in c return 0;
Array tutorial in c Excellent    1011 (45%)
}
Good    626 (27%)
Preprocessor tutorial in c
Avg    129 (5%)
(a)0
Advanced c tutorial Worst    470 (21%)
(b)Garbage value

(c)null Votes so far: 2236 
POPULAR POSTS (d) 200 Poll closed 
(e)Compiler error
C program examples | Interview
Complete List
Answer: (d)
C interview questions and answers Explanation:

Check given number is prime number Physical address of huge pointer p
or not using c program
Huge address: 0XC0563331
Offset address: 0x3331
Program to convert decimal to binary in
c Segment address: 0XC056

TO FIND FACTORIAL OF A NUMBER Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
USING C PROGRAM Page 3

Find out the perfect number using c
program

TO FIND FIBONACCI SERIES USING
C PROGRAM
Page 4 of 25
MULTIPLICATION OF TWO MATRICES
USING C PROGRAM

C questions and answers Physical address= Segment address * 0X10 + Offset

address
Prime number program in c using
=0XC056 * 0X10 +0X3331
recursion
=0XC0560 + 0X3331

=0XC3891
C PROGRAMMING QUESTIONS AND Physical address of huge pointer q
ANSWER Huge address: 0XC2551341

C questions and answers Offset address: 0x1341
Segment address: 0XC255
C interview questions and answers
Physical address= Segment address * 0X10 + Offset
Pointers to pointers in c programming
language address
=0XC255 * 0X10 +0X1341
Debugging questions in c with answers
=0XC2550 + 0X1341
Aptitude questions and answers in c
=0XC3891
Since both huge pointers p and q are pointing same

There was an error in this gadget physical address so content of q will also same as
content of q.
content of q.
148

(3) Write c program which display mouse pointer and
position of pointer. (In x coordinate, y coordinate)?

Answer:

#include<dos.h>
#include<stdio.h>

int main(){
union REGS i,o;

int x,y,k;
//show mouse pointer

i.x.ax=1;
int86(0x33,&i,&o);
while(!kbhit()) //its value will false when we hit
key in the key board

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 4

Page 5 of 25

i.x.ax=3; //get mouse position

x=o.x.cx;
y=o.x.dx;
printf("(%d , %d)",x,y);

delay(250);
int86(0x33,&i,&o);
}
return 0;
}

(4) Write a c program to create dos command: dir.

Answer:

Step 1: Write following code.
Step 1: Write following code.

#include <stdio.h>
#include <dos.h>

int main(int count,char *argv[]){
struct find_t q ;

int a;
if(count==1)
argv[1]="*.*";
a = _dos_findfirst(argv[1],1,&q);

if(a==0){
while (!a){
printf(" %s\n", q.name);
a = _dos_findnext(&q);

}
}
else{
printf("File not found");

}
return 0;

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 5

Page 6 of 25

Step 2: Save the as list.c (You can give any name)
Step 3: Compile and execute the file.
Step 4: Write click on My computer of Window XP
operating system and select properties.
Step 5: Select Advanced ­> Environment Variables
Step 6: You will find following window:
Click on new button (Button inside the red box)
Step 7: Write following:
Variable name: path
Variable value: c:\tc\bin\list.c (Path where you have

saved)

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 6

Page 7 of 25
Step 8: Open command prompt and write list and press

enter.

(5) What will be output if you will compile and execute
the following c code?

#include<stdio.h>

int main(){
int i;
float a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=3;i++)
printf("%d ",*ptr++);

return 0;
}
Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 7

Page 8 of 25

(a)0 0 0 0
(b)Garbage Garbage Garbage Garbage
(c)102 56 ­80 32
(d)102 102 ­90 64
(e)Compiler error
Answer: (d)
Explanation:

In c float data type is four byte data type while char
pointer ptr can point one byte of memory at a time.
Memory representation of float a=5.2

ptr pointer will point first fourth byte then third
byte then second byte then first byte.

Content of fourth byte:
Binary value=01100110
Decimal value= 64+32+4+2=102

Content of third byte:
Binary value=01100110
Decimal value=64+32+4+2=102
Content of second byte:

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 8

Page 9 of 25

Binary value=10100110
Decimal value=­128+32+4+2=­90

Content of first byte:
Content of first byte:
Binary value=01000000
Decimal value=64

Note: Character pointer treats MSB bit of each byte
i.e. left most bit of above figure as sign bit.

(6) What will be output if you will compile and execute
the following c code?

#include<stdio.h>

int main(){
int i;
double a=5.2;
char *ptr;
ptr=(char *)&a;
for(i=0;i<=7;i++)
printf("%d ",*ptr++);

return 0;

(a) ­51 ­52 ­52 ­52 ­52 ­52 20 64
(b) 51 52 52 52 52 52 20 64
(c) Eight garbage values.
(d) Compiler error
(e) None of these

Answer: (a)
Explanation:

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 9

Page 10 of 25

In c double data type is eight byte data type while
char pointer ptr can point one byte of memory at a

time.

Memory representation of double a=5.2

ptr pointer will point first eighth byte then seventh
byte then sixth byte then fifth byte then fourth byte
then third byte then second byte then first byte as
shown in above figure.

Content of eighth byte:
Binary value=11001101
Decimal value= ­128+64+8+4+1=­51

Content of seventh byte:

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 10

Page 11 of 25
Binary value=11001100
Decimal value= ­128+64+8+4=­52

Content of sixth byte:
Binary value=11001100
Decimal value= ­128+64+8+4=­52

Content of fifth byte:
Binary value=11001100
Decimal value= ­128+64+8+4=­52

Content of fourth byte:
Binary value=11001100
Decimal value= ­128+64+8+4=­52

Content of third byte:
Binary value=11001100
Decimal value= ­128+64+8+4=­52

Content of second byte:
Binary value=000010100
Decimal value=16+4=20
Content of first byte:
Binary value=01000000
Decimal value=64

Note: Character pointer treats MSB bit of each byte
i.e. left most bit of above figure as sign bit.

(7) What will be output if you will compile and execute
the following c code?

#include<stdio.h>

int main(){

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 11

Page 12 of 25
Page 12 of 25

printf("%s","c" "question" "bank");
return 0;
}

(a) c question bank
(b) c
(c) bank
(d) cquestionbank
(e) Compiler error

Answer: (d)
Explanation:

In c string constant “xy” is same as “x” “y”

(8) What will be output if you will compile and execute
the following c code?

#include<stdio.h>

int main(){
char *str="c­pointer";
printf("%*.*s",10,7,str);
return 0;
}

(a) c­pointer

(b) c­pointer
(c) c­point
(d) cpointer null null

(e) c­point

Answer: (e)
Explanation:

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 12
Page 13 of 25

Meaning of %*.*s in the printf function:
First * indicates the width i.e. how many spaces will
take to print the string and second * indicates how
many characters will print of any string.
Following figure illustrates output of above code:

(9) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

int main(){
int a=­12;
a=a>>3;
printf("%d",a);
return 0;
}

(a) ­4
(b) ­3
(c) ­2
(d) ­96
(e) Compiler error

Answer :( c)

Explanation:

Binary value of 12 is: 00000000 00001100
Binary value of ­12 wills 2’s complement of 12 i.e.

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 13
Page 14 of 25

So binary value of ­12 is: 11111111 11110100

Right shifting rule:
Rule 1: If number is positive the fill vacant spaces in
the left side by 0.
Rule 2: If number is negative the fill vacant spaces in
the left side by 1.
In this case number is negative. So right shift all the
binary digits by three space and fill vacant space by 1
as shown following figure:

Since it is negative number so output will also a
negative number but its 2’s complement.

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 14

Page 15 of 25

Hence final output will be:

And its decimal value is: 2
Hence output will be:­2

(10) What will be output if you will compile and
execute the following c code?

#include<stdio.h>
#include <string.h>

int main(){
printf("%d %d",sizeof("string"),strlen("string"));

return 0;
}

(a) 6 6
(b) 7 7
(c) 6 7
(d) 7 6
(e) None of these

Answer: (d)
Explanation:
Explanation:

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 15

Page 16 of 25

Sizeof operator returns the size of string including
null character while strlen function returns length of
a string excluding null character.

(11) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

int main(){
static main;
int x;
x=call(main);
printf("%d ",x);

return 0;
}

int call(int address){
address++;
return address;
}

(a) 0
(b) 1
(c) Garbage value
(d) Compiler error
(e) None of these

Answer: (b)
Explanation:

As we know main is not keyword of c but is special type
As we know main is not keyword of c but is special type
of function. Word main can be name variable in the main
and other functions.

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 16

Page 17 of 25

(12) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

int main(){
int a,b;
a=1,3,15;
b=(2,4,6);
printf("%d ",a+b);

return 0;
}

(a) 3
(b) 21
(c) 17
(d) 7
(e) Compiler error

Answer: (d)
Explanation:

In c comma behaves as separator as well as operator.
a=1, 3, 15;
b= (2, 4, 6);
In the above two statements comma is working as
operator. Comma enjoys least precedence and associative
is left to right.
Assigning the priority of each operator in the first
statement:

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 17

Page 18 of 25

Hence 1 will assign to a.
Assigning the priority of each operator in the second

statement:

(13) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

int extern x;
int main()
printf("%d",x);
x=2;
return 0;
}

int x=23;

(a) 0
(b) 2
(b) 2
(c) 23
(d) Compiler error
(e) None of these

Answer: (c)
Explanation:

extern variables can search the declaration of variable
anywhere in the program.
Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 18

Page 19 of 25

(14) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

int main(){
int i=0;
if(i==0){
i=((5,(i=3)),i=1);
printf("%d",i);
}
else
printf("equal");
}

(a) 5
(b) 3
(c) 1
(d) equal
(e) None of above

Answer: (c)
(15) What will be output if you will compile and
execute the following c code?

int main(){
int a=25;
printf("%o %x",a,a);

return 0;
}

(a) 25 25
(b) 025 0x25

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 19

Page 20 of 25

(c) 12 42
(d) 31 19
(e) None of these

Answer: (d)
Explanation:

%o is used to print the number in octal number format.
%x is used to print the number in hexadecimal number

format.

Note: In c octal number starts with 0 and hexadecimal
number starts with 0x.

(16) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

#define message "union is\
power of c"
int main(){
printf("%s",message);
return 0;
}

(a) union is power of c
(b) union ispower of c
(c) union is
Power of c
(d) Compiler error
(e) None of these

Answer: (b)
Explanation:

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 20

Page 21 of 25

If you want to write macro constant in new line the end
with the character \.

(17) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

#define call(x) #x

int main(){
printf("%s",call(c/c++));
return 0;
}

(a)c
(b)c++
(c)#c/c++
(d)c/c++
(e)Compiler error
Answer: (d)
Explanation:

# is string operator. It converts the macro function
call argument in the string. First see the intermediate

file:

test.c 1:
test.c 2: void main(){
test.c 3: printf("%s","c/c++");
test.c 4: return 0;
test.c 4: }
test.c 5:

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 21

Page 22 of 25

It is clear macro call is replaced by its argument in
the string format.

(18) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

int main(){
if(printf("cquestionbank"))
printf("I know c");

else
printf("I know c++");

return 0;
}

(a) I know c
(a) I know c
(b) I know c++
(c) cquestionbankI know c
(d) cquestionbankI know c++
(e) Compiler error

Answer: (c)
Explanation:

Return type of printf function is integer which returns
number of character it prints including blank spaces.
So printf function inside if condition will return 13.
In if condition any non­ zero number means true so else
part will not execute.

(19) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/
Page 22

Page 23 of 25

int main(){
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");

else
printf("Less than");

return 0;

}
(a) Equal
(b) Greater than
(c) Less than
(d) Compiler error
(e) None of above

Answer: (d)
Explanation:

Static variables are load time entity while auto
variables are run time entity. We cannot initialize any
load time variable by the run time variable.
In this example i is run time variable while x is load
time variable.

(20) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

int main(){

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/

Page 23

Page 24 of 25

printf("%s",__DATE__);
return 0;

(a) Current system date
(b) Current system date with time
(c) null
(d) Compiler error
(e) None of these

Answer: (a)
Answer: (a)
Explanation:

__DATE__ is global identifier which returns current
system date.

(21) What will be output if you will compile and
execute the following c code?

#include<stdio.h>

void start();
void end();
#pragma startup start
#pragma exit end

int static i;

int main(){
printf("\nmain function: %d",++i);

return 0;

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/

Page 24

Page 25 of 25

void start(){
printf("\nstart function: %d",++i);

void end(){
printf("\nend function: %d",++i);

(a)
main function: 2
start function: 1
end function:3

(b)
start function: 1
main function: 2
end function:3

(c)
main function: 2
end function:3
start function: 1
(d) Compiler error
(e) None of these
Answer: (b)

Explanation:

Every c program start with main function and terminate
with null statement. But #pragma startup can call
function just before main function and #pragma exit

Copyright@ritesh kumar: http://cquestionbank.blogspot.com/

Page 25
This PDF doc keeps c programming questions and answer with explanation in
depth. To free download the pdf doc go to the File ­> Download Original

Recommend this on Google

9 comments:

Gaurav Kumar 2/28/13, 6:10 PM
It's good but how can download the pdf file
Reply

Replies

ritesh kumar 2/28/13, 7:21 PM
download  the  pdf  click  on  top  right  corner  of  pdf(Open  in  new  window)  then  go  to  the  File  ­>
Download Original

Reply

Bhuvan 6/4/13, 9:15 PM
Can u upload "C in Depth" by Srivastava..
Reply

Anonymous 6/27/13, 11:42 AM
your genuis, can you give some vb.net sample programs or codes?
Reply

rahul sati 7/10/14, 11:09 AM
i have this book "c in depth" .. nice book.
Reply

Replies

Naga Prasad 6/4/16, 10:51 AM
hi...
can u send C IN DEPTH softcopy to my mail..id:bnprasad020@gmail.com
thanks in advance.

Reply

Deepak Mandal 3/20/15, 3:08 PM
its good u can understand easily
Reply

raja rajeswari 4/18/15, 8:58 AM
pls upload the full book in pdf format
Reply

Ramesh Thapa 2/29/16, 10:00 AM
Please Upload the full book in pdf format
Reply
Enter your comment...

Comment as:  Select profile...

Publish
  Preview

Links to this post
Create a Link

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

Copyright@Priyanka. Picture Window template. Powered by Blogger.

You might also like