c语言函数(acm)

来源:互联网 发布:ce源码 编辑:程序博客网 时间:2024/05/18 01:23

函数名: abort

     : 异常终止一个进程

     : void abort(void);

程序例:

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

printf("Calling abort()\n");

abort();

return 0; /* This is never reached */

}

 

 

 

函数名: abs

     : 求整数的绝对值

     : int abs(int i);

程序例:

#include <stdio.h>

#include <math.h>

 

int main(void)

{

int number = -1234;

 

printf("number: %d     absolute value: %d\n", number, abs(number));

return 0;

}

函数名: acos

     : 反余弦函数

     : double acos(double x);

程序例:

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result;

double x = 0.5;

 

result = acos(x);

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

return 0;

}

函数名: asctime

     : 转换日期和时间为ASCII

     : char *asctime(const struct tm *tblock);

程序例:

#include <stdio.h>

#include <string.h>

#include <time.h>

int main(void)

{



struct tm t;

char str[80];

 

/* sample loading of tm structure      */

 

t.tm_sec           = 1;     /* Seconds */

t.tm_min           = 30; /* Minutes */

t.tm_hour        = 9;     /* Hour */

t.tm_mday        = 22; /* Day of the Month      */

t.tm_mon           = 11; /* Month */

t.tm_year        = 56; /* Year - does not include century */

t.tm_wday        = 4;     /* Day of the week      */

t.tm_yday        = 0;     /* Does not show in asctime      */

t.tm_isdst     = 0;     /* Is Daylight SavTime; does not show in asctime */

 

/* converts structure to null terminated

string */

 

strcpy(str, asctime(&t));

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

 

return 0;

}

函数名: asin

     : 反正弦函数

     : double asin(double x);

程序例:

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result;

double x = 0.5;

 

result = asin(x);

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

return(0);

}

函数名: atan

     : 反正切函数

     : double atan(double x);

程序例:

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result;

double x = 0.5;

 

result = atan(x);

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

return(0);

}

 

 

 

函数名: atan2

     : 计算Y/X的反正切值

     : double atan2(double y, double x);

程序例:

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result;

double x = 90.0, y = 45.0;

result = atan2(y, x);

printf("The arc tangent ratio of %lf is %lf\n", (/ x), result);

return 0;

}

函数名: atof

     : 把字符串转换成浮点数

     : double atof(const char *nptr);

程序例:

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

float f;

char *str = "12345.67";

 

= atof(str);

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

return 0;

}

函数名: atoi

     : 把字符串转换成长整型数

     : int atoi(const char *nptr);

程序例:

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

int n;

char *str = "12345.67";

 

= atoi(str);

printf("string = %s integer = %d\n", str, n);

return 0;

}

 

 

 

函数名: atol

     : 把字符串转换成长整型数

     : long atol(const char *nptr);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

long l;

char *str = "98765432";

 

= atol(lstr);

printf("string = %s integer = %ld\n", str, l);

return(0);

}

函数名: bsearch

     : 二分法搜索

用法: 

void *bsearch(const void *keyconst void *base, size_t nmemsize_t size, int (*comp)(const void *, const void *));//数据必须是经过预先排序的,而排序的规则要和comp所指向比较子函数的规则相同。

程序例:

#include <stdio.h>
#include <stdlib.h>

#define NUM8

int compare(const void *p, const void *q)
{
    return (*(int *)- *(int *)q);
}

int main(int argc, char *argv[])
{
    int array[NUM] = {9, 2, 7, 11, 3, 87, 34, 6};
    int key = 3;
    int *p;

    
    qsort(array, NUM, sizeof(int), compare);
    p = (int *)bsearch(&key, array, NUM, sizeof(int), compare);

    (== NULL) ? puts("notfound") : puts("found");

    return 0;
}

函数名: cabs

     : 计算复数的绝对值

     : double cabs(struct complex z);

程序例:

#include <stdio.h>

#include <math.h>

 

int main(void)

{

struct complex z;

double val;

 

z.= 2.0;

z.= 1.0;

val = cabs(z);

 

printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);

return 0;

}

 

 

 

 

函数名: calloc

     : 分配主存储器

     : void *calloc(size_t nelem, size_t elsize);

程序例:

 

#include <stdio.h>

#include <alloc.h>

 

int main(void)

{

char *str = NULL;

 

/* allocate memory for string */

str = calloc(10, sizeof(char));

 

/* copy "Hello" into string */

strcpy(str, "Hello");

 

/* display string */

printf("String is %s\n", str);

 

/* free memory */

free(str);

 

return 0;

}

 

 

 

 

函数名: ceil

     : 向上舍入

     : double ceil(double x);

程序例:

 


#include <math.h>

#include <stdio.h>

 

int main(void)

{

double number = 123.54;

double down, up;

 

down = floor(number);

up = ceil(number);

 

printf("original number             %5.2lf\n", number);

printf("number rounded down %5.2lf\n", down);

printf("number rounded up        %5.2lf\n", up);

 

return 0;

}

 

 

 

 

函数名: cgets

     : 从控制台读字符串

     : char *cgets(char *str);

程序例:

 

#include <stdio.h>

#include <conio.h>

 

int main(void)

{

char buffer[83];

char *p;

 

/* There's space for 80 characters plus the NULL terminator */

buffer[0] = 81;

 

printf("Input some chars:");

= cgets(buffer);

printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);

printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);

 

/* Leave room for 5 characters plus the NULL terminator */

buffer[0] = 6;

 

printf("Input some chars:");

= cgets(buffer);

printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);

printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);

return 0;

}

函数名: clock

     : 确定处理器时间

     : clock_t clock(void);

程序例:

 

#include <time.h>

#include <stdio.h>

#include <dos.h>

 

int main(void)

{

clock_t start, end;

start = clock();

 

delay(2000);

 

end = clock();

printf("The time was: %f\n", (end - start) / CLK_TCK);

 

return 0;

}

函数名: cos

     : 余弦函数

     : double cos(double x);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result;

double x = 0.5;

 

result = cos(x);

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

return 0;

}

 

 

 

 

函数名: cosh

     : 双曲余弦函数

     : dluble cosh(double x);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result;

double x = 0.5;

 

result = cosh(x);

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

return 0;

}

函数名: cprintf

     : 送格式化输出至屏幕

     : int cprintf(const char *format[, argument, ...]);

程序例:

 

#include <conio.h>

 

int main(void)

{

/* clear the screen */

clrscr();

 

/* create a text window */

window(10, 10, 80, 25);

 

/* output some text in the window */

cprintf("Hello world\r\n");

 

/* wait for a key */

getch();

return 0;

}

函数名: cputs

     : 写字符到屏幕

     : void cputs(const char *string);

程序例:

 

#include <conio.h>

 

int main(void)

{

/* clear the screen */

clrscr();

 

/* create a text window */

window(10, 10, 80, 25);

 

/* output some text in the window */

cputs("This is within the window\r\n");

 

/* wait for a key */

getch();

return 0;

}

函数名: cscanf

     : 从控制台执行格式化输入

     : int cscanf(char *format[,argument, ...]);

程序例:

 

#include <conio.h>

 

int main(void)

{

char string[80];

 

/* clear the screen */

clrscr();

 

/* Prompt the user for input */

cprintf("Enter a string with no spaces:");

 

/* read the input */

cscanf("%s", string);

 

/* display what was read */

cprintf("\r\nThe string entered is: %s", string);

return 0;

}

 

 

 

 

