In this blog post, you will learn the alignof Operator in C and their concept. You will learn how to use alignof (_Alignof
) Operator in your code and its effect. We also see some programming examples to understand the alignof Operator. So let’s understand what is alignof Operator.
What is _Alignof Operator in C?
The _Alignof operator yields the alignment requirement of its operand type. The operand is not evaluated and the result is an integer constant. We generally use the _Alignof operator through the convenience macro alignof
, which is provided in the header <stdalign.h> .
_Alignof Syntax:
_Alignof(type-name) (since C11) alignof(type-name) (since C11)
_Alignof operators return a value of type size_t (an unsigned integer type) and the value of the result is implementation-defined. When it is applied to an array type, the result is the alignment requirement of the element type. Let’s see an example code to understand the _Alignof operators.
// Compile with /std:c11 #include <stdalign.h> #include <stdio.h> typedef struct { int a; double b; }data; int main() { printf("_Alignof(char) = %d\n", _Alignof(char)); printf("_Alignof(short) = %d\n", _Alignof(short)); printf("alignof(char) = %d\n", alignof(char)); printf("alignof(short) = %d\n", alignof(short)); printf("alignof(int) = %d\n", alignof(int)); printf("alignof(long) = %d\n", alignof(long)); printf("alignof(float) = %d\n", alignof(float)); printf("alignof(double) = %d\n", alignof(double)); /*Alignment would be according to the largest element in the structure*/ printf("alignof(data) = %d\n", alignof(data)); return 0; }
Output:
Note:
The _Alignof operator must not be applied to a function type or an incomplete type.
Difference between sizeof and alignof (sizeof vs alignof)?
Both sizeof and alignof operators are fundamentally different from each other. The sizeof
operator yields the size (in bytes) of its operand while the alignof
operator yields the alignment requirement of its operand type. Let’s see an example,
int main() { int data; printf("Size in Bytes = %u\n",sizeof data); printf("Alignment Require = %u\n",alignof data); return 0; }
Output:
Size in Bytes = 4
Alignment require = 4
When alignof is applied to an array type, the result is the alignment requirement of the element type. But when sizeof is applied to an operand that has array type, the result is the total number of bytes in the array. Let’s see an example,
// Compile with /std:c11 #include <stdalign.h> #include <stdio.h> #include <stddef.h> int main() { int data[20]; printf("Size in Bytes = %u\n",sizeof data); printf("Alignment Require = %u\n",alignof data); return 0; }
Output:
Size in Bytes = 80
Alignment require = 4
When alignof
is applied to an operand that has structure or union type, the alignof operator yields the alignment requirement for the structure or union type. But when sizeof
is applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding. Let’s see an example,
// Compile with /std:c11 #include <stdalign.h> #include <stdio.h> #include <stddef.h> typedef struct { char a; int b; float c; double d; } data; int main() { /*Alignment would be according to the largest element in the structure*/ printf("alignof(data) = %d\n", alignof(data)); //total number of bytes including padding bytes printf("sizeof(data) = %d\n", sizeof(data)); return 0; }
Output:
alignof(data) = 8 sizeof(data) = 24
In sizeof
, if the type of the operand is a variable-length array type (VLA), the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant. In alignof
The operand is not evaluated and the result is an integer constant. Let’s see an example,
// Compile with /std:c11 #include <stdalign.h> #include <stdio.h> #include <stddef.h> int main() { const int n = 3; char b[n+3]; // variable length array printf("%u\n",sizeof b); printf("%u\n",alignof b); return 0; }
Output:
6, 1
Recommended Post:
- Alignment specifiers in C ( _Alignas).
- Function Specifiers in C.
- Type qualifiers in C.
- Punctuators in C.
- Elements of C language.
- C String Literals with Its Types
- C identifiers and naming rules.
- Best Laptop for Gamers and Programmers.
- Stringizing operator (#) in C
- Token Pasting Operator in C/C++ programming.