Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Size of int, float, double, char in C++

Print the size in bytes of char, int, float, double, long, and long long with sizeof in C++. Learn which sizes are guaranteed and which change between compilers.

BeginnerBasic ProgramsExample 6 of 14
size-of-int-float-double-char.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 cout << "Size of char: " << sizeof(char) << " bytes" << endl;
6 cout << "Size of int: " << sizeof(int) << " bytes" << endl;
7 cout << "Size of float: " << sizeof(float) << " bytes" << endl;
8 cout << "Size of double: " << sizeof(double) << " bytes" << endl;
9 cout << "Size of long: " << sizeof(long) << " bytes" << endl;
10 cout << "Size of long long: " << sizeof(long long) << " bytes" << endl;
11
12 return 0;
13}

Sample Output

Size of char: 1 bytes

Always 1.

Size of int: 4 bytes

Typical value.

Size of long: 8 bytes

Typical on 64-bit Linux/macOS; 4 on Windows.

Size of long long: 8 bytes

At least 64 bits on every compiler.

In this one

  • How sizeof reports the size of a type in bytes
  • Why sizeof(char) is always 1
  • Why long is 4 bytes on Windows but 8 on Linux
  • Which type to pick for values above 2 billion

How it works

  1. 1

    Each cout line calls sizeof on one type.

  2. 2

    sizeof is worked out at compile time, so no input is needed.

  3. 3

    The result is printed followed by the word bytes.

  4. 4

    return 0 ends the program.

Edge cases

Running on Windows

Build with MSVC or MinGW on Windows.

Size of long will print 4, not 8. The C++ standard only says long is at least 32 bits. Use long long or int64_t when you need 64 bits everywhere.

sizeof on an array

int a[10]; cout << sizeof(a);

This prints 40 on a 4-byte-int system, the size of the whole array. Passing the array to a function turns it into a pointer, and sizeof inside the function gives the pointer size (often 8).

What's going on

This C++ program prints how many bytes char, int, float, double, long, and long long use, via sizeof. No input. Run it on your compiler — the numbers are the point.

sizeof(x) is bytes, and it's a compile-time value. You can write sizeof(int) or sizeof x on a variable. Same answer.

sizeof(char) is 1. Always. That's in the standard. The sample prints Size of char: 1 bytes (yes, "1 bytes" is grammatically wrong; the program doesn't pluralize).

sizeof(int) is 4 on almost every machine used for this course. Not a guarantee. Old textbooks still show 16-bit int.

sizeof(float) is usually 4, sizeof(double) 8. IEEE-754 on desktop compilers.

sizeof(long) is the annoying one: 4 on typical Windows, 8 on typical 64-bit Linux/macOS. The sample here shows 8. Print it. Don't copy a number from a website.

sizeof(long long) is 8 on every compiler you're likely to use. That's the type for values past 2 billion.

If long is 4 bytes, 1L << 40 will not do what a 64-bit long would. Use long long (or 1LL) when you mean 64 bits.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which size is guaranteed by the C++ standard?

Pick an answer to check it.

Question 2

On 64-bit Windows, sizeof(long) is usually:

Pick an answer to check it.

Questions people ask

Is int always 4 bytes?

No. The standard only guarantees at least 16 bits. On nearly all modern desktop compilers it is 4 bytes, but you should check with sizeof if it matters.

What type does sizeof return?

It returns a value of type size_t, an unsigned integer type. That is why comparing it with a negative int can give surprising results.

Why is the output "1 bytes"?

The program always prints the word bytes. It does not check for plural. That is a wording quirk, not a bug in sizeof.