Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Pack | Compiler Directives and Advanced Control
C Preprocessing

Свайпніть щоб показати меню

book
Pack

Using #pragma pack can reduce the overall size of a structure or ensure precise alignment for specific cases, such as network protocols or hardware interfaces.

main.c

main.c

copy
1234567891011121314
#include <stdio.h> #include <stdint.h> // Without using `#pragma pack` struct Packet { uint8_t flag; // 8 bits or 1 bytes uint16_t length; // 16 bits or 2 bytes uint8_t type; // 8 bits or 1 bytes }; int main() { printf("Size without packing: %zu bytes\n", sizeof(struct Packet)); return 0; }

As you may recall, structure alignment is determined by the largest field within the structure.

Using #pragma pack

main.c

main.c

copy
123456789101112131415
#include <stdio.h> #include <stdint.h> #pragma pack(push, 1) struct PacketPacked { uint8_t flag; // 8 bits = 1 bytes uint16_t length; // 16 bits == 2 bytes uint8_t type; // 8 bits == 1 bytes }; #pragma pack(pop) int main() { printf("Size with packing: %zu bytes\n", sizeof(struct PacketPacked)); return 0; }

The directive #pragma pack(push, 1) creates a local alignment context for a structure.

You start a new alignment scope with push, apply a specific rule (here, 1-byte alignment), define the structure, then restore the previous alignment with pop.

Using push with 1 ensures that all fields are aligned on 1-byte boundaries, with no padding between them.

Завдання

Swipe to start coding

You're working with a digital sensor that sends binary data packets over UART. Each packet follows a fixed 4-byte layout. To match this exact layout with no padding, use the #pragma pack directive to enforce 1-byte alignment.

BytesFieldType
1Command codeuint8_t
2Sensor valueuint16_t
1Checksumuint8_t
  • Fill in the first ___ to start the packed context with 1-byte alignment.
  • Fill in the second ___ to restore default alignment after the struct.
  • Use the correct field access expressions (e.g. packet.cmd) in each printf to output: the command byte, the 2-byte value and the checksum byte.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

close

Awesome!

Completion rate improved to 5.56

book
Pack

Using #pragma pack can reduce the overall size of a structure or ensure precise alignment for specific cases, such as network protocols or hardware interfaces.

main.c

main.c

copy
1234567891011121314
#include <stdio.h> #include <stdint.h> // Without using `#pragma pack` struct Packet { uint8_t flag; // 8 bits or 1 bytes uint16_t length; // 16 bits or 2 bytes uint8_t type; // 8 bits or 1 bytes }; int main() { printf("Size without packing: %zu bytes\n", sizeof(struct Packet)); return 0; }

As you may recall, structure alignment is determined by the largest field within the structure.

Using #pragma pack

main.c

main.c

copy
123456789101112131415
#include <stdio.h> #include <stdint.h> #pragma pack(push, 1) struct PacketPacked { uint8_t flag; // 8 bits = 1 bytes uint16_t length; // 16 bits == 2 bytes uint8_t type; // 8 bits == 1 bytes }; #pragma pack(pop) int main() { printf("Size with packing: %zu bytes\n", sizeof(struct PacketPacked)); return 0; }

The directive #pragma pack(push, 1) creates a local alignment context for a structure.

You start a new alignment scope with push, apply a specific rule (here, 1-byte alignment), define the structure, then restore the previous alignment with pop.

Using push with 1 ensures that all fields are aligned on 1-byte boundaries, with no padding between them.

Завдання

Swipe to start coding

You're working with a digital sensor that sends binary data packets over UART. Each packet follows a fixed 4-byte layout. To match this exact layout with no padding, use the #pragma pack directive to enforce 1-byte alignment.

BytesFieldType
1Command codeuint8_t
2Sensor valueuint16_t
1Checksumuint8_t
  • Fill in the first ___ to start the packed context with 1-byte alignment.
  • Fill in the second ___ to restore default alignment after the struct.
  • Use the correct field access expressions (e.g. packet.cmd) in each printf to output: the command byte, the 2-byte value and the checksum byte.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

close

Awesome!

Completion rate improved to 5.56

Свайпніть щоб показати меню

some-alt