59593

Format specifiers for data type BYTE, WORD and DWORD in c-language?

Question:

In C-language, what are the most appropriate format specifiers for data type BYTE, WORD and DWORD to be used with printf and scanf functions? I am having a hard time displaying BPB field's values over console.

For example, if I am trying to display BPB_BytsPerSec using "%lu", I am getting unusual figures..

printf("Bytes per Sector: %lu", b->BPB_BytsPerSec);

I am getting a value of "514", which I believe, is wrong interpretation.. Please suggest the way out. Thanks.

(I am using gcc 5.1 via MinGW, over a 64bit Windows)

Here is the Structure We are talking about:

/* BPB Structure Collected from internet */ struct BPB_FAT32 { BYTE BS_jmpBoot[3]; // 0 BYTE BS_OEMName[8]; // 3 WORD BPB_BytsPerSec; // 11 BYTE BPB_SecPerClus; // 13 WORD BPB_ResvdSecCnt; // 14 BYTE BPB_NumFATs; // 16 WORD BPB_RootEntCnt; // 17 WORD BPB_TotSec16; // 19 BYTE BPB_Media; // 21 WORD BPB_FATSz16; // 22 WORD BPB_SecPerTrk; // 24 WORD BPB_NumHeads; // 26 DWORD BPB_HiddSec; // 28 DWORD BPB_TotSec32; // 32 DWORD BPB_FATSz32; // 36 WORD BPB_ExtFlags; // 40 WORD BPB_FSVer; // 42 DWORD BPB_RootClus; // 44 WORD BPB_FSInfo; // 48 WORD BPB_BkBootSec; // 50 BYTE BPB_Reserved[12]; // 52 BYTE BS_DrvNum; // 64 BYTE BS_Reserved1; // 65 BYTE BS_BootSig; // 66 DWORD BS_VolID; // 67 BYTE BS_VolLab[11]; // 71 BYTE BS_FilSysType[8]; // 82 };

And this the function through which i am trying to display these values..

void DisplayBlock(struct BPB_FAT32* b){ printf("\n\n\t1. Jump to boot code..........: 0x%02x%02x%02x",b->BS_jmpBoot[0],b->BS_jmpBoot[1],b->BS_jmpBoot[2]); printf("\n\n\t2. OEM name & version.........: "); for(int i=0; i<8;i++) printf("%c",(char)b->BS_OEMName[i]); printf("\n\n\t3. Bytes per Sector...........: %u",b->BPB_BytsPerSec); // WORD printf("\n\n\t4. Sectors per Cluster........: %lu", b->BPB_SecPerClus); // BYTE printf("\n\n\t5. Reserved Sectors Count.....: %lu", b->BPB_ResvdSecCnt); // WORD printf("\n\n\t6. Number of FAT32 tables.....: %u", b->BPB_NumFATs); // BYTE printf("\n\n\t7. Number of Root Entries.....: %lu", b->BPB_RootEntCnt); // WORD printf("\n\n\t8. Total Sectors 16bits.......: %lu",b->BPB_TotSec16); // WORD printf("\n\n\t9. Media Descriptor...........: %lu",b->BPB_Media); // BYTE 0xF8 is the standard value for fixed media printf("\n\n\t10. Size According to FAT16....: %lu",b->BPB_FATSz16); // WORD printf("\n\n\t11. Number of Sector per Track.: %lu",b->BPB_SecPerTrk); // WORD printf("\n\n\t12. Number of Heads............: %lu",b->BPB_NumHeads); // WORD printf("\n\n\t13. Number of Hidden Sectors...: %lu", b->BPB_HiddSec); // DWORD printf("\n\n\t14. Total Sectors 32bits.......: %lu", b->BPB_TotSec32); // DWORD /* FAT32 Specific Fields Starting offset:36 */ printf("\n\n\t15. Size According to FAT32....: %lu", b->BPB_FATSz32); // DWORD //printf("\n\n\t16. Extended Flags............: %u",b->BPB_ExtFlags); // WORD Usually 0. printf("\n\n\t17. File System Version........: %lu", b->BPB_FSVer); // WORD Must be 0:0 printf("\n\n\t18. Root Cluster Number........: %lu",b->BPB_RootClus); // DWORD Usually 2 printf("\n\n\t19. File System Information....: %lu",b->BPB_FSInfo); // WORD printf("\n\n\t20. Backup Boot Sector.........: %lu",b->BPB_BkBootSec); // WORD Recomended 6 // BYTE BPB_Reserved[12] /* Remaining common fields FAT12/FAT16 BPB*/ printf("\n\n\t21. Drive Number...............: %u",b->BS_DrvNum); // BYTE // BYTE BS_Reserved1; /* 65 reserved */ printf("\n\n\t22. Boot Signature.............: %u",b->BS_BootSig); // BYTE printf("\n\n\t23. Volume ID..................: %lu",b->BS_VolID); // DWORD printf("\n\n\t24. Volume Lable...............: %s",b->BS_VolLab); //for(int i=0;i<11;i++) printf("%c",b->BS_VolLab[i]); // BYTE [11] printf("\n\n\t25. File System Type...........: "); for(int i=0;i<8;i++) printf("%c",b->BS_FilSysType[i]); // BYTE [8] }

And here is the unhappy output : <a href="https://i.stack.imgur.com/Rvv1O.jpg" rel="nofollow"><img alt="a Sample output" class="b-lazy" data-src="https://i.stack.imgur.com/Rvv1O.jpg" data-original="https://i.stack.imgur.com/Rvv1O.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned long DWORD; typedef unsigned long long QWORD;

Recommend