Advance C programs With Explnation
Advance C programs With Explnation
#include<stdio.h>
struct marks{
int p:3;
int c:3;
int m:2;
};
int main(){
printf("%d %d %d",s.p,s.c,s.m);
return 0;
(a) 2 -6 5
(b) 2 -6 1
(c) 2 2 1
Answer: (c)
Explanation:
(2) #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
(e) c-point
Answer: (e)
Meaning of %*.*s in the printf function:
#include<stdio.h>
int main(){
int a=-12;
a=a>>3;
printf("%d",a);
return 0;
(a) -4
(b) -3
(c) -2
(d) -96
Answer :( c)
Explanation:
Since it is negative number so output will also a negative number but its 2’s complement.
Hence final output will be:
#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
Answer: (d)
Explanation:
Sizeof operator returns the size of string including
int main(){
static main;
int x;
x=call(main);
printf("%d ",x);
return 0;
address++;
return address;
(a) 0
(b) 1
Answer: (b)
Explanation:
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.
#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
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:
#include<stdio.h>
int extern x;
int main()
printf("%d",x);
x=2;
return 0;
}
int x=23;
(a) 0
(b) 2
(c) 23
Answer: (c)
Explanation:
#include<stdio.h>
int main(){
int i=0;
if(i==0){
i=((5,(i=3)),i=1);
printf("%d",i);
else
(a) 5
(b) 3
(c) 1
(d) equal
(e) None of above
printf("equal");
int main(){
int a=25;
printf("%o %x",a,a);
return 0;
(a) 25 25
(c) 12 42
(d) 31 19
Answer: (d)
Explanation:
format.
#include<stdio.h>
int main(){
printf("%s",message);
return 0;
(c) union is
Power of c
Answer: (b)
Explanation:
If you want to write macro constant in new line the end with the character \.
#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:
file:
test.c 1:
test.c 3: printf("%s","c/c++");
test.c 4: return 0;
test.c 4: }
test.c 5:
#include<stdio.h>
int main(){
if(printf("cquestionbank"))
printf(“I Know c”);
else
printf(“I Know c++”);
return 0;
(a) I know c
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.
#include<stdio.h>
int main(){
int i=10;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
return 0;
(a) Equal
Answer: (d)
Explanation:
time variable.
(20) What will be output if you will compile and
#include<stdio.h>
int main(){
printf("%s",__DATE__);
return 0;
}
OPERATORS
#include<stdio.h>
int main(){
float a=0.7;d
if(a<0.7){
printf("C");
}
else{
printf("C++");
}
return 0;
}
Output:
#include<stdio.h>
int main(){
int i=5,j;
j=++i+++i+++i;
printf("%d %d",i,j);
return 0;
}
Output:
(3)
What will be output of the following program?
#include<stdio.h>
int main(){
int i=1;
i=2+2*i++;
printf("%d",i);
return 0;
}
(4)
What will be output of the following program?
#include<stdio.h>
int main(){
int a=2,b=7,c=10;
c=a==b;
printf("%d",c);
return 0;
}
6)
What will be output of the following program?
#include<stdio.h>
int main(){
int a=0,b=10;
if(a=0){
printf("true");
}
else{
printf("false");
}
return 0;
}
(9)
What will be output of the following program?
#include<stdio.h>
int main(){
int x=100,y=20,z=5;
printf("%d %d %d");
return 0;
}
EXPLANATION
Output:
Turbo C++ 3.0: 5 20 100
(11)
What will be output of the following program?
#include<stdio.h>
int main(){
int a;
a=sizeof(!5.6);
printf("%d",a);
return 0;
}
EXPLANATION
Output:
Turbo C++ 3.0: 2
Turbo C ++4.5: 2
Linux GCC: 4
Visual C++: 4
Explanation:
! is negation operator it return either integer 0 or 1.
! Any operand = 0 if operand is non zero.
! Any operand = 1 if operand is zero.
So, !5.6 = 0
Since 0 is integer number and size of integer data type
is two byte.
LOOPS
(1)
#include<stdio.h>
extern int x;
int main(){
do{
do{
printf("%o",x);
}
while(!-2);
}
while(0);
return 0;
}
int x=8;
(2)
What will be output of following c code?
#include<stdio.h>
int main(){
int i=2,j=2;
while(i+1?--i:j++)
printf("%d",i);
return 0;
}
EXPLANATION
Output: 1
Explanation:
Consider the while loop condition: i + 1 ? -- i : ++j
In first iteration:
i + 1 = 3 (True)
So ternary operator will return -–i i.e. 1
In c 1 means true so while condition is true. Hence
printf statement will print 1
In second iteration:
i+ 1 = 2 (True)
So ternary operator will return -–i i.e. 0
In c zero means false so while condition is false.
Hence program control will come out of the while loop.
#include<stdio.h>
#define p(a,b) a##b
#define call(x) #x
int main(){
do{
int i=15,j=3;
printf("%d",p(i-+,+j));
}
while(*(call(625)+3));
return 0;
}
EXPLANATION
Output: 11
Explanation:
First iteration:
p(i-+,+j)
=i-++j // a##b
=i - ++j
=15 – 4
= 11
While condition is : *(call(625)+ 3)
= *(“625” + 3)
Note: # preprocessor operator convert the operand into
the string.
=*(It will return the memory address of character ‘\0’)
= ‘\0’
= 0 //ASCII value of character null character
Since loop condition is false so program control will
come out of the for loop.
POINTERS
1.
What will be output of following program?
#include<stdio.h>
int main(){
int a = 320;
char *ptr;
ptr =( char *)&a;
printf("%d ",*ptr);
return 0;
}
(A) 2
(B) 320
(C) 64
(D) Compilation error
(E) None of above
E x p l a n a t i o n :
Turbo C ++4.5: 64
Linux GCC: 64
Visual C++: 64
2.
What will be output of following program?
#include<stdio.h>
#include<conio.h>
int main(){
void (*p)();
int (*q)();
int (*r)();
p = clrscr;
q = getch;
r = puts;
(*p)();
(*r)("cquestionbank.blogspot.com");
(*q)();
return 0;
}
(A) NULL
(B) cquestionbank.blogspot.com
(C) c
(D) Compilation error
(E) None of above
E x p l a n a t i o n :
Turbo C++ 3.0: cquestionbank.blogspot.com
Turbo C ++4.5: cquestionbank.blogspot.com
5.
What will be output of following program?
#include<stdio.h>
#include<string.h>
int main(){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
return 0;
}
(A) c questions
(B) c (null)
(C) (null) (null)
(D) Compilation error
(E) None of above
E x p l a n a t i o n :
Turbo C++ 3.0: (null) (null)