C++ Example - Sizes of int, float, double and char Variables
Use the C++ sizeof operator to compute the space occupied by int, float, double, and char variables.
sizeof operator syntax:
sizeof(dataType);
**Note:** Results may differ across systems.
Example
include
<
iostream
using
namespace
std;
int
main
(
)
{
cout
<<
"
char:
"
<<
sizeof
(
char
)
<<
"
bytes
"
<<
endl
;
cout
<<
"
int:
"
<<
sizeof
(
int
)
<<
"
bytes
"
<<
endl
;
cout
<<
"
float:
"
<<
sizeof
(
float
)
<<
"
bytes
"
<<
endl
;
cout
<<
"
double:
"
<<
sizeof
(
double
)
<<
"
bytes
"
<<
endl
;
return
0
;
}
The output of the above program is:
char: 1 字节
int: 4 字节
float: 4 字节
double: 8 字节
C++ Example