Output of C programs | Set 46

29 Aug, 2017
QUE.1 What would be output following C program?
#include <stdio.h>int main(){    char* str = "IncludeHelp";    printf("%c\n", *&*str);    return 0;}
OPTIONa) Errorb) IncludeHelpc) Id)*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 pointer    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;}
OPTIONa) Errorb) value =50, size= 4value =65, size= 4c) value =50, size= 4value =65, size= 1d)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;}
OPTIONa) 11, 11b) 10, 11c) ERRORd) 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;}
OPTIONa) 5b) 5.0c) ERRORd) 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;}
OPTIONa) c= 12b) c= 10c) c= 2d) c= 0
Answer:c
Explanation : Bitwise AND (&) operator copies bit(s), if they are 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.
This article is contributed by Ajay Puri(ajay0007). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
C-Operators
C-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Runtime Errors
Different ways to copy a string in C/C++
Output of C Programs | Set 3
Output of C++ Program | Set 1
Output of Java Program | Set 3
Output of C++ programs | Set 47 (Pointers)
Output of Java Programs | Set 12
Output of Java Program | Set 7
unsigned specifier (%u) in C with Examples
Output of Java program | Set 15 (Inner Classes)