What is alignof operator in C++?

Operators are symbols that instruct the compiler to perform an operation.

The alignof operator is an operator that returns the alignment to be applied to the given type of variable. The returned value is in bytes.

Syntax

alignof(type)

Parameters

type − data type that’s alignment is determined.

Return Value

The number of bytes for the alignment is returned.

Code

#include <iostream>
using namespace std;
int main()
{
cout << "Alignment of Integer : " << alignof(int) << '\n';
cout << "Alignment of Char : " << alignof(char) << '\n';
cout << "Alignment of Float : " << alignof(float) << '\n';
cout << "Alignment of Double : " << alignof(double) << '\n';
cout << "Alignment of Integer array : " << alignof(int[3]) << '\n';
}

The code above shows the alignment of the different data types. When calculating an array’s alignment, the array’s data type is passed along with its size.

The alignment of the type of array is returned next.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved