聲明和初始化(Declarations and Initializations)

返回

如何使用integer type? 如果數(shù)值大于32,767或小于-32,767,使用long. 如果容量重要的話(比如說巨大的數(shù)組,許多構(gòu)造體),使用short. 其他的情況使用int.

整數(shù)常數(shù)的數(shù)據(jù)判斷

#include <stdio.h> int main(void) { int plus3 = 3, minus3 = -3; double ddt; // 2147483647是可表示的最大整數(shù) ddt = 2147483647 + plus3; printf("Ex1 ddt= %f\n",ddt); ddt = 2147483647U + plus3; printf("Ex2 ddt= %f\n",ddt); ddt = minus3 + 1; printf("Ex3 ddt= %f\n",ddt); ddt = minus3 + 1U; printf("Ex4 ddt= %f\n",ddt); ddt = minus3 + 0; printf("Ex5 ddt= %f\n",ddt); ddt = minus3 + 0U; printf("Ex6 ddt= %f\n",ddt); return 0; } /* 運(yùn)行結(jié)果 Ex1 ddt= -2147483646.000000 Ex2 ddt= 2147483650.000000 Ex3 ddt= -2.000000 Ex4 ddt= 4294967294.000000 Ex5 ddt= -3.000000 Ex6 ddt= 4294967293.000000 */

extern 聲明外部變量

#include <stdio.h> void func(void); int glb = 88888888; int main(void) { printf("main:glb=%d\n",glb); func(); return 0; } // test080905_2.c #include <stdio.h> extern int glb; void func(void) { printf("func:glb=%d\n",glb); } /* C:\ctest\cl test080905.c test080905_2.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86 Copyright (C) Microsoft Corp 1984-1998. All rights reserved. test080905.c test080905_2.c ????を生成中... Microsoft (R) Incremental Linker Version 6.00.8168 Copyright (C) Microsoft Corp 1992-1998. All rights reserved. /out:test080905.exe test080905.obj test080905_2.obj C:\ctest>test080905 main:glb=88888888 func:glb=88888888 */

聲明外部變量2

#include <stdio.h> void sub(void); extern int glb1; int main(void) { printf("%d\n",glb1); /* 不能使用glb2變量==> printf("%d\n",glb2); test080908.c test080908.c(10) : error C2065: 'glb2' : 定義されていない識別子です。 test080908.c(20) : warning C4098: 'sub' : 戻り値の型が 'void' で宣言された関數(shù)が 、値を返しました。 */ sub(); return 0; } void sub(void) { extern int glb2; // 函數(shù)內(nèi)宣言 printf("%d\n",glb1); printf("%d\n",glb2); return 0; } /* 運(yùn)行結(jié)果 44444444 44444444 88888888 */

獲取一定范圍的任意整數(shù)

#include <stdio.h> #include <stdlib.h> int main() { int N; int rand1; N = 1000; rand1 = (int)((double)rand()/((double)RAND_MAX+1)*N); printf("RAND_MAX is %d\n", RAND_MAX); printf("N is %d\n", N); printf("N rand is %d", rand1); return 0; } /* 答案有問題? RAND_MAX is 32767 N is 1000 N rand is 1 */

返回int型和double型的函數(shù)

#include <stdio.h> int rint(); double rdb(); int main(void) { int a; double d; a = rint(8888); printf("%d\n", a); d = rdb(88.88); printf("%f\n", d); return 0; } int rint(int dt) { return dt * 10; } double rdb(double dt) { return dt * 10.0; } /* 運(yùn)行結(jié)果 88880 888.800000 */

文字型數(shù)據(jù)

#include <stdio.h> int main() { char c1; unsigned char c2; int it; c1 = '\xFF'; c2 = '\xFF'; it = '\xFF'; printf("c1=%d\n", c1); printf("c2=%d\n", c2); printf("it=%d\n", it); c1 = 0xFF; c2 = 0xFF; it = 0xFF; printf("c1=%d\n", c1); printf("c2=%d\n", c2); printf("it=%d\n", it); return 0; } /* 運(yùn)行結(jié)果 c1=-1 c2=255 it=-1 c1=-1 c2=255 it=255 */
返回
扎鲁特旗| 开江县| 桂林市| 农安县| 民丰县| 金门县| 鄄城县| 江油市| 来安县| 连云港市| 普陀区| 霍城县| 石棉县| 历史| 连城县| 馆陶县| 昌图县| 城口县| 凌源市| 惠安县| 普兰县| 罗田县| 牟定县| 元朗区| 水富县| 开原市| 阜宁县| 马山县| 楚雄市| 青神县| 黄龙县| 沂源县| 柳州市| 连城县| 武邑县| 深泽县| 马龙县| 沙湾县| 杂多县| 福安市| 板桥市|