C – Bit fields

Home/Learn C Programming/C – Bit fields
C – Bit fields 2017-08-07T14:21:50+00:00

Bit fields :

C permits us to use small bit fields to hold data. We have been using integer field of size 16 bit to store data. The data item requires much less than 16 bits of space, in such case we waste memory space. In this situation we use small bit fields in structures.

The bit fields data type is either int or unsigned int. the maximum value that can store in unsigned int filed is :-  (2 power n ) – 1  and in int filed is :- 2 power ( n – 1) . Here ‘n’ is the bit length.

Note :

scanf() statement cannot read data into bit fields because scanf() statement, scans on format data into 2 bytes address of the filed.

Syntax :

struct struct_name

{

unsigned  (or)  int identifier1 : bit_length;

unsigned  (or)  int identifier2 : bit_length;

………………………………………….

………………………………………….

unsigned  (or)  int identifierN : bit_length;

};

C – BIT FIELD EXAMPLE PROGRAM :

 

Prev Next