编程札记

e

exit

C 库函数 - exit() 描述 C 库函数 void exit(int status) 立即终止调用进程。任何属于该进程的打开的文件描述符都会被关闭,该进程的子进程由进程 1 继承,初始化,且会向父进程发送一个 SIGCHLD 信号。 声明 下面是 exit() 函数的声明。 void exit(int status) 参数 status -- 返回给父进程的状态值。 返回值 该函数不返回值。 实例 下面的实例演示了 exit() 函数的用法。 #include <stdio.h> #include <stdlib.h> int main () { printf("程序的开头....\n"); printf("退出程序....\n"); exit(0); printf("程序的结尾....\n"); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: 程序的开头.... 退出程序....

阅读全文 ↗
a

atexit

C 库函数 - atexit() 描述 C 库函数 int atexit(void (*func)(void)) 当程序正常终止时,调用指定的函数 func。您可以在任何地方注册你的终止函数,但它会在程序终止的时候被调用。 声明 下面是 atexit() 函数的声明。 int atexit(void (*func)(void)) 参数 func -- 在程序终止时被调用的函数。 返回值 如果函数成功注册,则该函数返回零,否则返回一个非零值。 实例 下面的实例演示了 atexit() 函数的用法。 #include <stdio.h> #include <stdlib.h> void functionA () { printf("这是函数A\n"); } int main () { /* 注册终止函数 */ atexit(functionA ); printf("启动主程序...\n"); printf("退出主程序...\n"); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: 启动主程序... 退出主程序... 这是函数A

阅读全文 ↗
a

abort

C 库函数 - abort() 描述 C 库函数 · 中止程序执行,直接从调用的地方跳出。 声明 下面是 abort() 函数的声明。 void abort(void) 参数 NA 返回值 该函数不返回任何值。 实例 下面的实例演示了 abort() 函数的用法。 #include <stdio.h> #include <stdlib.h> int main () { FILE *fp; printf("准备打开 nofile.txt\n"); fp = fopen( "nofile.txt","r" ); if(fp == NULL) { printf("准备终止程序\n"); abort(); } printf("准备关闭 nofile.txt\n"); fclose(fp); return(0); } 让我们编译并运行上面的程序,这将产生以下结果,因为我们尝试打开的文件 nofile.txt 是不存在的: 准备打开 nofile.txt 准备终止程序

阅读全文 ↗
r

realloc

C 库函数 - realloc() 描述 C 库函数 void *realloc(void *ptr, size_t size) 尝试重新调整之前调用 malloc 或 calloc 所分配的 ptr 所指向的内存块的大小。 声明 下面是 realloc() 函数的声明。 void *realloc(void *ptr, size_t size) 参数 ptr -- 指针指向一个要重新分配内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。如果为空指针,则会分配一个新的内存块,且函数返回一个指向它的指针。 size -- 内存块的新的大小,以字节为单位。如果大小为 0,且 ptr 指向一个已存在的内存块,则 ptr 所指向的内存块会被释放,并返回一个空指针。 返回值 该函数返回一个指针 ,指向重新分配大小的内存。如果请求失败,则返回 NULL。 实例 下面的实例演示了 realloc() 函数的用法。 #include <stdio.h> #include <stdlib.h> int main() { char *str; /* 最初的内存分配 */ str = (char *) malloc(15); strcpy(str, "codingdict"); printf("String = %s, Address = %u\n", str, str); /* 重新分配内存 */ str = (char *) realloc(str, 25); strcat(str, ".cn"); printf("String = %s, Address = %u\n", str, str); free(str); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: String = codingdict, Address = 355090448 String = codingdict.com, Address = 355090448

阅读全文 ↗
m

malloc

C 库函数 - malloc() 描述 C 库函数 · 分配所需的内存空间,并返回一个指向它的指针。 声明 下面是 malloc() 函数的声明。 void *malloc(size_t size) 参数 size -- 内存块的大小,以字节为单位。 返回值 该函数返回一个指针 ,指向已分配大小的内存。如果请求失败,则返回 NULL。 实例 下面的实例演示了 malloc() 函数的用法。 #include <stdio.h> #include <stdlib.h> int main() { char *str; /* 最初的内存分配 */ str = (char *) malloc(15); strcpy(str, "2xkt"); printf("String = %s, Address = %u\n", str, str); /* 重新分配内存 */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %u\n", str, str); free(str); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: String = 2xkt, Address = 355090448 String = 2xkt.com, Address = 355090448

阅读全文 ↗
f

free

C 库函数 - free() 描述 C 库函数 void free(void *ptr) 释放之前调用 calloc、malloc 或 realloc 所分配的内存空间。 声明 下面是 free() 函数的声明。 void free(void *ptr) 参数 ptr -- 指针指向一个要释放内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。如果传递的参数是一个空指针,则不会执行任何动作。 返回值 该函数不返回任何值。 实例 下面的实例演示了 free() 函数的用法。 #include <stdio.h> #include <stdlib.h> int main() { char *str; /* 最初的内存分配 */ str = (char *) malloc(15); strcpy(str, "2xkt"); printf("String = %s, Address = %u\n", str, str); /* 重新分配内存 */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %u\n", str, str); /* 释放已分配的内存 */ free(str); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: String = 2xkt, Address = 355090448 String = 2xkt.com, Address = 355090448

阅读全文 ↗
c

calloc

