How to get the number of bytes of a variable in C++

1 Answer

0 votes
#include <iostream>

int main()
{
    char ch;
    std::size_t bytes = sizeof(ch);
    std::cout << bytes << "\n";
    
    int i;
    std::cout << sizeof(i) << "\n";
    
    long l;
    std::cout << sizeof(l) << "\n";
    
    float f;
    std::cout << sizeof(f) << "\n";
    
    double d;
    std::cout << sizeof(d) << "\n";
}



/*
run:

1
4
8
4
8

*/

 



answered Jun 3, 2025 by avibootz

Related questions

1 answer 152 views
1 answer 88 views
1 answer 92 views
1 answer 176 views
1 answer 117 views
1 answer 147 views
...