新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 高效的C编程之: 变量类型

高效的C编程之: 变量类型

作者:时间:2013-09-30来源:网络收藏

本文引用地址:http://www.eepw.com.cn/article/257017.htm

charbyte;

charc;

unsignedstate;

unsignedflags;

};

externstructglobsg;

#definetxg.tx

#definerxg.rx

#definebyteg.byte

#definecg.c

#definestateg.state

#defineflagsg.flags

#else

externchartx;

externcharrx;

externcharbyte;

externcharc;

externunsignedstate;

externunsignedflags;

#endif

/*************

*globals.c*

*************/

/*DEFINITIONSofglobals-singlesourcefile*/

#ifdef__arm

#includeglobals.h

structglobsg;

#else

chartx;

charrx;

charbyte;

charc;

unsignedstate;

unsignedflags;

#endif

/************

*access.c*

************/

#includestdio.h>

#includeglobals.h

intmain()

{tx=1;

rx=2;

byte=3;

c=4;

state=5;

flags=6;

printf(%u%u%u%u%u%un,tx,rx,byte,c,state,flags);

return0;

}

将变量定义在结构体内有以下几点好处。

·全局变量使用更小的内存空间。(没有使用结构体占有24字节,而使用结构体之后只占有12字节)

·全局变量被放置在ZI段而不是RW段,这样就减少了ROM映像文件的大小。

c语言相关文章:c语言教程



上一页 1 2 3 下一页

评论


相关推荐

技术专区

关闭