Output of C programs | Set 48

4.3

1. What is the output of following program?

#include <stdio.h>
#define square(x) (x * x)

int main()
{
    int x, y = 1;
    x = square(y + 1);
    printf("%d\n", x);
    return 0;
}

Options:
(A) Error
(B) 4
(C) 3
(D) Garbage value

Answer : (C)

Explanation : The macro function square(x)(x*x) calculates the square of the given number.

Initially int y = 1;
In the  next step  x = square(y+1); 
 x = y+1 * y+1; Here square(x) is replaced  x*x .
 x = 1+1 * 1+1; 
 x = 1 + 1 + 1; 
 x = 3; 

2. What is the output of following program?
The code given below is executed from command Line.
Input given by the user: myfile 2 4 6

// myprog.c
#include <stdio.h>

int main(int argc, char* argv[])
{
    int result;
    result = argv[2] + argv[4] + argv[6];
    printf("%d", result);
    return 0;
}

Options:
(A) It will reflect a garbage value.
(B) Error
(C) 12
(D) myfile 12

Answer : (B) Error

Explanation : Error because argv[2], argv[4] and argv[6] areof string type and we cannot perform arithmetic operations without converting the string to integer type.

3. What is the output of following program?

#include <stdio.h>
void main()
{
    unsigned char x = 400;
    printf("%d", x);
}

Options:
(A) Error
(B) 144
(C) 400
(D) Garbage value

Answer :(B) 144 

Explanation : Since the input is beyond the range of unsigned char it will reflect the input’s cyclic value.

4.What is the output of following program?

#include <stdio.h>
int main()
{
    int i;
    char input;
    for (i = 1; i <= 5; i++) {
        // The input provided is 'X'
        scanf("%c", &input);
        printf("%c", input);
        ungetc(input, stdin);
    }
    return 0;
}

Options:
(A) Error
(B) X
(C) XXXXX
(D) A garbage value.

Answer:(C)- XXXXX 

Explanation:The loop will run 5 times. Now, we provide the input as ‘X’. Which is scanned as ‘input’.

printf("%c", input);
The above line prints 'X'

Now the ungetc(input, stdin) statement pushes the character’X’ back into input stream.

For the next run of the loop.
The scanf statement gets the input from “stdin” because of “ungetc” function used previously. Now the printf statement will print ‘X’ Since input= ‘X’ and ungetc(input, stdin) pushes ‘X’ back to the input stream and the same process will take place for the remaining iterations.

5. What is the output of following program?

#include <stdio.h>
int main()
{
    printf("Geeksfor ", "Geeks\n");
    return 0;
}

Options:
(A) Geeksfor
(B) GeeksforGeeks
(C) Error
(D) Geeks

Answer:(A) Geeksfor

Explanation : printf() allows variable number of arguments. Since there is no format specifier, second parameter is ignored with warning.

This article is contributed by Siddharth Pandey. 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:



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










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