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.

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)

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)

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++.

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.

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.

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".

MINDMASTER said:   9 years ago
ITS 1,1,4.

'A' IS A CHARACTER AND SIZEOF() WILL RETURN 1 BYTE. There is no question of sizeof(65).

Nigam said:   9 years ago
C++ compiler gives 1, 1, 4.

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.


Post your comments here:

Your comments will be displayed after verification.