0% found this document useful (0 votes)
12 views

Advance C programs With Explnation

The document contains a series of C programming questions along with their answers and explanations. It covers various topics such as data types, operators, loops, pointers, and function behavior in C. Each question is followed by multiple choice answers and detailed explanations of the output produced by the code snippets.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Advance C programs With Explnation

The document contains a series of C programming questions along with their answers and explanations. It covers various topics such as data types, operators, loops, pointers, and function behavior in C. Each question is followed by multiple choice answers and detailed explanations of the output produced by the code snippets.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

(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;

};

int main(){

struct marks s={2,-6,5};

printf("%d %d %d",s.p,s.c,s.m);

return 0;

(a) 2 -6 5

(b) 2 -6 1

(c) 2 2 1

(d) Compiler error

(e) None of these

Answer: (c)

Explanation:

Binary value of 2: 00000010 (Select three two bit)

Binary value of 6: 00000110

Binary value of -6: 11111001+1=11111010

(Select last three bit)


Binary value of 5: 00000101 (Select last two bit)

Complete memory representation:

(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

(d) cpointer null null

(e) c-point

Answer: (e)
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:


(3) 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.


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.
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:
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 of function. Word main can be name variable in
the main and other functions.

(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:

(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

(c) 23

(d) Compiler error

(e) None of these

Answer: (c)

Explanation:

extern variables can search the declaration of variable

anywhere in the program.

(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

(a) 5

(b) 3

(c) 1

(d) equal
(e) None of above

printf("equal");

(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/

(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:
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:

# 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:

#include<stdio.h>

int main(){

if(printf("cquestionbank"))
printf(“I Know c”);
else
printf(“I Know c++”);

return 0;

(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>
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(){
printf("%s",__DATE__);

return 0;
}

OPERATORS

What will be output of the following program?

#include<stdio.h>
int main(){
float a=0.7;d
if(a<0.7){
printf("C");
}
else{
printf("C++");
}
return 0;
}

Output:

Turbo C++ 3.0: c

What will be output of the following program?

#include<stdio.h>
int main(){
int i=5,j;
j=++i+++i+++i;
printf("%d %d",i,j);
return 0;
}

Output:

Turbo C++ 3.0: 8 24

Turbo C ++4.5: Compilation error

(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

Turbo C ++4.5: 5 20 100

Linux GCC: Garbage values

Visual C++: 5 100 20

By default x, y, z are auto type data which are stored


in stack in memory. Stack is LIFO data structure. So in
stack first stores 100 then 20 then 5 and program
counter will point top stack i.e. 5. Default value of
%d in printf is data which is present in stack. So
output is revere order of declaration. So output will
be 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)

What will be output of following c code?

#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.

(9)What will be output of following c code?

#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++ 3.0: 64

Turbo C ++4.5: 64

Linux GCC: 64

Visual C++: 64

As we know int is two byte data byte while char is one


byte data byte. char pointer can keep the address one
byte at time.

Binary value of 320 is 00000001 01000000 (In 16 bit)


Memory representation of int a = 320 is:

So ptr is pointing only first 8 bit which color is


green and Decimal value is 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

Linux GCC: Compilation error

Visual C++: Compilation error

p is pointer to function whose parameter is void and


return type is also void. r and q is pointer to
function whose parameter is void and return type is int
. So they can hold the address of such function.

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)

Turbo C ++4.5: Run time error


Linux GCC: Run time error

Visual C++: Run time error

We cannot assign any string constant in null pointer by


strcpy function.

You might also like