Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
alignof operator in C++
Next article icon

alignof operator in C++

Last Updated : 08 Jun, 2018
Comments
Improve
Suggest changes
Like Article
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  

Next Article
alignof operator in C++

R

rajasethupathi
Improve
Article Tags :
  • Misc
  • C++
  • cpp-structure
Practice Tags :
  • CPP
  • Misc

Similar Reads

    Assignment Operators in C++
    In C++, the assignment operator forms the backbone of computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language i.e. assign some value to the variables in
    6 min read
    Operators in C++
    C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language.Example:C++#include <iostream> using namespace std; int main() { int a = 10 + 20; cout << a; return 0; }Outpu
    9 min read
    Bitwise Operators in C++
    In C+, Bitwise Operators are the operators that are used to perform bit-level operations on the integers. While performing these operations, integers are considered as sequences of binary digits. These operators are useful for low-level programming, system programming, and optimizing performance.C++
    6 min read
    array::operator[ ] in C++ STL
    Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::operator[] This operator is used to reference the element present at position given inside the operator.
    2 min read
    bitset operator[] in C++ STL
    bitset::operator[] is a built-in function in C++ STL which is used to assign value to any index of a bitset. Syntax: bitset_operator[index] = value Parameter: The parameter index specifies the position at which the value is to be assigned. Return Value: The function returns the value to the bit at t
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences