Output of C programs | Set 37

3

1.) What will be the output of the following code?

#include <stdio.h>
int main(void)
{
    int y, z;
    int x = scanf("%d %d", &y, &z);
    printf("%d", x);
    return 0;
}

Input:

 12 10

a)12
b)2
c)Syntax Error
d)10

Answer : b

Explanation: scanf() returns the number of variables it successfully stored.

2. What will be the output of the following code?

#include <stdio.h>
int main(void)
{
    printf("%d", printf("geeksforgeeks"));
    return 0;
}

a)Syntax Error
b)geeksforgeeks
c)geeksforgeeks13
d)13

Answer : c

Explanation : () returns the length of output(printed by printf) as integer.

3. What will be the output of the following code?

#include <stdio.h>
int add(int a, int b)
{
    if (a != 0 && b != 0)
        return printf("%*c%*c", a, '.', b, '.');
    else
        return a != 0 ? a : b;
}
int main()
{
    int A = 0, B = 0;
    scanf("%d %d", &A, &B);
    printf("Required sum is %d", add(A, B));
    return 0;
}

Input:

22 10

a)42
b)Compilation Error
c)Required sum is 32
d)32

Answer : c

Explanation : printf() adds any numbers without using the addition operator .

4. What will be the output of the following code?

#include <stdio.h>
int main()
{
    printf("%m");
    return 0;
}

a) Success
b)Compilation Error
c)%m
d)None of the above.

Answer : a

Explanation : “%m” when used within printf() prints “Success”. The ‘%m’ conversion is a GNU C Library extension.

5. What will be the output of the following code?

#include <stdio.h>
double m[] = { 7709179928849219.0, 771 };
int main()
{
    m[1]-- ? m[0] *= 2, main() : printf((char*)m);
}

a) C++Sucks
b)Compilation Error
c)714
d)770

Answer: a

Explanation :
The number 7709179928849219.0 has the following binary representation as a 64-bit double:
01000011 00111011 01100011 01110101 01010011 00101011 00101011 01000011
+^^^^^^^ ^^^^—- ——– ——– ——– ——– ——– ——–
+ shows the position of the sign; ^ of the exponent, and – of the mantissa (i.e. the value without the exponent).
Since the representation uses binary exponent and mantissa, doubling the number increments the exponent by one. This program does it precisely 771 times, so the exponent which started at 1075 (decimal representation of 10000110011) becomes 1075 + 771 = 1846 at the end; binary representation of 1846 is 11100110110. The resultant pattern looks like this:
01110011 01101011 01100011 01110101 01010011 00101011 00101011 01000011
——– ——– ——– ——– ——– ——– ——– ——–
0x73 ‘s’ 0x6B ‘k’ 0x63 ‘c’ 0x75 ‘u’ 0x53 ‘S’ 0x2B ‘+’ 0x2B ‘+’ 0x43 ‘C’
This pattern corresponds to the string that we see printed, only backwards. At the same time, the second element of the array becomes zero, providing null terminator, making the string suitable for passing to printf().

6. What will be the output of the following code?

#include <stdio.h>
int main()
{
    printf("geeksforgeeks");
    brk(0);
}

a)geeksforgeeks
b)Compilation Error
c)program will not terminate
d)None of the above

Answer :  a

Explanation : brk(0) terminates the program and acts like return statement.

This article is contributed by Nikita Tiwari. 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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.