Click here to Skip to main content
15,883,938 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
i need explanation on this code for each part and why the answer is 8 4 4 on c language

#include<stdio.h>

int main(){
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d",sizeof('A'));

return 0;
}

What I have tried:

i tried to figure it out but still has no clue i need answer asap
Posted
Updated 26-Oct-21 5:57am

The sizeof operator returns the size of the variable or object provided. In your case you are asking fo the size of i) a double type, ii) an int, and iii) a character. See Storage of Basic Types | Microsoft Docs[^] for the actual sizes.

[edit]
as noted by OriginalGriff, the third item is treated as an int type.
[/edit]
 
Share this answer
 
v2
Comments
OriginalGriff 26-Oct-21 12:00pm    
"iii) a character"
Not so - 'A' is an integer by default - hence the length of 4.
(It's a legacy thing from the first days of the C language)
Richard MacCutchan 26-Oct-21 12:08pm    
Corrected. And yes, I really should know that. :(
OriginalGriff 26-Oct-21 12:46pm    
Easy thing to forget! :laugh:
CPallini 26-Oct-21 14:05pm    
5.
Richard MacCutchan 26-Oct-21 14:17pm    
Thanks, although only 66.6% right. :(
6.5 is a double: 8 bytes long.
90000 is an int: 4 bytes long.
'A' is a int: 4 bytes long.
Try this:
C
char a = 'a';
printf("%zu\n",sizeof('a'));
printf("%zu",sizeof(a));
 
Share this answer
 
Comments
CPallini 26-Oct-21 14:05pm    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900