Find Output Of Program

Question 6 :

What will be the output of the program?

#include < stdio . h >
#include

int main()
{
    char dest[] = {97, 97, 0};
    char src[] = "aaa";
    int i;
    if((i = memcmp(dest, src, 2))==0)
        printf("Got it");
    else
        printf("Missed");
    return 0;
}


A). Missed
B). Got it
C). Error in memcmp statement
D). None of above
Answer : Option B

Explanation :

memcmp compares the first 2 bytes of the blocks dest and src as unsigned chars. So, the ASCII value of 97 is 'a'.
if((i = memcmp(dest, src, 2))==0) When comparing the array dest and src as unsigned chars, the first 2 bytes are same in both variables.so memcmp returns '0'.
Then, the if(0=0) condition is satisfied. Hence the output is "Got it".


Question 7 :

What will function gcvt() do?


A). Convert vector to integer value
B). Convert floating-point number to a string
C). Convert 2D array in to 1D array.
D). Covert multi Dimensional array to 1D array
Answer : Option B

Explanation :

The gcvt() function converts a floating-point number to a string. It converts given value to a null-terminated string.

#include < stdlib . h >
#include  < stdio . h >
int main(void)
{
    char str[25];
    double num;
    int sig = 5; /* significant digits */

    /* a regular number */
    num = 9.876;
    gcvt(num, sig, str);
    printf("string = %s\n", str);

    /* a negative number */
    num = -123.4567;
    gcvt(num, sig, str);
    printf("string = %s\n", str);

    /* scientific notation */
    num = 0.678e5;
    gcvt(num, sig, str);
    printf("string = %s\n", str);

    return(0);
}
Output:
string = 9.876
string = -123.46
string = 67800


Question 8 :

What will be the output of the program?

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


A). aaaa
B). aaaaa
C). Garbage value.
D). Error in ungetc statement.
Answer : Option B

Explanation :

for(i=1; i<=5; i++) Here the for loop runs 5 times.

Loop 1:
scanf("%c", &c); Here we give 'a' as input.
printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

Loop 2:
Here the scanf("%c", &c); get the input from "stdin" because of "ungetc" function.
printf("%c", c); Now variable c = 'a'. So it prints the character 'a'.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.

This above process will be repeated in Loop 3, Loop 4, Loop 5.