函数名: ctime

     : 把日期和时间转换为字符串

     : char *ctime(const time_t *time);

程序例:

 

#include <stdio.h>

#include <time.h>

 

int main(void)

{

time_t t;

 

time(&t);

printf("Today's date and time: %s\n", ctime(&t));

return 0;

}

函数名: difftime

     : 计算两个时刻之间的时间差

     : double difftime(time_t time2, time_t time1);

程序例:

 

#include <time.h>

#include <stdio.h>

#include <dos.h>

#include <conio.h>

 

int main(void)

{

time_t first, second;

 

clrscr();

first = time(NULL);     /* Gets system

time */

delay(2000);                       /* Waits 2 secs */

second = time(NULL); /* Gets system time

again */

 

printf("The difference is: %f \

seconds\n",difftime(second,first));

getch();

 

return 0;

}

 

函数名: div

     : 将两个整数相除, 返回商和余数

     : div_t (int number, int denom);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

div_t x;

 

int main(void)

{

= div(10,3);

printf("10 div 3 = %d remainder %d\n", x.quot, x.rem);

 

return 0;

}

函数名: ecvt

     : 把一个浮点数转换为字符串

     : char ecvt(double value, int ndigit, int *decpt, int *sign);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

 

int main(void)

{

char *string;

double value;

int dec, sign;

int ndig = 10;

 

clrscr();

value = 9.876;

string = ecvt(value, ndig, &dec, &sign);

printf("string = %s                dec = %d \

sign = %d\n", string, dec, sign);

 

value = -123.45;

ndig= 15;

string = ecvt(value,ndig,&dec,&sign);

printf("string = %s dec = %d sign = %d\n",

string, dec, sign);

 

 

value = 0.6789e5; /* scientific

notation */

ndig = 5;

string = ecvt(value,ndig,&dec,&sign);

printf("string = %s                             dec = %d\

sign = %d\n", string, dec, sign);

 

return 0;

}

函数名: eof

     : 检测文件结束

     : int eof(int *handle);

程序例:

 

#include <sys\stat.h>

#include <string.h>

#include <stdio.h>

#include <fcntl.h>

#include <io.h>

 

int main(void)

{

int handle;

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

char ch;

 

/* create a file */

handle = open("DUMMY.FIL",

O_CREAT | O_RDWR,

S_IREAD | S_IWRITE);

 

/* write some data to the file */

write(handle, msg, strlen(msg));

 

/* seek to the beginning of the file */

lseek(handle, 0L, SEEK_SET);

 

/*

reads chars from the file until hit EOF

*/

do

{

read(handle, &ch, 1);

printf("%c", ch);

} while (!eof(handle));

 

close(handle);

return 0;

}

函数名: exit

     : 终止程序

     : void exit(int status);

程序例:

 

#include <stdlib.h>

#include <conio.h>

#include <stdio.h>

 

int main(void)

{

int status;

 

printf("Enter either 1 or 2\n");

status = getch();

/* Sets DOS errorlevel     */

exit(status - '0');

 

/* Note: this line is never reached */

return 0;

}

函数名: exp


页码,47/265


 

     : 指数函数

     : double exp(double x);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result;

double x = 4.0;

 

result = exp(x);

printf("'e' raised to the power \

of %lf (e ^ %lf) = %lf\n",

x, x, result);

 

return 0;

}

 

函数名: fabs

     : 返回浮点数的绝对值

     : double fabs(double x);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

float     number = -1234.0;

 

printf("number: %f     absolute value: %f\n",

number, fabs(number));

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";

 

/* allocate memory for the far pointer */

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

 

/* copy "Hello" into allocated memory */

/*

Note: movedata is used because you

might be in a small data model, in

which case a normal string copy routine

can not be used since it assumes the

pointer size is near.

*/

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

FP_SEG(fptr), FP_OFF(fptr),

strlen(str));

 

/* display string (note the F modifier) */

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

 

/* free the memory */

farfree(fptr);

 

return 0;

}

 

 

 

 

函数名: farcoreleft

     : 返回远堆中未作用存储区大小

     : long farcoreleft(void);

程序例:

 

#include <stdio.h>

#include <alloc.h>

 

int main(void)

{

printf("The difference between the\

highest allocated block in the\

far\n");

printf("heap and the top of the far heap\

is: %lu bytes\n", farcoreleft());

 

return 0;

}

函数名: farfree

     : 从远堆中释放一块

     : void farfree(void);

程序例:

 

#include <stdio.h>

#include <alloc.h>

#include <string.h>

#include <dos.h>

 

int main(void)

{

char far *fptr;

char *str = "Hello";

 

/* allocate memory for the far pointer */

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

 

/* copy "Hello" into allocated memory */

/*

Note: movedata is used because you might be in a small data model,

in which case a normal string copy routine can't be used since it

assumes the pointer size is near.

*/

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

FP_SEG(fptr), FP_OFF(fptr),

strlen(str));

 

/* display string (note the F modifier) */

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

 

/* free the memory */

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";

 

/* allocate memory for the far pointer */

fptr = farmalloc(10);

 

/* copy "Hello" into allocated memory */

/*

Note: movedata is used because we might

be in a small data model, in which case

a normal string copy routine can not be

used since it assumes the pointer size

is near.

*/

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

FP_SEG(fptr), FP_OFF(fptr),

strlen(str));

 

/* display string (note the F modifier) */

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

 

/* free the memory */

farfree(fptr);

 

return 0;

}

 

 

 

 

函数名: farrealloc

     : 调整远堆中的分配块

     : void far *farrealloc(void far *block, unsigned long newsize);

程序例:

 

#include <stdio.h>

#include <alloc.h>

 

int main(void)

