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 1 of 2.

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.

Pranali said:   8 years ago
@Hema.

The computer knows only binary language i.e. numeric data.

Each and every instruction given by the user is converted into binary format. And then for 'char' also internally it takes ASSCII value.

Pranali said:   8 years ago
@Xa, your prediction is wrong.

According to architecture, the size of datatype varies.
In 32 arch, int=4,char=1,float=4,double=8.
in 16 arch, int=2,char=1,float=2,etc.

Xa said:   1 decade ago
I think 1, 1, 4 because with char ch[] = 'A'; => sizeof (ch) = size ('A').

And sizeof (ch) = 4 => sizeof ('A') = 4 too.

And Output on DevC++ gives 1, 1, 4.
(1)

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.

Shahebaz said:   9 years ago
The size of data types may vary according to the compiler. The 16-bit compiler has 2 bytes for int.

So option should be "vary according to the compiler".

Hema said:   1 decade ago
sizeof('A') is asked. How is 65 taken into consideration?

'A' indicates character then how ill ASCII be considered?

Output on DevC++ gives 1, 1, 4.

Raj said:   1 decade ago
Integer takes two bytes and you are saying it takes four bytes how it can be possible or you clearly mention that it is compiled on linux platform.

Srinivas said:   8 years ago
Here sizeof('A') is asked.

If we declare any character with single code 'a'. it is indicates character type. So the answer is 1 1 4 in turbo c++.

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.


Post your comments here:

Your comments will be displayed after verification.