retagged by
1,656 views
1 votes
1 votes
#include<stdio.h>
int main(void) {
    printf("bytes occupied by '7' = %d\n", sizeof('7'));
    printf("bytes occupied by 7 = %d\n", sizeof(7));
    printf("bytes occupied by 7.0 = %d\n", sizeof(7.0));
      
	return 0;
}
retagged by

1 Answer

Best answer
4 votes
4 votes

Output:

bytes occupied by '7' = 4
bytes occupied by 7 = 4
bytes occupied by 7.0 = 8

The sizeof operator allows a program to determine how much memory is required to store values of a particular type. The syntax will be 

sizeof(type-name)

Now, $sizeof()$ may give different output according to machine

In a 32-bit gcc compiler

$$sizeof(char) \rightarrow 1 \\ sizeof(int) \rightarrow 4 \\  sizeof(float) \rightarrow 4 \\  sizeof(double) \rightarrow 8  $$

bytes occupied by '7' = 4

In C language, type of a character constant like 'x' [Here, '7'] is actually of type $int$, and thus $sizeof('a')$ [Here, $sizeof('7')$] is equal to $sizeof(int)$ which is $4$.

bytes occupied by 7 = 4

Now, $sizeof(7)$ is $sizeof(int)$ which is $4$

bytes occupied by 7.0 = 8

Now, $7.0$ is a $double$, not a $float$

because, 

if decimal floating-point constants suffixed by $f$ or $F$, then that indicates a constant of type $float$. 

If decimal floating point constants suffixed by $l$ or $L$ $\rightarrow$ indicates type $long \hspace{0.1cm} double$.

if decimal floating point constants left unsuffixed then that indicates a $double$ constant. 

So, $sizeof(7.0)$ indicates $sizeof(double)$ i.e. $8$ 

selected by

Related questions

1 votes
1 votes
1 answer
1
radha gogia asked Jul 10, 2018
1,720 views
If I have an array :int a [3][4] ; How to evaluate below Outputs ? 1. sizeof(*a) 2.sizeof( a) 3. sizeof(a) Please explain precisely .
1 votes
1 votes
1 answer
2
Shiva Sagar Rao asked Feb 16, 2019
670 views
#include <stdio.h int main() { int a = 1; char d[] = "ab"; printf("%d", sizeof(a+d)); return 0; }Explain the Output
1 votes
1 votes
2 answers
3
raushan sah asked May 24, 2018
1,169 views
#include<stdio.h int main() { char *s="\123456789\n"; printf("%d", sizeof(s)); return 0; }why this piece of code gives output $8$please explain. thanx