{

char far *fptr;

 

fptr = farmalloc(10);

printf("First address: %Fp\n", fptr);

fptr = farrealloc(fptr,20);

printf("New address     : %Fp\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";

 

/* create a file containing 10 bytes */

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

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

 

/* close the file */

fclose(fp);

return 0;

}

 

 

 

 

函数名: fcloseall

     : 关闭打开流

     : int fcloseall(void);

程序例:

 

#include <stdio.h>

 

int main(void)

{

int streams_closed;

 

/* open two streams */

fopen("DUMMY.ONE", "w");

fopen("DUMMY.TWO", "w");

 

/* close the open streams */

streams_closed = fcloseall();

 

if (streams_closed == EOF)

/* issue an error message */

perror("Error");

else

/* print result of fcloseall() function */

printf("%d streams were closed.\n", streams_closed);

 

return 0;

}

函数名: fcvt

     : 把一个浮点数转换为字符串

     : char *fcvt(double value, int ndigit, int *decpt, int *sign);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

 

int main(void)

{

char *string;

double value;

int dec, sign;

int ndig = 10;

 

clrscr();

value = 9.876;

string = ecvt(value, ndig, &dec, &sign);

printf("string = %s                dec = %d \

sign = %d\n", string, dec, sign);

 

value = -123.45;

ndig= 15;

string = ecvt(value,ndig,&dec,&sign);

printf("string = %s dec = %d sign = %d\n",

string, dec, sign);

 

 

value = 0.6789e5; /* scientific

notation */

ndig = 5;

string = ecvt(value,ndig,&dec,&sign);

printf("string = %s                             dec = %d\

sign = %d\n", string, dec, sign);

 

return 0;

}

 

 

 

 

函数名: fdopen

     : 把流与一个文件句柄相接

     : FILE *fdopen(int handle, char *type);

程序例:

 

#include <sys\stat.h>

#include <stdio.h>

include <fcntl.h>

#include <io.h>

 

int main(void)

{

int handle;

FILE *stream;

 

/* open a file */

handle = open("DUMMY.FIL", O_CREAT,

S_IREAD | S_IWRITE);

 

/* now turn the handle into a stream */

stream = fdopen(handle, "w");

 

if (stream == NULL)

printf("fdopen failed\n");

else

{

fprintf(stream, "Hello world\n");

fclose(stream);

}

return 0;

}

 

 

 

函数名: feof

     : 检测流上的文件结束符

     : int feof(FILE *stream);

程序例:

 

#include <stdio.h>

 

int main(void)

{

FILE *stream;

 

/* open a file for reading */

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

 

/* read a character from the file */

fgetc(stream);

 

/* check for EOF */

if (feof(stream))

printf("We have reached end-of-file\n");

 

/* close the file */

fclose(stream);

return 0;

}

函数名: ferror

     : 检测流上的错误

     : int ferror(FILE *stream);

程序例:

 

#include <stdio.h>

 

int main(void)

{

FILE *stream;

 

/* open a file for writing */

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

 

/* force an error condition by attempting to read */

(void) getc(stream);

 

if (ferror(stream))     /* test for an error on the stream */

{

/* display an error message */

printf("Error reading from DUMMY.FIL\n");

 

/* reset the error and EOF indicators */

clearerr(stream);

}

 

fclose(stream);

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";

 

/* create a file */

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

 

/* write some data to the file */

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

 

clrscr();

printf("Press any key to flush\

DUMMY.FIL:");

getch();

 

/* flush the data to DUMMY.FIL without\

closing it */

flush(stream);

 

printf("\nFile was flushed, Press any key\

to quit:");

getch();

return 0;

}

 

void flush(FILE *stream)

{

int duphandle;

 

/* flush the stream's internal buffer */

fflush(stream);

 

/* make a duplicate file handle */

duphandle = dup(fileno(stream));

 

/* close the duplicate handle to flush\

the DOS buffer */

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 a character followed by \

<Enter>: ");

 

/* read the character from stdin */

ch = fgetchar();

 

/* display what was read */

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

ch);

return 0;

}

 

 

 

 

函数名: fgetpos

     : 取得当前文件的句柄

     : int fgetpos(FILE *stream);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

FILE *stream;

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

fpos_t filepos;

 

/* open a file for update */

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

 

/* write a string into the file */

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

 

/* report the file pointer position */

fgetpos(stream, &filepos);

printf("The file pointer is at byte\

%ld\n", filepos);

 

fclose(stream);

return 0;

}

 

 

 

 

函数名: fgets

     : 从流中读取一字符串

     : char *fgets(char *string, int n, FILE *stream);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

FILE *stream;

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

char msg[20];

 

/* 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 start of the file */

fseek(stream, 0, SEEK_SET); /* read a string from the file */

fgets(msg, strlen(string)+1, stream);

 

/* display the string */

printf("%s", msg);

 

fclose(stream);

return 0;

}

 

 

 

 

函数名: filelength

     : 取文件长度字节数

     : long filelength(int handle);

程序例:

 

#include <string.h>

#include <stdio.h>

#include <fcntl.h>

#include <io.h>

 

int main(void)

{

int handle;

char buf[11] = "0123456789";

 

/* create a file containing 10 bytes */

handle = open("DUMMY.FIL", O_CREAT);

write(handle, buf, strlen(buf));

 

/* display the size of the file */

printf("file length in bytes: %ld\n",

filelength(handle));

 

/* close the file */

close(handle);

return 0;

}

函数名: floor

     : 向下舍入

     : double floor(double x);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double number = 123.54;

double down, up;

 

down = floor(number);

up = ceil(number);

 

printf("original number             %10.2lf\n",

number);

printf("number rounded down %10.2lf\n",

down);

printf("number rounded up        %10.2lf\n",

up);

return 0;

}

 

 

 

 

函数名: flushall

     : 清除所有缓冲区

     : int flushall(void);

程序例:

 

#include <stdio.h>

 

int main(void)

{

FILE *stream;

 

/* create a file */

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

 

/* flush all open streams */

printf("%d streams were flushed.\n",

flushall());

 

/* close the file */

fclose(stream);

return 0;

}

 

 

 

 

函数名: fmod

     : 计算xy的模, x/y的余数

     : double fmod(double x, double y);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double x = 5.0, y = 2.0;

double result;

 

result = fmod(x,y);

printf("The remainder of (%lf / %lf) is \

%lf\n", x, y, result);

return 0;

}

函数名: fopen

     : 打开一个流

     : FILE *fopen(char *filename, char *type);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

#include <dir.h>

 

int main(void)

{

char *s;

char drive[MAXDRIVE];

char dir[MAXDIR];

char file[MAXFILE];

char ext[MAXEXT];

int flags;

 

s=getenv("COMSPEC"); /* get the comspec environment parameter */

flags=fnsplit(s,drive,dir,file,ext);

 

printf("Command processor info:\n");

if(flags & DRIVE)

printf("\tdrive: %s\n",drive);

if(flags & DIRECTORY)

printf("\tdirectory: %s\n",dir);

if(flags & FILENAME)

printf("\tfile: %s\n",file);

if(flags & EXTENSION)

printf("\textension: %s\n",ext);

 

return 0;

}

 

 

函数名: fprintf

     : 传送格式化输出到一个流中

     : int fprintf(FILE *stream, char *format[, argument,...]);

程序例:

 

/* Program to create backup of the

AUTOEXEC.BAT file */

 

#include <stdio.h>

 

int main(void)

{

FILE *in, *out;

 

if ((in = fopen("\\AUTOEXEC.BAT", "rt"))

== NULL)

{

fprintf(stderr, "Cannot open input \

file.\n");

return 1;

}

 

if ((out = fopen("\\AUTOEXEC.BAK", "wt"))

== NULL)

{

fprintf(stderr, "Cannot open output \

file.\n");

return 1;

}

 

while (!feof(in))

fputc(fgetc(in), out);

 

fclose(in);

fclose(out);

return 0;

}

函数名: fputc

     : 送一个字符到一个流中

     : int fputc(int ch, FILE *stream);

程序例:

#include <stdio.h>

 

int main(void)

{

char msg[] = "Hello world";

int i = 0;

 

while (msg[i])

{

fputc(msg[i], stdout);

i++;

}

return 0;

}

 

 

 

 

函数名: fputchar

     : 送一个字符到标准输出流(stdout)

     : int fputchar(char ch);

程序例:

 

#include <stdio.h>

 

int main(void)

{

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

int i = 0;

 

while (msg[i])

{

fputchar(msg[i]);

i++;

}

return 0;

}

 

 

 

 

函数名: fputs

     : 送一个字符到一个流中

     : int fputs(char *string, FILE *stream);

程序例:

 

#include <stdio.h>

 

int main(void)

{

/* write a string to standard output */

fputs("Hello world\n", stdout);

return 0;

}

 

 

 

 

函数名: fread

     : 从一个流中读数据

     : int fread(void *ptr, int size, int nitems, FILE *stream);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

FILE *stream;

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

char buf[20];

 

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

== NULL)

{

fprintf(stderr,

"Cannot open output file.\n");

return 1;

}

 

/* write some data to the file */

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

 

/* seek to the beginning of the file */

fseek(stream, SEEK_SET, 0);

 

/* read the data and display it */

fread(buf, strlen(msg)+1, 1, stream);

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

 

fclose(stream);

return 0;

}

 

 

 

函数名: free

     : 释放已分配的块

     : void free(void *ptr);

程序例:

 

#include <string.h>

#include <stdio.h>

#include <alloc.h>

int main(void)

{

char *str;

 

/* allocate memory for string */

str = malloc(10);

 

/* copy "Hello" to string */

strcpy(str, "Hello");

 

/* display string */

printf("String is %s\n", str);

 

/* free memory */

free(str);

 

return 0;

}

函数名: freemem

     : 释放先前分配的DOS内存块

     : int freemem(unsigned seg);

程序例:

 

#include <dos.h>

#include <alloc.h>

#include <stdio.h>

 

int main(void)

{

unsigned int size, segp;

int stat;

 

size = 64; /* (64 x 16) = 1024 bytes */

stat = allocmem(size, &segp);

if (stat < 0)

printf("Allocated memory at segment:\

%x\n", segp);

else

printf("Failed: maximum number of\

paragraphs available is %u\n",

stat);

freemem(segp);

 

return 0;

}

 

 

 

函数名: freopen

     : 替换一个流


页码,70/265


 

     : FILE *freopen(char *filename, char *type, FILE *stream);

程序例:

 

#include <stdio.h>

 

int main(void)

{

/* redirect standard output to a file */

if (freopen("OUTPUT.FIL", "w", stdout)

== NULL)

fprintf(stderr, "error redirecting\

stdout\n");

 

/* this output will go to a file */

printf("This will go into a file.");

 

/* close the standard output stream */

fclose(stdout);

 

return 0;

}

 

 

 

 

函数名: frexp

     : 把一个双精度数分解为尾数的指数

     : double frexp(double value, int *eptr);

程序例:

 

#include <math.h>

#include <stdio.h>

 

int main(void)

{

double mantissa, number;

int exponent;

 

number = 8.0;

mantissa = frexp(number, &exponent);

 

printf("The number %lf is ", number);

printf("%lf times two to the ", mantissa);

printf("power of %d\n", exponent);

 

return 0;

}

 

 

 

函数名: fscanf

     : 从一个流中执行格式化输入

     : int fscanf(FILE *stream, char *format[,argument...]);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

int i;

 

printf("Input an integer: ");

 

/* read an integer from the

standard input stream */

if (fscanf(stdin, "%d", &i))

printf("The integer read was: %i\n",

i);

else

{

fprintf(stderr, "Error reading an \

integer from stdin.\n");

exit(1);

}

return 0;

}

函数名: gcvt

     : 把浮点数转换成字符串

     : char *gcvt(double value, int ndigit, char *buf);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

char str[25];

double num;

int sig = 5; /* significant digits */

 

/* a regular number */

num = 9.876;

gcvt(num, sig, str);

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

 

/* a negative number */

num = -123.4567;

gcvt(num, sig, str);

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

 

/* scientific notation */

num = 0.678e5;

gcvt(num, sig, str);

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

 

return(0);

}

函数名: getc

     : 从流中取字符

     : int getc(FILE *stream);

程序例:

 

#include <stdio.h>

 

int main(void)

{

char ch;

 

printf("Input a character:");

/* read a character from the

standard input stream */

ch = getc(stdin);

printf("The character input was: '%c'\n",

ch);

return 0;

}

函数名: getch

     : 从控制台无回显地取一个字符

     : int getch(void);

程序例:

 

#include <stdio.h>

#include <conio.h>

 

int main(void)

{

char ch;

 

printf("Input a character:");

ch = getche();

printf("\nYou input a '%c'\n", ch);

return 0;

}

 

 

 

函数名: getchar

     : stdin流中读字符

     : int getchar(void);

程序例:

 

#include <stdio.h>

 

int main(void)

{

int c;

 

/* Note that getchar reads from stdin and

is line buffered; this means it will

not return until you press ENTER. */

 

while ((= getchar()) != '\n')

printf("%c", c);

 

return 0;

}

函数名: gets

     : 从流中取一字符串

     : char *gets(char *string);

程序例:

               

#include <stdio.h>

 

int main(void)

{

char string[80];

 

printf("Input a string:");

gets(string);

printf("The string input was: %s\n",

string);

return 0;

}

函数名: hypot

    : 计算直角三角形的斜边长

    : double hypot(double x, double y);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result;

double x = 3.0;

double y = 4.0;

 

result = hypot(x, y);

printf("The hypotenuse is: %lf\n", result);

 

return 0;

}

函数名: itoa

     : 把一整数转换为字符串

     : char *itoa(int value, char *string, int radix);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

int number = 12345;

char string[25];

 

itoa(number, string, 10);

printf("integer = %d string = %s\n", number, string);

return 0;

}

函数名: labs

     : 取长整型绝对值

     : long labs(long n);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

long result;

long x = -12345678L;

 

result= labs(x);

printf("number: %ld abs value: %ld\n",

x, result);

 

return 0;

}

 

 

 

 

