bit-field-ex.c - A program that illustrates a struct with bit fields. | Lecture 1 - slide 22 : 34 Program 7 |
#include <stdio.h> struct pixel { unsigned int red: 8; unsigned int green: 8; unsigned int blue: 8; }; typedef struct pixel pixel; pixel make_pixel(unsigned int red, unsigned int green, unsigned int blue){ pixel p; p.red = red; p. green = green; p.blue = blue; return p; } unsigned int get_red(pixel p){ return p.red; } unsigned int get_green(pixel p){ return p.green; } unsigned int get_blue(pixel p){ return p.blue; } int main(void) { pixel p; p = make_pixel(80, 90, 100); printf("Pixel info: red = %d, green = %d, blue = %d\n", get_red(p), get_green(p), get_blue(p)); printf("Size of a pixel pixel: %d bytes\n", sizeof(pixel)); return 0; }