Output of C programs | Set 46
Last Updated :
19 Oct, 2022
QUE.1 What would be output following C program?
#include <stdio.h>
int main()
{
char * str = "IncludeHelp" ;
printf ( "%c\n" , *&*str);
return 0;
}
|
OPTION
a) Error
b) IncludeHelp
c) I
d)*I
Answer: c
Explanation : & is a reference operator, * is de-reference operator. We can use these operators any number of times. str points the first character of IncludeHelp, *str points “I”, * & again reference and de-reference the value of str.
QUE.2 What would be output following C program?
#include <stdio.h>
int main()
{
int iVal;
char cVal;
void * ptr;
iVal = 50;
cVal = 65;
ptr = &iVal;
printf ( "value =%d, size= %d\n" , *( int *)ptr, sizeof (ptr));
ptr = &cVal;
printf ( "value =%d, size= %d\n" , *( char *)ptr, sizeof (ptr));
return 0;
}
|
OPTION
a) Error
b) value =50, size= 4
value =65, size= 4
c) value =50, size= 4
value =65, size= 1
d)Garbage Values
Answer: b
Explanation : void pointer can be type casted to any type of data type, and pointer takes 4 bytes (On 32 bit compiler). To print value using void pointer, you will have to write like this *(data_type*)void_ptr;.
QUE.3 What would be output following C program?
#include <stdio.h>
int main()
{
char ch = 10;
void * ptr = &ch;
printf ( "%d, %d" , *( char *)ptr, ++(*( char *)ptr));
return 0;
}
|
OPTION
a) 11, 11
b) 10, 11
c) ERROR
d) 10, 10
Answer: a
Explanation : *(char*)ptr will return the value of ch, since we know printf evaluates right to left. So, ++(*(char*)ptr) will increase the value to 11.
QUE.4 What is the output of the following C program?
#include <stdio.h>
int main()
{
int a = 10, b = 2;
int * pa = &a, *pb = &b;
printf ( "value = %d" , *pa/*pb);
return 0;
}
|
OPTION
a) 5
b) 5.0
c) ERROR
d) None of these
Answer:c
Explanation : ERROR: unexpected end of file found in comment. The compiler is treated the operator / and * as /*, that happens to be the starting of comment. To fix the error, use either *pa/ *pb (space between operators) or *pa/(*pb).
QUE.5 What is the output of the following C program?
#include <stdio.h>
int main()
{
int a = 10;
int b = 2;
int c;
c = (a & b);
printf ( "c= %d" , c);
return 0;
}
|
OPTION
a) c= 12
b) c= 10
c) c= 2
d) c= 0
Answer:c
Explanation : Bitwise AND (&) operator copies bit(s), if there exist both of the operands. Here, binary of a is “1010” and binary of b is “0010”. Thus, result of expression (a & b) is “0010” which is equivalent to 2 in Decimal.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...