C Programming - Strings

Exercise :: Strings - Find Output of Program

16. 

If char=1, int=4, and float=4 bytes size, What will be the output of the program ?

#include<stdio.h>

int main()
{
    char ch = 'A';
    printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
    return 0;
}

A. 1, 2, 4
B. 1, 4, 4
C. 2, 2, 4
D. 2, 4, 8

Answer: Option B

Explanation:

Step 1: char ch = 'A'; The variable ch is declared as an character type and initialized with value 'A'.

Step 2:

printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14));

The sizeof function returns the size of the given expression.

sizeof(ch) becomes sizeof(char). The size of char is 1 byte.

sizeof('A') becomes sizeof(65). The size of int is 4 bytes (as mentioned in the question).

sizeof(3.14f). The size of float is 4 bytes.

Hence the output of the program is 1, 4, 4


17. 

If the size of pointer is 32 bits What will be the output of the program ?

#include<stdio.h>

int main()
{
    char a[] = "Visual C++";
    char *b = "Visual C++";
    printf("%d, %d\n", sizeof(a), sizeof(b));
    printf("%d, %d", sizeof(*a), sizeof(*b));
    return 0;
}

A. 10, 2
2, 2
B. 10, 4
1, 2
C. 11, 4
1, 1
D. 12, 2
2, 2

Answer: Option C

Explanation:

No answer description available for this question. Let us discuss.

18. 

What will be the output of the program ?

#include<stdio.h>

int main()
{
    static char mess[6][30] = {"Don't walk in front of me...", 
                               "I may not follow;", 
                               "Don't walk behind me...", 
                               "Just walk beside me...", 
                               "And be my friend." };

    printf("%c, %c\n", *(mess[2]+9), *(*(mess+2)+9));
    return 0;
}

A. t, t
B. k, k
C. n, k
D. m, f

Answer: Option B

Explanation:

No answer description available for this question. Let us discuss.

19. 

What will be the output of the program ?

#include<stdio.h>

int main()
{
    char str1[] = "Hello";
    char str2[10];
    char *t, *s;
    s = str1;
    t = str2;
    while(*t=*s)
        *t++ = *s++;
    printf("%s\n", str2);
    return 0;
}

A. Hello
B. HelloHello
C. No output
D. ello

Answer: Option A

Explanation:

No answer description available for this question. Let us discuss.

20. 

What will be the output of the program ?

#include<stdio.h>

int main()
{
    char str[] = "India\0BIX\0";
    printf("%d\n", sizeof(str));
    return 0;
}

A. 10
B. 6
C. 5
D. 11

Answer: Option D

Explanation:

The following examples may help you understand this problem:

1. sizeof("") returns 1 (1*).

2. sizeof("India") returns 6 (5 + 1*).

3. sizeof("BIX") returns 4 (3 + 1*).

4. sizeof("India\0BIX") returns 10 (5 + 1 + 3 + 1*).
    Here '\0' is considered as 1 char by sizeof() function.

5. sizeof("India\0BIX\0") returns 11 (5 + 1 + 3 + 1 + 1*).
    Here '\0' is considered as 1 char by sizeof() function.





Have a question?

Ask your question now !