1000 C Programming Questions PDF
1000 C Programming Questions PDF
3. struct list{
int x;
struct list *next;
}*head; 7. #define MAX(x,y) (x)>(y)?(x):(y)
the struct head.x =100 main()
Ans: above is correct / { int i=10,j=5,k=0;
wrong k= MAX(i++,++j);
expl: Before using the printf("%d %d %d ",i,j,k);
ptr type struct variable }
we have to give memory
to that .And also when ans. 12 6 11
ever the struct variable
is ptr then we access 8. main()
the {
members by "->" int a=10,b=5, c=3,d=3;
operator. if (a<b)&&(c=d++)
printf(“%d %d %d %d”,
4. a,b,c,d);
main() else
{ printf("%d %d %d %d”,
int i; a,b,c,d);
i=1; }
i=i+2*i++;
printf(%d,i);} ans: 10 5 3 3 Note: if condition
ans: 4 should be in braces
5. main() 9. main()
{ {
FILE *fp1,*fp2; int i = 10;
fp1=fopen("one","w") printf(" %d %d %d \n", ++i,
fp2=fopen("one","w") i++, ++i);
fputc('A',fp1) }
fputc('B',fp2)
fclose(fp1) ans: 13 11 11
fclose(fp2)} 10. main()
a.error b. c. d. {
ans: no error. But It will int *p, *c, i;
over writes on same file. i = 5;
p = (int*)
6. #include<malloc.h> (malloc(sizeof(i)));
char *f() printf("\n%d",*p);
{char *s=malloc(8); *p = 10;
strcpy(s,"goodbye");} printf("\n%d %d",i,*p);
main() c = (int*) calloc(2);
{ printf("\n%d\n",*c);
char *f(); }
Note: calloc function y>>=3;PRINT(y);
has less parameters calloc(n, }
elemsize)
ans: int = 0 int = 3 int =
0
main()
{ 15. main()
int *p, *c, i; {
i = 5; char s[] = "Bouquets and
p = (int*) Brickbats";
(malloc(sizeof(i))); printf("\n%c, ",*(&s[2]));
printf("\n%d",*p); printf("\n%s, ",s+5);
*p = 10; printf("\n%s,",s);
printf("\n%d %d",i,*p); printf("\n%c",*(s+2));
c = (int*) calloc(2,2); }
printf("\n%d\n",*c);
} ans: u,
ans: garbage, 5, 10, 0 (malloc gives ets and Brickbats,
garbage and calloc initializes with Bouquets and
zeros) Brickbats,
u
11. #define MAX(x,y) (x) >(y)?(x):(y) 16. main()
main() {
{ struct s1
int i=10,j=5,k=0; {
k= MAX(i++,++j); char *str;
printf("%d..%d..%d",i,j,k); struct s1 *ptr;
} };
static struct s1 arr[] =
ans: 12 6 11 { {"Hyderabad",arr+1},
{"Bangalore",arr+2
12. main() },
{ {"Delhi",arr}
enum _tag{ left=10, right, };
front=100, back}; struct s1 *p[3];
printf("left is %d, right is int i;
%d, front is %d, back is for(i=0;i<=2;i++)
%d",left,right,front,back); p[i] = arr[i].ptr;
}
printf("%s\n",(*p)-
ans: left is 10, right is >str);
11, front is 100, back is 101 printf("%s\n",(+
+*p)->str);
13. main() printf("%s\n",((*p)+
{ +)->str);
int a=10,b=20;
a>=5?b=100:b=200; }
printf("%d\n",b);
} ans: Bangalore
Delhi
ans: lvalue required for Delhi
ternary operator
17. main()
14. #define PRINT(int) printf("int = %d {
",int) char *p = "hello world!";
main() p[0] = 'H';
{ printf("%s",p);
int x,y,z; }
x=03;y=02;z=01;
PRINT(x^x); ans: Hello world
z<<=3;PRINT(x);
18. main() {
{
char c[]={ "
int x=1,y=1;
while( (x > 0) && (y > 0) ) enter" , "first" , "print" ,
{
printf("%16d%16d",x,y); "new" }.;
x += y;
y += x; char **cp[]={c+3,
} c+2, c+1, c};
}
char ***cpp[]=cp;
ans: here x = x+y and y
printf("%s", ++*cp);
= x+2y when y goes
beyond 32767 it falls in
printf("%s",--*++cp);
–ve side and loop breaks
}
{ ans:
main()
i++; {
t += 2; int i,j;
for(i=1;i<=5;i++)
s += s; {
} printf("\n");
for(j=i;j>0;j--)
return i; printf("%d",i);
} }
33. main()
30. main() {
{ char *p1="Name";
int x=10,y=15; char *p2;
x=x++; p2=(char *)malloc(20);
y=++y; while(*p2++=*p1++);
printf("%d %d\n",x,y); printf("%s\n",p2);
} }
Modify_value() ans: 5 20 1
{
return (x+=10);
} 35. #define swap1(a,b) a=a+b;b=a-
b;a=a-b;
change_value() main()
{ {
return(x+=1); int x=5,y=10;
} swap1(x,y);
printf("%d %d\n",x,y);
ans: swap2(x,y);
printf("%d %d\n",x,y);
First output : 12 }
Second output : 1
Third output : 1 int swap2(int a,int b)
{
32. main() int temp;
temp=a; Ans: b will execute faster.
b=a;
a=temp; 40. main()
return; {
} int a=1,b=2,c=3;
printf("%d,%d",a,b,c);
ans: }
10 5
10 5 ans: 1, 2
41. main()
{
36. main() struct
{ char *ptr = "Ramco {
Systems"; char a[3];
(*ptr)++; int b;
printf("%s\n",ptr); }x;
ptr++; char *cp;
printf("%s\n",ptr); printf(“%d
} %d”,sizeof(cp),sizeof(x));
}
ans:
Samco Systems ans: 4 5 since pointer
amco Systems cp stores address(32-
bit) 4 bytes it takes and
and x takes 5 bytes(3
37. main() for character array a
{ char s1[]="Ramco"; and 2 for int b)
char s2[]="Systems";
s1=s2;
printf("%s",s1); 42. main()
} {
int p=3,q=4;
ans: lvalue required (s1 q = shw(&p);
is base address of printf("%d %d",p,q);
array) }
49. main()
{
46. const char * int i=10;
char * const printf("%d %d %d",i,++i,i+
What is the differnce between the +);
above two? }
main() Code 2 :
{ for(i=0; i<100; i++)
static int i=0; for(j=0; j<1000; j++)
number(i); x = y;
}
Which code will execute faster
ans: lvalue required (function
name is an address. So ++ ans: Code2 (Code 1 =
operator should not be applied) 1,01000 increment operations)
(Code 2 =
72. main() 1,00100 increment operations)
{
unsigned char i; 76. main()
int sum; {
for(i=0; i<300; i++) int a[10] = {1, 2, 3, 4, 5, 6,
sum+ = i; 7, 8, 9, 10}, i, x=10, temp;
printf("\nSum = %d\n", for(i=0; i<x; i++){
sum); temp = a[i];
} a[i] = a[x-i-1];
a[x-i-1] = temp;
ans: infinite loop }
ans: 6 main()
{
int a[2][3][4]={ unsigned short a=-1;
{2,1,4,3,6,5,8,7,0,9,2,2}, unsigned char b=a;
{1,2,3,4,5,6,7,8,9,0,1,2} printf("%d %d ",a,b);
}; }
printf("%u %u %u
%u",a,*a,**a,***a); ans: -1 255 (%d format
} specifier)
printf("%d",scanf("%d",&a)); ans: 33
}
210. main()
ans: it will wait for a {
character from keyboard. If u int *i;
enter any number int s=(int
it will print 1. *)malloc(10*sizeof(int));
for (i=0;i<10;i++)
205. main() {
{ printf("%d",i*i);
printf("as"); }
printf("\bhi"); }
printf("is\n");
} ans: error (Nonportable
pointer conversion and illegal use
ans: ahiis (\b is pointer i*i)
backspace. So s is erased)
211. array’s base address is
206. main() 1000....array is a[5][4]..then wat is
{ de
correct address of a[4][3]...Each }
element takes 4 bytes
ans: 2 1
261. main()
256. find(int x,int y); {
main() printf("%d", strlen(""));
} use [^\n] it takes
multiple strings till it
ans: 0 (strlen excludes encounters newline
null character. It is a null string) (i.e., enter is pressed)
ans: 11 360.
main()
355. main() {
{ int x = 5;
int b; printf("%d %d", x++, +
b=f(20); +x);
printf("%d",b); return 0;
} }
f(int a)
{ ans: 6 6
a>20 ? return (10):
return (20); 361. main()
} {
int z = 4;
ans: error in function printf("%d", printf(" %d %d
definition ", z, z));
}
356. main()
{ ans: 4 4 5 (three spaces
int b; are there total five
b=f(20); characters will be
printf("%d",b); printed by printf
} statement)
f(int a)
{ 362. main()
return a>20 ? (10): (20); {
int z = 45; k = k/2;
printf("%d", printf(" %d %d printf(“%f%f ”, *j, *k);
", z, z)); }
}
ans: error (pointer
ans: 45 45 7 multiplication and
division is illegal)
363. main( )
{ 368. main( )
int a[ ] = { 10, 20, 30, 40, {
50}; static char s[ ] =
int j; “Rendezvous”;
for (j = 0; j < 5; j++) printf(“%d”, *(s+
{ strlen(s)));
printf("%d", * a); }
a++;
} ans: 0
}
369. main()
ans: lvalue required {
char **p="Hello";
364. main() printf("%c",*p);
{ }
Int n=20, i = 0;
while(n-->0); ans: H
i = i+n;
printf("%d",i); 370. main()
} {
char **p="Hello";
ans: -1 printf("%s",p);
}
365. main()
{ ans: Hello
int i = 0; char ch = ‘A’
do { 371. main()
printf(“%c”, ch); {
} while (i++ <5| | ++ch < char **p="Hello";
=’F’); printf("%s",*p); /* (or)
} printf(“%s”,**p); */
}
ans: AAAAAABCDEF
ans: error
366. int count, sum;
main() 372. main()
{ {
for(count = 4; sum += char **p="Hello";
--count;); printf("%c",**p);
printf("%d", sum); }
}
ans: error
ans: 0
373. main()
367. main( ) {
{ char a[]="Hello";
static float a[ ] = { 13.24, printf("%c\n",*a++);
1.5} }
float *j, *k;
j = a; ans: lvalue required
k = a + 2;
j = j * 2; 374. main()
{
int a=3,b=2,c=1; if(i)
static int k= a<b<c-1; main();
printf("%d",k); }
}
ans: 5 4 3 2 1
ans: illegal initialization
(for static initializer 379. main()
should be constant {
expression or constant) int a=5,c;
int ptr;
375. main() ptr=&a;
{ c=*ptr * a;
int a=3,b=2,c=1; printf("%d,%d",c,a);
int k= a<b<c-1; }
printf("%d",k);
} ans: error (nonportable
pointer conversion and
ans: 0 invalid indirection)
457. main()
{
int a[]={0,2,4,6,8}; #define putchar(c) printf("%c",c)
int *ptr; main()
ptr=a; {
printf("%d", *((char *) int c='d';
ptr+4)); putchar(c);
} }
ans: 4 ans: d
ans: 10 ans: 10
default : ans: c
ctr++;
}; 487. #define putchar (c)
printf("%d",ctr); printf("%c",c)
getch(); void main()
} {
char s='c';
ans: 3 putchar (s);
}
482. #define putchar(c) printf("%c",c);
main() ans: error (gap should
{ not be there between putchar and
int c=69; (c) )
putchar(c);
} 488. void main()
{
ans: E int a[]={9,4,1,7,5};
int *p;
483. main() p=&a[3];
{ printf("%d",p[-1]);
} return(&i);
}
ans: 1
ans: we can't return address of
489. void main() auto variable as it
{ is allocation is made in stack
int a[]={10,20,30,40,50}; which is deallocated
int *p; when the function returns.
p= (int*)((char *)a +
sizeof(int)); 502. (1)To find string length by using
printf("%d",*p); recursive function.
} (2)To find fibonaci series by using
recursive
ans: 20 function.
(3)To write code for malloc so that
490. Which code will run faster allocation may be
made fastly.
for(i=0;i<100;i++) (4)Write a fn prototype which
for(j=0;j<10;j++) return a pointer which
a[i][j]=0; points to an array of 10 ints.
578. In the following code, is p2 an 583. What is the type of the variable b
integer or an integer pointer? in the following declaration?
typedef int* ptr #define FLOATPTR float*
ptr p1,p2; FLOATPTR a,b;
ans: 89 8a ans: 3 7
ans: 10 9 10 ans: 5 0
ans: 12 12 10 ans: 20 6
main()
{ 1009. will these two work in same
char *a="new"; manner
x(a); #define intp int *
printf("%s",a); typedef int * inpp;
}
ans: no
ans: error (nonportable
pointer conversion) #define intp int *
typedef int * inpp;
1006. int x(char *a) main()
{ {
char *b; inpp t1,t2;
a=(char *) intp m1,m2;
malloc(10*sizeof(char)); printf("%d %d %d
b=(char *) %d",sizeof(t1),sizeof(t2),sizeof(m1)
malloc(10*sizeof(char)); ,sizeof(m2));
a="hello"; }
b=a;
} ans: 4 4 4 2 (t1,t2 and
m1 are pointers and m2 is
main() integer)
{
char *a="new"; 1010. #define max 10
x(a); main()
printf("%s",a); {
} int a,b;
int *p,*q;
ans: new a=10;b=19;
p=&(a+b);
1007. int x(char *a) q=&max;
{ }
char b[10];
a=(char *) ans: error (& must take
malloc(10*sizeof(char)); address of a memory location)
a="hello";
b=a; 1011. main()
} {
char S[6]= "HELLO";
main() printf("%s ",S[6]);
{ }
(D) Instead of %f , %n should be
ans: error (trying to used for formatting
print from memory location
zero) ans: (A)
ans: 12 29 ans: 20
1060. main() for (;;);
{ {
int i = 5; if(i==1)
printf("%d\n", i++*i--); {
} printf("%d",i);
exit();
ans: 30 }
}
1061. main() }
{
int i = 5; ans: infinite loop (no
printf("%d %d", i,i++*i--*i+ output)
+);
} 1066. const int n = 7;
int a[n];
ans: 6 150 main()
{
1062. main() ;
{ }
char ch='a';
printf("%d ",ch); ans: error (constant
printf("%d",((int)ch)++); expression required for array
} size)
1138. Given an array of integers, find the 1142. Write a program to find whether a
contiguous sub-array with the given m/c is big-endian or little-
largest sum. endian!
ans: Start reversing the list. Do this 1150. Compute the number of ones in an
again, printing the contents. unsigned integer.
1154. You're given an array containing 1163. Tell about strtok & strstr functions.
both positive and negative integers
and required to find the sub-array 1164. #define int sizeof(int)
with the largest sum (O(N) a la main()
KBL). Write a routine in C for the {
above. printf("%d",int);
}
1155. Given two strings S1 and S2.
Delete from S2 all those characters ans: 2
which occur in S1 also and finally
create a clean S2 with the relevant 1165. #define i sizeof(i)
characters deleted. main()
{
1156. Besides communication cost, what printf("%d",i);
is the other source of inefficiency in }
RPC? (answer : context switches,
excessive buffer copying). How can ans: error (undefined
you optimize the communication? symbol i)
(ans : communicate through shared
memory on same machine,
bypassing the kernel _ A Univ. of
Wash. thesis)