-
-
[旧帖]
[原创]C语言-常量
0.00雪花
-
发表于:
2011-5-27 00:25
4434
-
说明:东西都比较简单,个人水平也比较一般,表述难免不清或错误。自己学习的一点总结,还请各位多指教不足和要改进的地方,我会及时更新,这里先谢过。
上一篇 关于基本数据类型和局部变量链接:
http://bbs.pediy.com/showthread.php?p=963344#post963344
C语言之常量
C语言的常量主要只在编译前值就确定的数值,类型主要包括:整型常量,浮点型常量,字符型常量和枚举型常量。
环境为win32,Vc
示例代码:
#define MAX_NUM 256
const int g_cnTest = 123;
int main(int argc, char* argv[])
{
//bool
bool isTest = true;
//char
char cChar = 'T';
//int
int nNum = 0x123;
nNum = MAX_NUM;
int nMax = g_cnTest;
//枚举
typedef enum EnTest{en_0, en_1, en_2};
EnTest et = en_2;
//float(double)
float fValue = 1.2f;
//double
double dfValue1 = 1.2;
double dfValue2 = 1.2;
//字符串
printf("Hello World!\r\n");
char* lpTest = (char*)("Hello World!\r\n");
return 0;
}
15: //bool
16: bool isTest = true;
0040E9D8 C6 45 FC [B][COLOR="Red"]01 [/COLOR] [/B] mov byte ptr [ebp-4],[B][COLOR="red"]1[/COLOR][/B]
17: //char
18: char cChar = 'T';
0040E9DC C6 45 F8 [B][COLOR="red"]54[/COLOR] [/B] mov byte ptr [ebp-8],[B][COLOR="red"]54h[/COLOR][/B]
19: //int
20: int nNum = [COLOR="red"]0x123[/COLOR];
0040E9E0 C7 45 F4 [B][COLOR="red"]23 01 00 00 [/COLOR][/B]mov dword ptr [ebp-0Ch],[B]123h[/B]
21: nNum = MAX_NUM;
0040E9E7 C7 45 F4 [B][COLOR="red"]00 01 00 00 [/COLOR][/B]mov dword ptr [ebp-0Ch],[B][COLOR="red"]100h[/COLOR][/B]
22: int nMax = cnTest;
0040E9EE C7 45 F0 [B][COLOR="red"]7B 00 00 00 [/COLOR][/B]mov dword ptr [ebp-10h],[B][COLOR="red"]7Bh[/COLOR][/B]
23: //枚举
24: typedef enum EnTest{en_0, en_1, [B]en_2[/B]};
25: EnTest et = en_2;
0040E9F5 C7 45 EC [B][COLOR="red"]02 00 00 00 [/COLOR][/B]mov dword ptr [ebp-14h],[B][COLOR="red"]2[/COLOR][/B]
28: //float
29: float fValue = 1.2f;
0040EA03 C7 45 E4 [COLOR="red"]9A 99 99 3F [/COLOR]mov dword ptr [ebp-1Ch],[COLOR="red"][B]3F99999Ah[/B][/COLOR]
30: //double
31: double dfValue1 = 1.2;
0040EA0A C7 45 DC [COLOR="red"]33 33 33 33 [/COLOR]mov dword ptr [ebp-24h],[COLOR="red"]33333333h[/COLOR]
0040EA11 C7 45 E0 [COLOR="red"]33 33 F3 3F [/COLOR]mov dword ptr [ebp-20h],[COLOR="red"]3FF33333h[/COLOR]
32: double dfValue2 = 1.2;
0040EA18 C7 45 D4 33 33 33 33 mov dword ptr [ebp-2Ch],33333333h
0040EA1F C7 45 D8 33 33 F3 3F mov dword ptr [ebp-28h],3FF33333h
int* lpInt = (int*)&g_cnTest;
*lpInt = 0x111;
int nNum = 0x123;
int nTest = nNum;
printf("%d", nTest);
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课