C语言位操作控件属性

简介:
#include <stdio.h>
#define YES 1
#define NO 0
#define SOLID 0
#define DOTTED 1
#define DASHED 2
#define BLUE 4
#define GREEN 2
#define RED 1
#define BLACK 0
#define YELLOW (RED | GREEN)
#define MAGENTA (RED | BLUE)
#define CYAN (GREEN | BLUE)
#define WHITE (RED | GREEN | BLUE)
const char * colors[8]={"black","red","green","yellow","blue","magenta","cyan","white"};
struct box_props {
    unsigned int opaque             :1;
    unsigned int fill_color         :3;
    unsigned int                    :4;
    unsigned int show_border        :1;
    unsigned int border_color       :3;
    unsigned int border_style       :2;
    unsigned int                    :2;
};
void show_settings(const struct box_props * pb);
int main(void){
    struct box_props box={YES,YELLOW,YES,GREEN,DASHED};
    printf("Original box setting:\n");
    show_settings(&box);
    box.fill_color=NO;
    box.fill_color=WHITE;
    box.border_color=MAGENTA;
    box.border_style=SOLID;
    printf("\nModified box settings:\n");
    show_settings(&box);
    return 0;
}
void show_settings(const struct box_props * pb){
    printf("Box is %s.\n",pb->opaque==YES?"shown":"transparent");
    printf("The fill color is %s.\n",colors[pb->fill_color]);
    printf("Border %s.\n",pb->show_border==YES?"shown":"not shown");
    printf("The border style is");
    switch(pb->border_style){
    case SOLID:printf("solid.\n");break;
    case DOTTED:printf("dotten.\n");break;
    case DASHED:printf("dashed.\n");break;
    default:printf("unkown type.\n");
    }

}
















本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/421035,如需转载请自行联系原作者

相关文章
|
2月前
|
算法 安全 C语言
C语言中的位操作运算符有什么作用
C语言中的位操作运算符有什么作用
19 0
|
5月前
|
C语言
C语言进阶教程(位操作和进制数的表示)
C语言进阶教程(位操作和进制数的表示)
44 0
|
11月前
|
C语言
聊一聊C语言位操作
聊一聊C语言位操作
51 0
|
17天前
|
C语言
C语言:内存函数(memcpy memmove memset memcmp使用)
C语言:内存函数(memcpy memmove memset memcmp使用)
|
3天前
|
存储 编译器 C语言
C语言:字符函数 & 字符串函数 & 内存函数
C语言:字符函数 & 字符串函数 & 内存函数
11 2
|
11天前
|
缓存 安全 编译器
【C 言专栏】C 语言函数的高效编程技巧
【5月更文挑战第1天】本文探讨了C语言中函数的高效编程技巧,包括函数的定义与作用(如代码复用和提高可读性)、设计原则(单一职责和接口简洁)、参数传递方式(值传递、指针传递和引用传递)、返回值管理、调用约定、嵌套与递归调用,以及函数优化技巧和常见错误避免。掌握这些技巧能提升C语言代码的质量和效率。
【C 言专栏】C 语言函数的高效编程技巧