函数名: ldexp

     : 计算value*2的幂

     : double ldexp(double value, int exp);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double value;

double x = 2;

 

/* ldexp raises 2 by a power of 3

then multiplies the result by 2         */

value = ldexp(x,3);

printf("The ldexp value is: %lf\n",

value);

 

return 0;

}

 

 

 

函数名: ldiv

     : 两个长整型数相除, 返回商和余数

     : ldiv_t ldiv(long lnumer, long ldenom);

程序例:

 

/* ldiv example */

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

ldiv_t lx;

 

lx = ldiv(100000L, 30000L);

printf("100000 div 30000 = %ld remainder %ld\n", lx.quot, lx.rem);

return 0;

}

 

 

 

 

函数名: lfind

     : 执行线性搜索

     : void *lfind(void *key, void *base, int *nelem, int width,

int (*fcmp)());

程序例:

 

#include <stdio.h>

#include <stdlib.h>

 

int compare(int *x, int *y)

{

return( *- *);

}

 

int main(void)

{

int array[5] = {35, 87, 46, 99, 12};

size_t nelem = 5;

int key;

int *result;

 

key = 99;

result = lfind(&key, array, &nelem,

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

if (result)

printf("Number %d found\n",key);

else

printf("Number %d not found\n",key);

 

return 0;

}

函数名: localtime

     : 把日期和时间转变为结构

     : struct tm *localtime(long *clock);

程序例:

 

#include <time.h>

#include <stdio.h>

#include <dos.h>

int main(void)

{

time_t timer;

struct tm *tblock;

 

/* gets time of day */

timer = time(NULL);

 

/* converts date/time to a structure */

tblock = localtime(&timer);

 

printf("Local time is: %s", asctime(tblock));

 

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 common log of %lf is %lf\n", x, result);

return 0;

}

函数名: lrotl, _lrotl

     : 将无符号长整型数向左循环移位

     : unsigned long lrotl(unsigned long lvalue, int count);

unsigned long _lrotl(unsigned long lvalue, int count);

程序例:

 

/* lrotl example */

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

unsigned long result;

unsigned long value = 100;

 

result = _lrotl(value,1);

printf("The value %lu rotated left one bit is: %lu\n", value, result);

 

return 0;

}

