Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++

What is alignof operator in C++?

Behzad Ahmad

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.

RELATED TAGS

c++

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring