alignof operator in C++ Last Updated : 08 Jun, 2018 Comments Improve Suggest changes 1 Likes Like Report In C++11 the alignof operator used to returns the alignment, in bytes of the specified type. Syntax: alignof(type) Syntax Explanation: alignof: operator returns the alignment in byte, required for instances of type, which type is either complete type, array type or a reference type. array type: alignment requirement of the element type is returned. reference type: the operator returns the alignment of referenced type. Return Value: The alignof operator typically used to returns a value of type std::size_t. Program: CPP // C++ program to demonstrate alignof operator #include <iostream> using namespace std; struct Geeks { int i; float f; char s; }; struct Empty { }; // driver code int main() { cout << "Alignment of char: " << alignof(char) << endl; cout << "Alignment of pointer: " << alignof(int*) << endl; cout << "Alignment of float: " << alignof(float) << endl; cout << "Alignment of class Geeks: " << alignof(Geeks) << endl; cout << "Alignment of Empty class: " << alignof(Empty) << endl; return 0; } Output: Alignment of char: 1 Alignment of pointer: 8 Alignment of float: 4 Alignment of class Geeks: 4 Alignment of Empty class: 1 alignof vs sizeof: The alignof value is the same as the value for sizeof for basic types. Consider, this example: typedef struct { int a; double b; } S; // alignof(S) == 8 Above case, the alignof value is the alignment requirement of the largest element in the structure. Example program to demonstrate the difference between alignof and sizeof: CPP // C++ program to demonstrate // alignof vs sizeof operator #include <iostream> using namespace std; struct Geeks { int i; float f; char s; }; int main() { cout << "alignment of Geeks : " << alignof(Geeks) << '\n'; cout << "sizeof of Geeks : " << sizeof(Geeks) << '\n'; cout << "alignment of int : " << alignof(int) << '\n'; cout << "sizeof of int : " << sizeof(int) << '\n'; } Output: alignment of Geeks : 4 sizeof of Geeks : 12 alignment of int : 4 sizeof of int : 4 Create Quiz Comment R rajasethupathi Follow 1 Improve R rajasethupathi Follow 1 Improve Article Tags : Misc C++ cpp-structure Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like