函数名: lsearch

     : 线性搜索

     : void *lsearch(const void *key, void *base, size_t *nelem,

size_t width, int (*fcmp)(const void *, const void *));

程序例:

 

#include <stdio.h>

#include <stdlib.h>

 

int compare(int *x, int *y)

{

return( *- *);

}

 

int main(void)

{

int array[5] = {35, 87, 46, 99, 12};

size_t nelem = 5;

int key;

int *result;

 

key = 99;

result = lfind(&key, array, &nelem,

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

if (result)

printf("Number %d found\n",key);

else

printf("Number %d not found\n",key);

 

return 0;

}

main()主函数

 

每一程序都必须有一main()函数, 可以根据自己的爱好把它放在程序的某个地方。有些

1. main() 参数

Turbo C2.0启动过程中, 传递main()函数三个参数: argc, argvenv

* argc:      整数, 为传给main()的命令行参数个数。

* argv:      字符串数组。

DOS 3.版本中, argv[0] 为程序运行的全路径名; DOS 3.0

argv[1] 为在DOS命令行中执行程序名后的第一个字符串;

argv[2] 为执行程序名后的第二个字符串;

...

argv[argc]NULL

*env:     安符串数组。env[] 的每一个元素都包含ENVVAR=value形式的字符串。其中ENVV

Turbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中说明(或不说

请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下的例子:

main()

main(int argc)

main(int argc, char *argv[])

main(int argc, char *argv[], char *env[])

其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不用argv[]的情

以下提供一样例程序EXAMPLE.EXE,   演示如何在main()函数中使用三个参数:

/*program name EXAMPLE.EXE*/

#include <stdio.h>

#include <stdlib.h>

main(int argc, char *argv[], char *env[])

{

int i;

printf("These are the %d     command- line     arguments passed     to

main:\n\n", argc);

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

printf("argv[%d]:%s\n", i, argv[i]);

printf("\nThe environment string(s)on this system are:\n\n");

for(i=0; env[i]!=NULL; i++)

printf(" env[%d]:%s\n", i, env[i]);

}

如果在DOS 提示符下, 按以下方式运行EXAMPLE.EXE:

C:\example first_argument "argument with blanks"      3     4     "last     butone" stop!

注意: 可以用双引号括起内含空格的参数, 如本例中的:        "     argumentwith blanks"

结果是这样的:

The value of argc is 7

These are the 7 command-linearguments passed to main:

argv[0]:C:\TURBO\EXAMPLE.EXE

argv[1]:first_argument

argv[2]:argument with blanks

argv[3]:3

argv[4]:4

argv[5]:last but one

argv[6]:stop!

argv[7]:(NULL)

The environment string(s) on this system are:

env[0]: COMSPEC=C:\COMMAND.COM

env[1]: PROMPT=$P$G                               /*视具体设置而定*/

env[2]: PATH=C:\DOS;C:\TC               /*视具体设置而定*/

 

应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包括参数间的

函数名: malloc

     : 内存分配函数

     : void *malloc(unsigned size);

程序例:

 

#include <stdio.h>

#include <string.h>

#include <alloc.h>

#include <process.h>

 

int main(void)

{

char *str;

 

/* allocate memory for string */

/* This will generate an error when compiling */

/* with C++, use the new operator instead. */

if ((str = malloc(10)) == NULL)

{

printf("Not enough memory to allocate buffer\n");

exit(1);    /* terminate program if out of memory */

}

 

/* copy "Hello" into string */

strcpy(str, "Hello");

 

/* display string */

printf("String is %s\n", str);

 

/* free memory */

free(str);

 

return 0;

}

 

函数名: memchr

     : 在数组的前n个字节中搜索字符

     : void *memchr(void *s, char ch, unsigned n);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

char str[17];

char *ptr;

 

strcpy(str, "This is a string");

ptr = memchr(str, 'r', strlen(str));

if (ptr)

printf("The character 'r' is at position: %d\n", ptr - str);

else

printf("The character was not found\n");

return 0;

}

 

 

函数名: memcpy

     : 从源source中拷贝n个字节到目标destin

     : void *memcpy(void *destin, void *source, unsigned n);

程序例:

 

#include <stdio.h>

#include <string.h>

int main(void)

{

char src[] = "******************************";

char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";

char *ptr;

printf("destination before memcpy: %s\n", dest);

ptr = memcpy(dest, src, strlen(src));

if (ptr)

printf("destination after memcpy:     %s\n", dest);

else

printf("memcpy failed\n");

return 0;

}

 

 

 

函数名: memicmp

     : 比较两个串s1s2的前n个字节, 忽略大小写

     : int memicmp(void *s1, void *s2, unsigned n);

程序例:

 

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char *buf1 = "ABCDE123";

char *buf2 = "abcde456";

int stat;

stat = memicmp(buf1, buf2, 5);

printf("The strings to position 5 are ");

if (stat)

函数名: memmove

     : 移动一块字节

     : void *memmove(void *destin, void *source, unsigned n);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

char *dest = "abcdefghijklmnopqrstuvwxyz0123456789";

char *src = "******************************";

printf("destination prior to memmove: %s\n", dest);

memmove(dest, src, 26);

printf("destination after memmove:          %s\n", dest);

return 0;

}

 

 

 

 

函数名: memset

     : 设置s中的所有字节为ch, s数组的大小由n给定

     : void *memset(void *s, char ch, unsigned n);

程序例:

 

#include <string.h>

#include <stdio.h>

#include <mem.h>

 

int main(void)

{

char buffer[] = "Hello world\n";

 

printf("Buffer before memset: %s\n", buffer);

memset(buffer, '*', strlen(buffer) - 1);

printf("Buffer after memset:     %s\n", buffer);

return 0;

}

函数名: modf

     : 把数分为指数和尾数

     : double modf(double value, double *iptr);

程序例:

 

#include <math.h>

#include <stdio.h>

 

int main(void)

{

double fraction, integer;

double number = 100000.567;

 

fraction = modf(number, &integer);

printf("The whole and fractional parts of %lf are %lf and %lf\n",

number, integer, fraction);

return 0;

}

函数名: movedata

     : 拷贝字节

     : void movedata(int segsrc, int offsrc, int segdest,

int offdest, unsigned numbytes);

程序例:

 

#include <mem.h>

 

#define MONO_BASE 0xB000

/* saves the contents of the monochrome screen in buffer */

void save_mono_screen(char near *buffer)

{

movedata(MONO_BASE, 0, _DS, (unsigned)buffer, 80*25*2);

}

 

int main(void)

{

char buf[80*25*2];

save_mono_screen(buf);

}

 

函数名: peek

     : 检查存储单元

     : int peek(int segment, unsigned offset);

程序例:

        #include <stdio.h>

#include <conio.h>

#include <dos.h>

 

int main(void)

{

int value = 0;

 

printf("The current status of your keyboard is:\n");

value = peek(0x0040, 0x0017);

if (value & 1)

printf("Right shift on\n");

else

printf("Right shift off\n");

 

if (value & 2)

printf("Left shift on\n");

else

printf("Left shift off\n");

 

if (value & 4)

printf("Control key on\n");

else

printf("Control key off\n");

 

if (value & 8)

printf("Alt key on\n");

else

printf("Alt key off\n");

 

if (value & 16)

printf("Scroll lock on\n");

else

printf("Scroll lock off\n");

 

if (value & 32)

printf("Num lock on\n");

else

printf("Num lock off\n");

 

if (value & 64)

printf("Caps lock on\n");

else

printf("Caps lock off\n");

 

return 0;

}

 

 

 

函数名: peekb

     : 检查存储单元

     : char peekb (int segment, unsigned offset);

程序例:

 

#include <stdio.h>

#include <conio.h>

#include <dos.h>

 

int main(void)

{

int value = 0;

 

printf("The current status of your keyboard is:\n");

value = peekb(0x0040, 0x0017);

if (value & 1)

printf("Right shift on\n");

else

printf("Right shift off\n");

 

if (value & 2)

printf("Left shift on\n");

else

printf("Left shift off\n");

 

if (value & 4)

printf("Control key on\n");

else

printf("Control key off\n");

 

if (value & 8)

printf("Alt key on\n");

else

printf("Alt key off\n");

 

if (value & 16)

printf("Scroll lock on\n");

else

printf("Scroll lock off\n");

 

if (value & 32)

printf("Num lock on\n");

else

printf("Num lock off\n");

 

if (value & 64)

printf("Caps lock on\n");

else

printf("Caps lock off\n");

 

return 0;

}

函数名: pow

     : 指数函数(xy次方)

     : double pow(double x, double y);

程序例:

 

#include <math.h>

#include <stdio.h>

 

int main(void)

{

double x = 2.0, y = 3.0;

 

printf("%lf raised to %lf is %lf\n", x, y, pow(x, y));

return 0;

}

函数名: pow10

     : 指数函数(10p次方)

     : double pow10(int p);

程序例:

 

#include <math.h>

#include <stdio.h>

 

int main(void)

{

double p = 3.0;

 

printf("Ten raised to %lf is %lf\n", p, pow10(p));

return 0;

}

函数名: qsort

     : 使用快速排序例程进行排序

     : void qsort(void *base, int nelem, int width, int (*fcmp)());

程序例:

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int sort_function( const void *a, const void *b);

 

char list[5][4] = { "cat", "car", "cab", "cap", "can" };

 

 

int main(void)

{

int     x;

 

qsort((void *)list, 5, sizeof(list[0]), sort_function);

for (= 0; x < 5; x++)

printf("%s\n", list[x]);

return 0;

}

 

int sort_function( const void *a, const void *b)

{

return( strcmp(a,b) );

}

函数名: rand

     : 随机数发生器

     : void rand(void);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

int i;

 

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

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

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

return 0;

}

