Skip to content
Related Articles

Related Articles

Improve Article

Output of C programs | Set 46

  • Difficulty Level : Medium
  • Last Updated : 29 Aug, 2017
Geek Week

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

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :