C Programming - Strings - Discussion

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


Praveen said: (Sep 13, 2010)  
Additional Info:

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

Srivibha said: (Nov 3, 2010)  
It prints 1 1 4 in turbo c++ compiler.

Mantosh Tiwari said: (Nov 12, 2010)  
There are given that the int is of size 4 bytes so it will print 1 4 4 ..

Hitesh Bhateja said: (Jan 19, 2011)  
@SRIVIBHA,

I agree with you..

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

Sajal said: (Sep 13, 2011)  
Please explain the reason.

Sattibabu said: (Nov 11, 2011)  
sizeof(A)=size of(65)=sizeof(int)=2bytes

Neeraj said: (Mar 7, 2012)  
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.

Bala said: (Jul 8, 2012)  
@Neeraj:
How come sizeof('as') return 2 bytes? In single quote can ther be two characters?

Yogesh said: (Oct 2, 2012)  
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.

Raj said: (Jul 24, 2013)  
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.

Satyam Rastogi said: (Jun 25, 2015)  
65 is a integer it takes 2 bit.

Hema said: (Jul 28, 2015)  
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.

Xa said: (Aug 24, 2015)  
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.

Nigam said: (Jun 13, 2016)  
C++ compiler gives 1, 1, 4.

Mindmaster said: (Jul 31, 2016)  
ITS 1,1,4.

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

Shahebaz said: (Oct 26, 2016)  
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".

Pranali said: (Apr 1, 2017)  
@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: (Apr 1, 2017)  
@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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.