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 256 views
1 answer 173 views
1 answer 179 views
1 answer 274 views
1 answer 202 views
1 answer 252 views
...