C Language Examples - Compute the Byte Sizes of int, float, double, and char
Use the sizeof operator to compute the byte sizes of the four variable types int, float, double, and char.
sizeof is a unary operator in C, like other C operators such as ++ and --; it is not a function.
The sizeof operator gives the storage size of its operand in bytes.
Example
#include
<
stdio.h
int
main
(
)
{
int
integerType;
float
floatType
;
double
doubleType
;
char
charType
;
// The sizeof operator is used to compute the byte size of a variable
printf
(
"
Size of int: %ld bytes
\
n
"
,
sizeof
(
integerType
)
)
;
printf
(
"
Size of float: %ld bytes
\
n
"
,
sizeof
(
floatType
)
)
;
printf
(
"
Size of double: %ld bytes
\
n
"
,
sizeof
(
doubleType
)
)
;
printf
(
"
Size of char: %ld byte
\
n
"
,
sizeof
(
charType
)
)
;
return
0
;
}
Running result:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Compute the byte sizes of long long and long double
Example
#include
<
stdio.h
int
main
(
)
{
int
a;
long
b
;
long
long
c
;
double
e
;
long
double
f
;
printf
(
"
Size of int = %ld bytes
\
n
"
,
sizeof
(
a
)
)
;
printf
(
"
Size of long = %ld bytes
\
n
"
,
sizeof
(
b
)
)
;
printf
(
"
Size of long long = %ld bytes
\
n
"
,
sizeof
(
c
)
)
;
printf
(
"
Size of double = %ld bytes
\
n
"
,
sizeof
(
e
)
)
;
printf
(
"
Size of long double = %ld bytes
\
n
"
,
sizeof
(
f
)
)
;
return
0
;
}
Running result:
Size of int = 4 bytes
Size of long = 8 bytes
Size of long long = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes
C Language Examples