C 库函数 - calloc() 描述 C 库函数 void *calloc(size_t nitems, size_t size) 分配所需的内存空间,并返回一个指向它的指针。malloc 和 calloc 之间的不同点是,malloc 不会设置内存为零,而 calloc 会设置分配的内存为零。 声明 下面是 calloc() 函数的声明。 void *calloc(size_t nitems, size_t size) 参数 nitems -- 要被分配的元素个数。 size -- 元素的大小。 返回值 该函数返回一个指针,指向已分配的内存。如果请求失败,则返回 NULL。 实例 下面的实例演示了 calloc() 函数的用法。 #include <stdio.h> #include <stdlib.h> int main() { int i, n; int *a; printf("要输入的元素个数:"); scanf("%d",&n); a = (int*)calloc(n, sizeof(int)); printf("输入 %d 个数字:\n",n); for( i=0 ; i < n ; i++ ) { scanf("%d",&a[i]); } printf("输入的数字为:"); for( i=0 ; i < n ; i++ ) { printf("%d ",a[i]); } return(0); } 让我们编译并运行上面的程序,这将产生以下结果: 要输入的元素个数:3 输入 3 个数字: 22 55 14 输入的数字为:22 55 14

阅读全文 ↗
s

strtoul

C 库函数 - strtoul() 描述 C 库函数 unsigned long int strtoul(const char *str, char **endptr, int base) 把参数 str 所指向的字符串根据给定的 base 转换为一个无符号长整数(类型为 unsigned long int 型),base 必须介于 2 和 36(包含)之间,或者是特殊值 0。 声明 下面是 strtoul() 函数的声明。 unsigned long int strtoul(const char *str, char **endptr, int base) 参数 str -- 要转换为无符号长整数的字符串。 endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。 base -- 基数,必须介于 2 和 36(包含)之间,或者是特殊值 0。 返回值 该函数返回转换后的长整数,如果没有执行有效的转换,则返回一个零值。 实例 下面的实例演示了 strtoul() 函数的用法。 #include <stdio.h> #include <stdlib.h> int main() { char str[30] = "2030300 This is test"; char *ptr; long ret; ret = strtoul(str, &ptr, 10); printf("数字(无符号长整数)是 %lu\n", ret); printf("字符串部分是 |%s|", ptr); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: 数字(无符号长整数)是 2030300 字符串部分是 | This is test|

阅读全文 ↗
s

strtol

C 库函数 - strtol() 描述 C 库函数 long int strtol(const char *str, char **endptr, int base) 把参数 str 所指向的字符串根据给定的 base 转换为一个长整数(类型为 long int 型),base 必须介于 2 和 36(包含)之间,或者是特殊值 0。 声明 下面是 strtol() 函数的声明。 long int strtol(const char *str, char **endptr, int base) 参数 str -- 要转换为长整数的字符串。 endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。 base -- 基数,必须介于 2 和 36(包含)之间,或者是特殊值 0。 返回值 该函数返回转换后的长整数,如果没有执行有效的转换,则返回一个零值。 实例 下面的实例演示了 strtol() 函数的用法。 #include <stdio.h> #include <stdlib.h> int main() { char str[30] = "2030300 This is test"; char *ptr; long ret; ret = strtol(str, &ptr, 10); printf("数字(无符号长整数)是 %ld\n", ret); printf("字符串部分是 |%s|", ptr); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: 数字(无符号长整数)是 2030300 字符串部分是 | This is test|

阅读全文 ↗
s

strtod

C 库函数 - strtod() 描述 C 库函数 double strtod(const char *str, char **endptr) 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。如果 endptr 不为空,则指向转换中最后一个字符后的字符的指针会存储在 endptr 引用的位置。 声明 下面是 strtod() 函数的声明。 double strtod(const char *str, char **endptr) 参数 str -- 要转换为双精度浮点数的字符串。 endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。 返回值 该函数返回转换后的双精度浮点数,如果没有执行有效的转换,则返回零(0.0)。 实例 下面的实例演示了 strtod() 函数的用法。 #include <stdio.h> #include <stdlib.h> int main() { char str[30] = "20.30300 This is test"; char *ptr; double ret; ret = strtod(str, &ptr); printf("数字(double)是 %lf\n", ret); printf("字符串部分是 |%s|", ptr); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: 数字(double)是 20.303000 字符串部分是 | This is test|

阅读全文 ↗
a

atol

C 库函数 - atol() 描述 C 库函数 long int atol(const char *str) 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。 声明 下面是 atol() 函数的声明。 long int atol(const char *str) 参数 str -- 要转换为长整数的字符串。 返回值 该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。 实例 下面的实例演示了 atol() 函数的用法。 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { long val; char str[20]; strcpy(str, "98993489"); val = atol(str); printf("字符串值 = %s, 长整型值 = %ld\n", str, val); strcpy(str, "codingdict.com"); val = atol(str); printf("字符串值 = %s, 长整型值 = %ld\n", str, val); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: 字符串值 = 98993489, 长整型值 = 98993489 字符串值 = codingdict.com, 长整型值 = 0

阅读全文 ↗
a

atoi

C 库函数 - atoi() 描述 C 库函数 int atoi(const char *str) 把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。 声明 下面是 atoi() 函数的声明。 int atoi(const char *str) 参数 str -- 要转换为整数的字符串。 返回值 该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。 实例 下面的实例演示了 atoi() 函数的用法。 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int val; char str[20]; strcpy(str, "98993489"); val = atoi(str); printf("字符串值 = %s, 整型值 = %d\n", str, val); strcpy(str, "codingdict.com"); val = atoi(str); printf("字符串值 = %s, 整型值 = %d\n", str, val); return(0); } 让我们编译并运行上面的程序,这将产生以下结果: 字符串值 = 98993489, 整型值 = 98993489 字符串值 = codingdict.com, 整型值 = 0

阅读全文 ↗