《C语言函数大全》 摘录

《c语言函数大全》

与其说这是一本书,还不如说这是一本字典……

 

书中的函数实在太多,涉及一些新的概念,比如内存、堆栈……这里把书中的一些“有用的”函数摘录出来,这些函数可以帮助快速解题,简化代码,用于预处理等等。

函数名: atof

功 能: 把字符串转换成浮点数

用 法: double atof(const char *nptr);

程序例:

#include <stdlib.h>

#include <stdio.h>

int main(void)

{

float f;

char *str = "12345.67";

f = atof(str);//atoi是转化为int , atol是转化为long

printf("string = %sfloat = %f\n", str, f);

return 0;

}

 

函数名: bsearch

功 能: 二分法搜索

用 法: void *bsearch(const void *key, const void *base, size_t *nelem, size_t w

程序例:

#include <stdlib.h>

#include <stdio.h>

#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))

int numarray[] = {123, 145, 512, 627, 800, 933};

int numeric (const int *p1, const int *p2)

{

return(*p1 - *p2);

}

int lookup(int key)

{

int *itemptr;

itemptr = bsearch (&key, numarray, NELEMS(numarray),

sizeof(int), (int(*)(const void *,const void *))numeric);

return (itemptr != NULL);

}

int main(void)

{

if (lookup(512))

printf("512 is inthe table.\n");

else

printf("512 isn't inthe table.\n");

return 0;

}

 

 

函数名: farcalloc

功 能: 从远堆栈中申请空间

用 法: void far *farcalloc(unsigned long units, unsigned ling unitsz);

程序例:

#include <stdio.h>

#include <alloc.h>

#include <string.h>

#include <dos.h>

int main(void)

{

char far *fptr;

char *str = "Hello";

fptr = farcalloc(10, sizeof(char));

movedata(FP_SEG(str), FP_OFF(str),

FP_SEG(fptr), FP_OFF(fptr),

strlen(str));

printf("Far stringis: %Fs\n", fptr);

farfree(fptr);

return 0;

}

 

函数名: farmalloc

功 能: 从远堆中分配存储块

用 法: void far *farmalloc(unsigned long size);

程序例:

#include <stdio.h>

#include <alloc.h>

#include <string.h>

#include <dos.h>

int main(void)

{

char far *fptr;

char *str = "Hello";

fptr = farmalloc(10);

movedata(FP_SEG(str), FP_OFF(str),

FP_SEG(fptr), FP_OFF(fptr),

strlen(str));

printf("Far stringis: %Fs\n", fptr);

farfree(fptr);

return 0;

}

 

函数名: fclose

功 能: 关闭一个流

用 法: int fclose(FILE *stream);

程序例:

#include <string.h>

#include <stdio.h>

int main(void)

{

FILE *fp;

char buf[11] = "0123456789";

fp = fopen("DUMMY.FIL", "w");

fwrite(&buf, strlen(buf), 1, fp);

fclose(fp);

return 0;

}

 

 

函数名: fflush

功 能: 清除一个流

用 法: int fflush(FILE *stream);

程序例:

#include <string.h>

#include <stdio.h>

#include <conio.h>

#include <io.h>

void flush(FILE *stream);

int main(void)

{

FILE *stream;

char msg[] = "This is a test";

stream = fopen("DUMMY.FIL", "w");

fwrite(msg, strlen(msg), 1, stream);

clrscr();

printf("Press anykey to flush\

DUMMY.FIL:");

getch();

flush(stream);

printf("\nFile wasflushed, Press any key\

to quit:");

getch();

return 0;

}

void flush(FILE *stream)

{

int duphandle;

fflush(stream);

duphandle = dup(fileno(stream));

close(duphandle);

}

 

函数名: fgetc

功 能: 从流中读取字符

用 法: int fgetc(FILE *stream);

程序例:

#include <string.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

FILE *stream;

char string[] = "This is a test";

char ch;

/* open a file for update */

stream = fopen("DUMMY.FIL", "w+");

/* write a string into the file */

fwrite(string, strlen(string), 1, stream);

/* seek to the beginning of the file */

fseek(stream, 0, SEEK_SET);

do

{

/* read a char from the file */

ch = fgetc(stream);

/* display the character */

putch(ch);

} while (ch != EOF);

fclose(stream);

return 0;

}

函数名: fgetchar

功 能: 从流中读取字符

用 法: int fgetchar(void);

程序例:

#include <stdio.h>

int main(void)

{

char ch;

/* prompt the user for input */

printf("Enter acharacter followed by \

<Enter>: ");

/* read the character from stdin */

ch = fgetchar();

/* display what was read */

printf("Thecharacter read is: '%c'\n",

ch);

return 0;

}

 

 

函数名: ftell

功 能: 返回当前文件指针

用 法: long ftell(FILE *stream);

程序例:

#include <stdio.h>

int main(void)

{

FILE *stream;

stream = fopen("MYFILE.TXT", "w+");

fprintf(stream, "This is atest");

printf("The filepointer is at byte \

%ld\n", ftell(stream));

fclose(stream);

return 0;

}

 

函数名: log10

功 能: 对数函数log

用 法: double log10(double x);

程序例:

#include <math.h>

#include <stdio.h>

int main(void)

{

double result;

double x = 800.6872;

result = log10(x);

printf("The commonlog of %lf is %lf\n", x, result);

return 0;

}

 

函数名: rand

功 能: 随机数发生器

用 法: void rand(void);

程序例:

#include <stdlib.h>

#include <stdio.h>

int main(void)

{

int i;

printf("Ten randomnumbers from 0 to 99\n\n");

for(i=0; i<10; i++)

printf("%d\n", rand() % 100);

return 0;

}

函数名: rewind

功 能: 将文件指针重新指向一个流的开头

用 法: int rewind(FILE *stream);

程序例:

#include <stdio.h>

#include <dir.h>

int main(void)

{

FILE *fp;

char *fname = "TXXXXXX", *newname, first;

newname = mktemp(fname);

fp = fopen(newname,"w+");

fprintf(fp,"abcdefghijklmnopqrstuvwxyz");

rewind(fp);

fscanf(fp,"%c",&first);

printf("The firstcharacter is: %c\n",first);

fclose(fp);

remove(newname);

return 0;

}

 

函数名: stpcpy

功 能: 拷贝一个字符串到另一个

用 法: char *stpcpy(char *destin, char *source);

程序例:

#include <stdio.h>

#include <string.h>

int main(void)

{

char string[10];

char *str1 = "abcdefghi";

stpcpy(string, str1);

printf("%s\n", string);

return 0;

}

 

函数名: strcat

功 能: 字符串拼接函数

用 法: char *strcat(char *destin, char *source);

程序例:

#include <string.h>

#include <stdio.h>

int main(void)

{

char destination[25];

char *blank = " ", *c = "C++", *Borland = "Borland";

strcpy(destination, Borland);

strcat(destination, blank);

strcat(destination, c);

printf("%s\n", destination);

return 0;

}

 

函数名: strcmp

功 能: 串比较

用 法: int strcmp(char *str1, char *str2);

程序例:

#include <string.h>

#include <stdio.h>

int main(void)

{

char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";

int ptr;

ptr = strcmp(buf2, buf1);

if (ptr > 0)

printf("buffer 2 isgreater than buffer 1\n");

else

printf("buffer 2 isless than buffer 1\n");

ptr = strcmp(buf2, buf3);

if (ptr > 0)

printf("buffer 2 isgreater than buffer 3\n");

else

printf("buffer 2 isless than buffer 3\n");

return 0;

}

 

函数名: strcpy

功 能: 串拷贝

用 法: char *strcpy(char *str1, char *str2);

程序例:

#include <stdio.h>

#include <string.h>

int main(void)

{

char string[10];

char *str1 = "abcdefghi";

strcpy(string, str1);

printf("%s\n", string);

return 0;

}

函数名: strcspn

功 能: 在串中查找第一个给定字符集内容的段

用 法: int strcspn(char *str1, char *str2);

程序例:

#include <stdio.h>

#include <string.h>

#include <alloc.h>

int main(void)

{

char *string1 = "1234567890";

char *string2 = "747DC8";

int length;

length = strcspn(string1, string2);

printf("Characterwhere strings intersect is at position %d\n", length);

return 0;

}

函数名: strdup

功 能: 将串拷贝到新建的位置处

用 法: char *strdup(char *str);

程序例:

#include <stdio.h>

#include <string.h>

#include <alloc.h>

int main(void)

{

char *dup_str, *string = "abcde";

dup_str = strdup(string);

printf("%s\n", dup_str);

free(dup_str);

return 0;

}

函数名: stricmp

功 能: 以大小写不敏感方式比较两个串

用 法: int stricmp(char *str1, char *str2);

程序例:

#include <string.h>

#include <stdio.h>

int main(void)

{

char *buf1 = "BBB", *buf2 = "bbb";

int ptr;

ptr = stricmp(buf2, buf1);

if (ptr > 0)

printf("buffer 2 isgreater than buffer 1\n");

if (ptr < 0)

printf("buffer 2 isless than buffer 1\n");

if (ptr == 0)

printf("buffer 2equals buffer 1\n");

return 0;

}

 

函数名: strrev

功 能: 串倒转

用 法: char *strrev(char *str);

程序例:

#include <string.h>

#include <stdio.h>

int main(void)

{

char *forward = "string";

printf("Beforestrrev(): %s\n", forward);

strrev(forward);

printf("Afterstrrev(): %s\n", forward);

return 0;

}

 

函数名: tmpnam

功 能: 创建一个唯一的文件名

用 法: char *tmpnam(char *sptr);

程序例:

#include <stdio.h>

int main(void)

{

char name[13];

tmpnam(name);

printf("Temporaryname: %s\n", name);

return 0;

}

函数名: tmpnam

功 能: 创建一个唯一的文件名

用 法: char *tmpnam(char *sptr);

程序例:

#include <stdio.h>

int main(void)

{

char name[13];

tmpnam(name);

printf("Temporaryname: %s\n", name);

return 0;

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值