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;
}
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.
Satyam rastogi said:
1 decade ago
65 is a integer it takes 2 bit.
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.
'A' indicates character then how ill ASCII be considered?
Output on DevC++ gives 1, 1, 4.
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.
And sizeof (ch) = 4 => sizeof ('A') = 4 too.
And Output on DevC++ gives 1, 1, 4.
(1)
Nigam said:
9 years ago
C++ compiler gives 1, 1, 4.
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).
'A' IS A CHARACTER AND SIZEOF() WILL RETURN 1 BYTE. There is no question of sizeof(65).
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".
So option should be "vary according to the compiler".
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.
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.
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.
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++.
If we declare any character with single code 'a'. it is indicates character type. So the answer is 1 1 4 in turbo c++.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers