In C, what does "do?"

Possible duplicate:
What does unsigned temp: 3 mean?

I am trying to learn socket programming in C and came across this:

unsigned char iph_ihl:5, iph_ver:4; 

I am confused by what the ":" does. Does it even do? Or is it just part of the variable name?

+4
source share
3 answers

You are looking at beatpots. These definitions must be inside the structure, and they mean that iph_ihl is a 5-bit field, and iph_ver is a 4-bit field.

Your example is a bit strange, since an unsigned char will be an 8-bit type on most machines, but 9 bits are declared there.

In general, bit fields are not quite portable, so I would recommend not using them, but you can read about them here .

+6
source

These are bit fields. Check out this good C bitfield documentation. It is usually used in memory-limited situations (an example of built-in programming) to pack our usage tightly.

Important point Bit fields do not have addresses - you do not have pointers to them or arrays of them

+1
source

Besides the above answers, you can take a look at this for a good introduction to bit fields. One note: bit fields in c can only be used for integer types. Using bit fields will not only make your program non-portable , it will also be compiler-dependent .

0
source

Source: https://habr.com/ru/post/1389987/


All Articles