函数名: randomize

     : 初始化随机数发生器

     : void randomize(void);

函数名: random

     : 随机数发生器

     : int random(int num);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

 

/* prints a random number in the range 0 to 99 */

int main(void)

{

randomize();

printf("Random number in the 0-99 range: %d\n", random (100));

return 0;

}

函数名: realloc

     : 重新分配主存

     : void *realloc(void *ptr, unsigned newsize);

程序例:

 

#include <stdio.h>

#include <alloc.h>

#include <string.h>

int main(void)

{

char *str;

 

/* allocate memory for string */

str = malloc(10);

 

/* copy "Hello" into string */

strcpy(str, "Hello");

 

printf("String is %s\n     Address is %p\n", str, str);

str = realloc(str, 20);

printf("String is %s\n     New address is %p\n", str, str);

 

/* free memory */

free(str);

 

return 0;

}

函数名: sbrk

     : 改变数据段空间位置

     : char *sbrk(int incr);

程序例:

 

#include <stdio.h>

#include <alloc.h>

 

int main(void)

{

printf("Changing allocation with sbrk()\n");

printf("Before sbrk() call: %lu bytes free\n",

(unsigned long) coreleft());

sbrk(1000);

printf(" After sbrk() call: %lu bytes free\n",

(unsigned long) coreleft());

return 0;


页码,189/265


}

函数名: sin

     : 正弦函数

     : double sin(double x);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result, x = 0.5;

 

result = sin(x);

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

return 0;

}

函数名: sinh

     : 双曲正弦函数

     : double sinh(double x);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result, x = 0.5;

 

result = sinh(x);

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

return 0;

}

函数名: sprintf

     : 送格式化输出到字符串中

     : int sprintf(char *string, char *farmat [,argument,...]);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

char buffer[80];

 

sprintf(buffer, "An approximation of Pi is %f\n", M_PI);

puts(buffer);

return 0;

}

 

 

 

函数名: sqrt

     : 计算平方根

     : double sqrt(double x);

程序例:

 

#include <math.h>

#include <stdio.h>

 

int main(void)

{

double x = 4.0, result;

 

result = sqrt(x);

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

return 0;

}

 

 

函数名: srand

     : 初始化随机数发生器

     : void srand(unsigned seed);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

 

int main(void)

{

int i;

time_t t;

 

srand((unsigned) time(&t));

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

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

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

return 0;

}

函数名: sscanf

     : 执行从字符串中的格式化输入

     : int sscanf(char *string, char *format[,argument,...]);

程序例:

 

#include <stdio.h>

#include <conio.h>

 

int main(void)

{

char label[20];

char name[20];

int entries = 0;

int loop, age;

double salary;

 

struct Entry_struct

{

                char   name[20];

int        age;

float salary;

} entry[20];

 

/* Input a label as a string of characters restricting to 20 characters */

printf("\n\nPlease enter a label for the chart: ");

scanf("%20s", label);

fflush(stdin);    /* flush the input stream in case of bad input */

 

/* Input number of entries as an integer */

printf("How many entries will there be? (less than 20) ");

scanf("%d", &entries);

fflush(stdin);       /* flush the input stream in case of bad input */

 

/* input a name restricting input to only letters upper or lower case */

for (loop=0;loop<entries;++loop)

{

printf("Entry %d\n", loop);

printf("    Name        : ");

scanf("%[A-Za-z]", entry[loop].name);

fflush(stdin);    /* flush the input stream in case of bad input */

 

/* input an age as an integer */

printf("    Age           : ");

scanf("%d", &entry[loop].age);

fflush(stdin);    /* flush the input stream in case of bad input */

 

/* input a salary as a float */

printf("    Salary : ");

scanf("%f", &entry[loop].salary);

fflush(stdin); /* flush the input stream in case of bad input */

}

 

/* Input a name, age and salary as a string, integer, and double */

printf("\nPlease enter your name, age and salary\n");

scanf("%20s %d %lf", name, &age, &salary);

 

 

/* Print out the data that was input */

printf("\n\nTable %s\n",label);

printf("Compiled by %s     age %d      $%15.2lf\n", name, age, salary);

printf("-----------------------------------------------------\n");

for (loop=0;loop<entries;++loop)

printf("%4d | %-20s | %5d | %15.2lf\n",

loop + 1,

entry[loop].name,

entry[loop].age,

entry[loop].salary);

printf("-----------------------------------------------------\n");

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++", *Borland = "Borland";

 

strcpy(destination, Borland);

strcat(destination, blank);

strcat(destination, c);

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

return 0;


页码,234/265


}

 

 

 

 

函数名: strchr

     : 在一个串中查找给定字符的第一个匹配之处\

     : char *strchr(char *str, char c);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

