Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
18Activity
0 of .
Results for:
No results containing your search query
P. 1
c

c

Ratings:

4.5

(2)
|Views: 528 |Likes:
Published by ratulray2006

More info:

Published by: ratulray2006 on Jul 22, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

07/24/2011

pdf

text

original

 
Dear visitor,
Thanks for your interest in C programming. In this page, you will find a list of interesting C programming questions/puzzles, These programs listed are the oneswhich I have received as e-mail forwards from my friends, a few I read in some books, a few from the internet, and a few from my coding experiences in C.Most of the programs are meant to be compiled, run and to be explained for their  behaviour. The puzzles/questions can be broadly put into the following categories:
General typo errors, which C programmers do often and are very difficult totrace.
Small programs which are extremely hard to understand at the firstexamination. These questions make a good excercise of reading andunderstanding effecient code written by others.I have used Gnu/Linux/gcc for all of them. The order in which the programs appear doesn't have any relation with the level of difficulty. Please feel free to contact me if you need any help in solving the problems. My contact info. is available here And you might be interested in a few references for C programming,which I personally found very interesting.Regards,
Gowri Kumar 
 
C puzzles
The expected output of the following C program is to print the elements in the array.But when actually run, it doesn't do so.
#include<stdio.h> #defineTOTAL_ELEMENTS(
sizeof
(array)
/
 
sizeof
(array[0])) 
int
 array[]
=
{23,34,12,17,204,99,16};  
int
 main(){ 
int
 d; 
for
(d
=-
1;d 
 <=
(TOTAL_ELEMENTS
-
2);d
++
) printf("%d\n",array[d
+
1]); 
return
 0;}
Find out what's going wrong.I thought the following program was a perfect C program. But on compiling, I found asilly mistake. Can you find it out (without compiling the program :-) ?
#include<stdio.h>
 void 
 OS_Solaris_print(){ printf("Solaris - Sun Microsystems\n"); }
 
 void 
 OS_Windows_print(){ printf("Windows - Microsoft\n"); }
 void 
 OS_HP
-
UX_print(){ printf("HP-UX - Hewlett Packard\n"); }
int
 main(){ 
int
 num; printf("Enter the number (1-3):\n");  scanf("%d",
&
num); 
switch
(num){ 
case
 1: OS_Solaris_print(); 
 break
; 
case
 2: OS_Windows_print(); 
 break
; 
case
 3: OS_HP
-
UX_print(); 
 break
; 
default
: printf("Hmm! only 1-3 :-)\n");  
 break
;} 
return
 0;}
What's the expected output for the following program and why?
enum 
{false,true};
int
 main(){ 
int
 i
=
1; 
do
{ printf("%d\n",i);  i
++
; 
if
(i 
 <
 15) 
continue
;}
while
(false); 
return
 0;}
The following program doesn't "seem" to print "hello-out". (Try executing it)
#include<stdio.h> #include<unistd.h> 
int
 main(){ 
while
(1){ fprintf(stdout,"hello-out");  fprintf(stderr,"hello-err");  sleep(1); }
 
 
return
 0;}
What could be the reason?
 #include<stdio.h> #definef(a,b)a
##
b #defineg(a)
#
a #defineh(a)g(a)  
int
 main(){ printf("%s\n",h(f(1,2)));  printf("%s\n",g(f(1,2)));  
return
 0;}
Just by looking at the program one "might" expect the output to be, the same for boththe printf statements. But on running the program you get it as: bash$ ./a.out12f(1,2) bash$Why is it so?
 #include<stdio.h> 
int
 main(){ 
int
 a
=
10; 
switch
(a){ 
case
 '1': printf("ONE\n");  
 break
; 
case
 '2': printf("TWO\n");  
 break
; 
defa1ut
: printf("NONE\n"); } 
return
 0;}
If you expect the output of the above program to be
NONE
, I would request you tocheck it out!!The following C program seg faults of IA-64, but works fine on IA-32.
int
 main(){ 
int*
 p; p 
=
(
int*
)malloc(
sizeof
(
int
)); 
*
p 
=
 10; 
return
 0;}
Why does it happen so?Here is a small piece of program(again just 14 lines of program) which counts thenumber of bits set in a number.Input Output0 0(0000000)5 2(0000101)

Activity (18)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
togopal liked this
Tripti Upadhyay liked this
vemprala liked this
jyotiranjan77 liked this
swap00seven liked this
sivalingamece liked this
Raja Rajendran liked this
Ashutosh Kumar liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->