C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 16)
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;
}
1, 2, 4
1, 4, 4
2, 2, 4
2, 4, 8
Answer: Option
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

Discussion:
19 comments Page 2 of 2.

Yogesh said:   1 decade ago
In single quotation there can be only character. Correct. Ok then sizeof ('A') will be simply sizeof (char). So it will definitely return 1 byte. Obviously.

Bala said:   1 decade ago
@Neeraj:
How come sizeof('as') return 2 bytes? In single quote can ther be two characters?

Neeraj said:   1 decade ago
sizeof('A')=it will always return 1 since there can be only 1 character in single quotes '' and the size of char is 1 bytes.

sizeof("A")=it will return 2 bytes there are 2 characters in it (A and \0)as the compiler inserts the null character automatically.

Sattibabu said:   1 decade ago
sizeof(A)=size of(65)=sizeof(int)=2bytes

Sajal said:   1 decade ago
Please explain the reason.

HITESH BHATEJA said:   1 decade ago
@SRIVIBHA,

I agree with you..

sizeof('A') returns 1 byte because A is character not integer so it always returns character value.

Mantosh tiwari said:   1 decade ago
There are given that the int is of size 4 bytes so it will print 1 4 4 ..

Srivibha said:   1 decade ago
It prints 1 1 4 in turbo c++ compiler.

Praveen said:   1 decade ago
Additional Info:

If 3.14f mentioned like 3.14 then it will be assumed as double. So answer will be 8.
(1)


Post your comments here:

Your comments will be displayed after verification.