char string[15];

char *ptr, c = 'r';

 

strcpy(string, "This is a string");

ptr = strchr(string, c);

if (ptr)

printf("The character %c is at position: %d\n", c, ptr-string);

else

printf("The character was not found\n");

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 is greater than buffer 1\n");

else

printf("buffer 2 is less than buffer 1\n");

 

ptr = strcmp(buf2, buf3);

if (ptr > 0)

printf("buffer 2 is greater than buffer 3\n");

else

printf("buffer 2 is less than buffer 3\n");

 

return 0;

}

 

 

 

 

函数名: strncmpi

     : 将一个串中的一部分与另一个串比较, 不管大小写

     : int strncmpi(char *str1, char *str2, unsigned maxlen);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

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

int ptr;

 

ptr = strcmpi(buf2, buf1);

 

if (ptr > 0)

printf("buffer 2 is greater than buffer 1\n");

 

if (ptr < 0)

printf("buffer 2 is less than buffer 1\n");

 

if (ptr == 0)

printf("buffer 2 equals buffer 1\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;


页码,236/265


}

 

 

 

 

函数名: 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("Character where 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 is greater than buffer 1\n");

 

if (ptr < 0)

printf("buffer 2 is less than buffer 1\n");

 

if (ptr == 0)

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

 

return 0;

}

 

函数名: strcmpi

     : 将一个串与另一个比较, 不管大小写

     : int strcmpi(char *str1, char *str2);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

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

int ptr;

 

ptr = strcmpi(buf2, buf1);

 

if (ptr > 0)

printf("buffer 2 is greater than buffer 1\n");

 

if (ptr < 0)

printf("buffer 2 is less than buffer 1\n");

 

if (ptr == 0)

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

 

return 0;

}

 

 

 

 

函数名: strncmp

     : 串比较

     : int strncmp(char *str1, char *str2, int maxlen);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int     main(void)

 

{

char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";

int ptr;

 

ptr = strncmp(buf2,buf1,3);

if (ptr > 0)

printf("buffer 2 is greater than buffer 1\n");

else

printf("buffer 2 is less than buffer 1\n");

 

ptr = strncmp(buf2,buf3,3);

if (ptr > 0)

printf("buffer 2 is greater than buffer 3\n");

else

printf("buffer 2 is less than buffer 3\n");

return(0);

}

 

 

 

函数名: strncmpi

     : 把串中的一部分与另一串中的一部分比较, 不管大小写

     : int strncmpi(char *str1, char *str2);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

char *buf1 = "BBBccc", *buf2 = "bbbccc";

int ptr;

 

ptr = strncmpi(buf2,buf1,3);

 

if (ptr > 0)

printf("buffer 2 is greater than buffer 1\n");

 

if (ptr < 0)

printf("buffer 2 is less than buffer 1\n");

 

if (ptr == 0)

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

 

return 0;

}

 

 

 

函数名: strncpy

     : 串拷贝

     : char *strncpy(char *destin, char *source, int maxlen);

程序例:

 

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char string[10];

char *str1 = "abcdefghi";

 

strncpy(string, str1, 3);

string[3] = '\0';

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

return 0;

}

函数名: strnicmp

     : 不注重大小写地比较两个串

     : int strnicmp(char *str1, char *str2, unsigned maxlen);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

char *buf1 = "BBBccc", *buf2 = "bbbccc";

int ptr;

 

ptr = strnicmp(buf2, buf1, 3);

 

if (ptr > 0)

printf("buffer 2 is greater than buffer 1\n");

 

if (ptr < 0)

printf("buffer 2 is less than buffer 1\n");

 

if (ptr == 0)

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

 

return 0;

}

 

 

 

 

函数名: strnset

     : 将一个串中的所有字符都设为指定字符

     : char *strnset(char *str, char ch, unsigned n);

程序例:

 

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char *string = "abcdefghijklmnopqrstuvwxyz";

char letter = 'x';

 

printf("string before strnset: %s\n", string);

strnset(string, letter, 13);

printf("string after     strnset: %s\n", string);

 

return 0;

}

函数名: strpbrk

     : 在串中查找给定字符集中的字符

     : char *strpbrk(char *str1, char *str2);

程序例:

 

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char *string1 = "abcdefghijklmnopqrstuvwxyz";

char *string2 = "onm";

char *ptr;

 

ptr = strpbrk(string1, string2);

 

if (ptr)

printf("strpbrk found first character: %c\n", *ptr);

else

printf("strpbrk didn't find character in set\n");

 

return 0;

}

 

 

 

 

函数名: strrchr

     : 在串中查找指定字符的最后一个出现

     : char *strrchr(char *str, char c);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

char string[15];

char *ptr, c = 'r';

 

strcpy(string, "This is a string");

ptr = strrchr(string, c);

if (ptr)

printf("The character %c is at position: %d\n", c, ptr-string);

else

printf("The character was not found\n");

return 0;

}

函数名: strrev

     : 串倒转

     : char *strrev(char *str);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

char *forward = "string";

 

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

strrev(forward);

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

return 0;

}

 

 

函数名: strset

     : 将一个串中的所有字符都设为指定字符

     : char *strset(char *str, char c);

程序例:

 

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char string[10] = "123456789";

char symbol = 'c';

 

printf("Before strset(): %s\n", string);

strset(string, symbol);

printf("After strset():     %s\n", string);

return 0;

}

函数名: strspn

     : 在串中查找指定字符集的子集的第一次出现

     : int strspn(char *str1, char *str2);

程序例:

 

#include <stdio.h>

#include <string.h>

#include <alloc.h>

 

int main(void)

{

char *string1 = "1234567890";

char *string2 = "123DC8";

int length;

 

length = strspn(string1, string2);

printf("Character where strings differ is at position %d\n", length);

return 0;

}

 

 

 

函数名: strstr

     : 在串中查找指定字符串的第一次出现

     : char *strstr(char *str1, char *str2);

程序例:

 

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char *str1 = "Borland International", *str2 = "nation", *ptr;

 

ptr = strstr(str1, str2);

printf("The substring is: %s\n", ptr);

return 0;

}

 

 

 

函数名: strtod

     : 将字符串转换为double型值

     : double strtod(char *str, char **endptr);

程序例:

 

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

char input[80], *endptr;

double value;

 

printf("Enter a floating point number:");

gets(input);

value = strtod(input, &endptr);

printf("The string is %s the number is %lf\n", input, value);

return 0;

}

 

 

 

 

函数名: strtok

     : 查找由在第二个串中指定的分界符分隔开的单词

     : char *strtok(char *str1, char *str2);

程序例:

 

#include <string.h>

#include <stdio.h>

 

int main(void)

{

char input[16] = "abc,d";

char *p;

 

/* strtok places a NULL terminator

in front of the token, if found */

= strtok(input, ",");

if (p)        printf("%s\n", p);

 

/* A second call to strtok using a NULL

as the first parameter returns a pointer

to the character following the token      */

= strtok(NULL, ",");

if (p)        printf("%s\n", p);

return 0;

}

 

 

 

 

函数名: strtol

     : 将串转换为长整数

     : long strtol(char *str, char **endptr, int base);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

char *string = "87654321", *endptr;

long lnumber;

 

/* strtol converts string to long integer      */

lnumber = strtol(string, &endptr, 10);

printf("string = %s     long = %ld\n", string, lnumber);

 

return 0;

}

 

 

函数名: strupr

     : 将串中的小写字母转换为大写字母

     : char *strupr(char *str);

程序例:

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;

 

/* converts string to upper case characters */

ptr = strupr(string);

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

return 0;

}

 

 

 

 

函数名: swab

     : 交换字节

     : void swab (char *from, char *to, int nbytes);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

 

char source[15] = "rFna koBlrna d";

char target[15];

 

int main(void)

{

swab(source, target, strlen(source));

printf("This is target: %s\n", target);

return 0;

}

函数名: system

     : 发出一个DOS命令

     : int system(char *command);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

int main(void)

{

printf("About to spawn command.com and run a DOS command\n");

system("dir");

return 0;

}


页码,245/265


 

函数名: tan

     : 正切函数

     : double tan(double x);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result, x;

 

= 0.5;

result = tan(x);

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

return 0;

}

 

 

 

 

函数名: tanh

     : 双曲正切函数

     : double tanh(double x);

程序例:

 

#include <stdio.h>

#include <math.h>

 

int main(void)

{

double result, x;

 

= 0.5;

result = tanh(x);

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

return 0;

}

函数名: time

     : 取一天的时间

     : logn time(long *tloc);

程序例:

 

#include <time.h>

#include <stdio.h>

#include <dos.h>

 

int main(void)

{

time_t t;

 

= time(NULL);

printf("The number of seconds since January 1, 1970 is %ld",t);

return 0;

}

函数名: tolower

     : 把字符转换成小写字母

     : int tolower(int c);

程序例:

 

#include <string.h>

#include <stdio.h>

#include <ctype.h>

 

int main(void)

{

int length, i;

char *string = "THIS IS A STRING";

 

length = strlen(string);

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

{

string[i] = tolower(string[i]);

}

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

 

return 0;

}

 

 

 

函数名: toupper

     : 把字符转换成大写字母

     : int toupper(int c);

程序例:

 

#include <string.h>

#include <stdio.h>

#include <ctype.h>

 

int main(void)

{

int length, i;

char *string = "this is a string";

 

length = strlen(string);

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

{

string[i] = toupper(string[i]);

}

 

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

 

return 0;

}

 

函数名: ultoa

     : 转换一个无符号长整型数为字符串

     : char *ultoa(unsigned long value, char *string, int radix);

程序例:

 

#include <stdlib.h>

#include <stdio.h>

 

int main( void )

{

unsigned long lnumber = 3123456789L;

char string[25];

 

ultoa(lnumber,string,10);

printf("string = %s     unsigned long = %lu\n",string,lnumber);

 

return 0;

}

 

函数名: vfprintf

     : 送格式化输出到一流中

     : int vfprintf(FILE *stream, char *format, va_list param);

程序例:

 

#include <stdio.h>

#include <stdlib.h>

#include <stdarg.h>

 

FILE *fp;

 

int vfpf(char *fmt, ...)

{

va_list argptr;

int cnt;

 

va_start(argptr, fmt);

cnt = vfprintf(fp, fmt, argptr);

va_end(argptr);

 

return(cnt);

}

 

int main(void)

{

int inumber = 30;

float fnumber = 90.0;

char string[4] = "abc";

 

fp = tmpfile();

if (fp == NULL)

{

perror("tmpfile() call");

exit(1);

}

 

vfpf("%d %f %s", inumber, fnumber, string);

rewind(fp);

fscanf(fp,"%d %f %s", &inumber, &fnumber, string);

printf("%d %f %s\n", inumber, fnumber, string);

fclose(fp);

 

return 0;

}

 

 

 

 

函数名: vfscanf

     : 从流中执行格式化输入

     : int vfscanf(FILE *stream, char *format, va_list param);

程序例:

 

#include <stdio.h>

#include <stdlib.h>

#include <stdarg.h>

 

FILE *fp;

 

int vfsf(char *fmt, ...)

{

va_list     argptr;

int cnt;

 

va_start(argptr, fmt);

cnt = vfscanf(fp, fmt, argptr);

va_end(argptr);

 

return(cnt);

}

 

int main(void)

{

int inumber = 30;

float fnumber = 90.0;

char string[4] = "abc";

 

fp = tmpfile();

if (fp == NULL)

{

perror("tmpfile() call");

exit(1);

}

fprintf(fp,"%d %f %s\n",inumber,fnumber,string);

rewind(fp);

 

vfsf("%d %f %s",&inumber,&fnumber,string);

printf("%d %f %s\n",inumber,fnumber,string);

fclose(fp);

 

return 0;

}

 

 

 

函数名: vprintf

     : 送格式化输出到stdout

     : int vprintf(char *format, va_list param);

程序例:

 

#include <stdio.h>

#include <stdarg.h>

 

int vpf(char *fmt, ...)

{

va_list argptr;

int cnt;

 

va_start(argptr, format);

cnt = vprintf(fmt, argptr);

va_end(argptr);

 

return(cnt);

}

 

int main(void)

{

int inumber = 30;

float fnumber = 90.0;

char *string = "abc";

 

vpf("%d %f %s\n",inumber,fnumber,string);

 

return 0;

}

 

 

 

函数名: vscanf

     : stdin中执行格式化输入

     : int vscanf(char *format, va_list param);

程序例:

 

#include <stdio.h>

#include <conio.h>

#include <stdarg.h>

int vscnf(char *fmt, ...)

{

va_list argptr;

int cnt;

 

printf("Enter an integer, a float,      and a string (e.g. i,f,s,)\n");

va_start(argptr, fmt);

cnt = vscanf(fmt, argptr);

va_end(argptr);

 

return(cnt);

}

 

int main(void)

{

int inumber;

float fnumber;

char string[80];

 

vscnf("%d, %f, %s", &inumber, &fnumber, string);

printf("%d %f %s\n", inumber, fnumber, string);

 

return 0;

}

 

 

 

 

函数名: vsprintf

     : 送格式化输出到串中

     : int vsprintf(char *string, char *format, va_list param);

程序例:

 

#include <stdio.h>

#include <conio.h>

#include <stdarg.h>

 

char buffer[80];

 

int vspf(char *fmt, ...)

{

va_list argptr;

int cnt;

 

va_start(argptr, fmt);

cnt = vsprintf(buffer, fmt, argptr);

va_end(argptr);

 

return(cnt);

}

 

int main(void)

{

int inumber = 30;

float fnumber = 90.0;

char string[4] = "abc";

 

vspf("%d %f %s", inumber, fnumber, string);

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

return 0;

}

 

 

 

 

函数名: vsscanf

     : 从流中执行格式化输入

     : int vsscanf(char *s, char *format, va_list param);

程序例:

 

#include <stdio.h>

#include <conio.h>

#include <stdarg.h>

 

char buffer[80] = "30 90.0 abc";

 

int vssf(char *fmt, ...)

{

va_list     argptr;

int cnt;

 

fflush(stdin);

 

va_start(argptr, fmt);

cnt = vsscanf(buffer, fmt, argptr);

va_end(argptr);

 

return(cnt);

}

 

int main(void)

{

int inumber;

float fnumber;

char string[80];

 

vssf("%d %f %s", &inumber, &fnumber, string);

printf("%d %f %s\n", inumber, fnumber, string);

return 0;

}

 

0 0
原创粉丝点击