Category Archives: C Advance Topics

C Bit Fields

In C Programming Language we have the option to store integer members into memory spaces smaller than the compiler would ordinarily allow. These can be achieved using the space-saving structure members are called bit fields, and their width in bits can be explicitly declared.

This is very useful especially when memory or data storage is at a premium. A bit field is set up with a structure declaration that labels each field and determines its width.

Bit Field Declaration

A bit-field declaration contains a type specifier followed by an optional member_name, a colon, a constant integer expression that indicates the field width in bits, and a semicolon.

Bit fields with a width of 0 must be unnamed. Unnamed bit-fields cannot be referenced or initialized. If a series of bit fields do not makeup up the size of an int, padding can take place.

The following example demonstrates padding. Suppose that an int occupies 4 bytes. The example declares the identifier room to be of type struct app_state:

The structure room contains eight members with a width of 16 bytes. The following table describes the storage that each member occupies:

Member Name Storage Occupied
light 1 bit
fan 1 bit
(padding — 30 bits) To the next int boundary
count The size of an int (4 bytes)
ac 4 bits
(unnamed field) 4 bits
clock 1 bit
(padding — 23 bits) To the next int boundary (unnamed field)
flag 1 bit
(padding — 31 bits) To the next int boundary

You can not access the field by direct its name.

Syntax:

We can access the member “light” as “room. light”, The following expression sets the light field to 1

When you assign to a bit field a value that is out of its range, the bit pattern is preserved and the appropriate bits are assigned. The following expression sets the “fan” field of the room structure to a 0 (zero) because only the least significant bit is assigned to the fan field:

Note:- The maximum bit field length is 64 bits. For portability, do not use bit fields greater than 32 bits in size.

 

C typedef

In C programming language typedef ( type definition ) is a keyword that allows us to create an alias for existing data types. Once, we have created a type definition, then we can declare a variable using the alias we created. This alias is equivalent to the data type for which it is created. Remember that, in addition to the type definition or alias we are always free to use the original keyword to define variables of any data type.

Syntax:

It is easy to create the alias, put the typedef keyword followed by the existing type name and the new name for that type.

Let’s take look at the example below:

We have created the alias for integer data type as “my_type”, now my_type behaves the same as an integer.

Advantages of C Typedef

  • It makes the program more portable.
  • typedef makes complex declarations easier to understand.

typedef with a struct

Take a look at below structure declaration

As we can see we have to include keyword struct every time you declare a new variable, but if we use typedef then the declaration will be as easy as below

this way c typedef makes your declaration simpler.

C Typedef with Union

typedef with enum