Advertisement
EBobkunov

union_packet_ipv4

Mar 10th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | Source Code | 0 0
  1. #include <stdio.h>
  2.  
  3. union packet{
  4.     unsigned int a;
  5.     struct {
  6.         unsigned char version : 4;
  7.         unsigned char ihl : 4;
  8.         unsigned char dscp : 6;
  9.         unsigned char ecn : 2;
  10.         unsigned short totallength : 16;
  11.     }ipv4;
  12. };
  13.  
  14. int main() {
  15.     union packet P;
  16.     printf("Enter int value: \n");
  17.     scanf("%d", &P.a);
  18.     printf("Version: %u \n", P.ipv4.version);
  19.     printf("IHL: %u \n", P.ipv4.ihl);
  20.     printf("DSCP: %u \n", P.ipv4.dscp);
  21.     printf("ECN: %u \n", P.ipv4.ecn);
  22.     printf("Total length: %u \n", P.ipv4.totallength);
  23.  
  24.     return 0;
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement