cc语言库函数
㈠ c语言字符串处理的库函数有哪些
函数名: strrchr
功 能: 在串中查找指定字符的最后一个出现
用 法: char *strrchr(char *str, char c);
举例:
[cpp] view plain
char fullname="./lib/lib1.so";
char *ptr;
ptr = strrchr(fullname,'/');
printf("filename is %s",++ptr);
//运行结果:filename is lib1.so
函数名: strchr
功 能: 在串中查找指定字符的第一个出现
用 法: char *strchr(char *str, char c);
举例:
[cpp] view plain
char fullname="./lib/lib1.so";
char *ptr;
ptr = strrchr(fullname,'.');
printf("after strchr() is %s",++ptr);
//运行结果:after strchr() is /lib/lib1.so
函数名: strtok
功 能: 在串中查找指定字符的第一个出现
用 法: char *strtok(char *s, char *delim);
说明:
1.strtok函数的实质上的处理是,strtok在s中查找包含在delim中的字符并用NULL(’/0′)来替换,直到找遍整个字符串。这句话有两层含义:(1)每次调用strtok函数只能获得一个分割单位。(2)要获得所有的分割单元必须反复调用strtok函数。
2.strtok函数以后的调用时的需用NULL来替换s.
3.形参s(要分割的字符串)对应的变量应用char s[]=”….”形式,而不能用char *s=”….”形式。
举例:
[cpp] view plain
void main()
{
char buf[]=”Golden Global View”;
char* token = strtok( buf, ” “);
while( token != NULL )
{
printf( ”%s “, token );
token = strtok( NULL, ” “);
}
return 0;
}
/*其结果为:
Golden
Global
View
*/
函数名:strncpy
功能:把src所指由NULL结束的字符串的前n个字节复制到dest所指的数组中
用法:char *strncpy(char *dest, char *src, int n);
说明:
如果src的前n个字节不含NULL字符,则结果不会以NULL字符结束。
如果src的长度小于n个字节,则以NULL填充dest直到复制完n个字节。
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
举例:
[c-sharp] view plain
#include <syslib.h>
#include <string.h>
main()
{
char buf[4];
char *s="abcdefg";
strncpy(buf,s,4);
printf("%s/n",buf);
return 0;
}
/*运行结果:
abcd
*/
函数名: stpcpy
功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char *source);
举例:
[cpp] view plain
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
stpcpy(string, str1);
printf("%s/n", string);
return 0;
}
/*运行结果
abcdefghi
*/
函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
举例:
[cpp] view plain
#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;
}
/*运行结果:
Borland C++
*/
函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0
举例:
[cpp] view plain
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1/n");
else if(ptr < 0)
printf("buffer 2 is less than buffer 1/n");
else
printf("buffer 2 is equal with buffer 1/n");
return 0;
}
/*运行结果:
buffer 2 is greater than buffer 1
*/
函数名: strncmpi
功 能: 将一个串中的一部分与另一个串比较, 不管大小写
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
举例:
[cpp] view plain
#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;
}
函数名: strcspn
功 能: 在串中查找第一个给定字符集内容的段
用 法: int strcspn(char *str1, char *str2);
举例:
[cpp] view plain
#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;
}
函数名: strp
功 能: 将串拷贝到新建的位置处
用 法: char *strp(char *str);
举例:
[cpp] view plain
#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main(void)
{
char *p_str, *string = "abcde";
p_str = strp(string);
printf("%s/n", p_str);
free(p_str);
return 0;
}
函数名: stricmp
功 能: 以大小写不敏感方式比较两个串
用 法: int stricmp(char *str1, char *str2);
举例:
[cpp] view plain
#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;
}
函数名: strerror
功 能: 返回指向错误信息字符串的指针
用 法: char *strerror(int errnum);
举例:
[cpp] view plain
#include <stdio.h>
#include <errno.h>
int main(void)
{
char *buffer;
buffer = strerror(errno);
printf("Error: %s/n", buffer);
return 0;
}
函数名: strncmp
功 能: 串比较
用 法: int strncmp(char *str1, char *str2, int maxlen);
举例:
[cpp] view plain
#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, int len);
举例:
[cpp] view plain
#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;
}
函数名: strnset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strnset(char *str, char ch, unsigned n);
举例:
[cpp] view plain
#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);
举例:
[cpp] view plain
#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;
}
函数名: strrev
功 能: 串倒转
用 法: char *strrev(char *str);
举例:
[cpp] view plain
#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;
}
/*运行结果:
Before strrev(): string
After strrev(): gnirts
*/
函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
举例:
[cpp] view plain
#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);
举例:
[cpp] view plain
#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;
}
函数名: strtol
功 能: 将串转换为长整数
用 法: long strtol(char *str, char **endptr, int base);
举例:
[cpp] view plain
#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);
举例:
[cpp] view plain
#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;
}
㈡ C语言有哪几种库函数
int isalpha(int ch) 若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0
int isalnum(int ch) 若ch是字母('A'-'Z','a'-'z')或数字('0'-'9')
返回非0值,否则返回0
int isascii(int ch) 若ch是字符(ASCII码中的0-127)返回非0值,否则返回0
int iscntrl(int ch) 若ch是作废字符(0x7F)或普通控制字符(0x00-0x1F)
返回非0值,否则返回0
int isdigit(int ch) 若ch是数字('0'-'9')返回非0值,否则返回0
int isgraph(int ch) 若ch是可打印字符(不含空格)(0x21-0x7E)返回非0值,否则返回0
int islower(int ch) 若ch是小写字母('a'-'z')返回非0值,否则返回0
int isprint(int ch) 若ch是可打印字符(含空格)(0x20-0x7E)返回非0值,否则返回0
int ispunct(int ch) 若ch是标点字符(0x00-0x1F)返回非0值,否则返回0
int isspace(int ch) 若ch是空格(' '),水平制表符('\t'),回车符('\r'),
走纸换行('\f'),垂直制表符('\v'),换行符('\n')
返回非0值,否则返回0
int isupper(int ch) 若ch是大写字母('A'-'Z')返回非0值,否则返回0
int isxdigit(int ch) 若ch是16进制数('0'-'9','A'-'F','a'-'f')返回非0值,
否则返回0
int tolower(int ch) 若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')
int toupper(int ch) 若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')
========数学函数(原型声明所在头文件为math.h、stdlib.h、string.h、float.h)===========
int abs(int i) 返回整型参数i的绝对值
double cabs(struct complex znum) 返回复数znum的绝对值
double fabs(double x) 返回双精度参数x的绝对值
long labs(long n) 返回长整型参数n的绝对值
double exp(double x) 返回指数函数ex的值
double frexp(double value,int *eptr) 返回value=x*2n中x的值,n存贮在eptr中
double ldexp(double value,int exp); 返回value*2exp的值
double log(double x) 返回logex的值
double log10(double x) 返回log10x的值
double pow(double x,double y) 返回xy的值
double pow10(int p) 返回10p的值
double sqrt(double x) 返回x的开方
double acos(double x) 返回x的反余弦cos-1(x)值,x为弧度
double asin(double x) 返回x的反正弦sin-1(x)值,x为弧度
double atan(double x) 返回x的反正切tan-1(x)值,x为弧度
double atan2(double y,double x) 返回y/x的反正切tan-1(x)值,y的x为弧度
double cos(double x) 返回x的余弦cos(x)值,x为弧度
double sin(double x) 返回x的正弦sin(x)值,x为弧度
double tan(double x) 返回x的正切tan(x)值,x为弧度
double cosh(double x) 返回x的双曲余弦cosh(x)值,x为弧度
double sinh(double x) 返回x的双曲正弦sinh(x)值,x为弧度
double tanh(double x) 返回x的双曲正切tanh(x)值,x为弧度
double hypot(double x,double y) 返回直角三角形斜边的长度(z),
x和y为直角边的长度,z2=x2+y2
double ceil(double x) 返回不小于x的最小整数
double floor(double x) 返回不大于x的最大整数
void srand(unsigned seed) 初始化随机数发生器
int rand() 产生一个随机数并返回这个数
double poly(double x,int n,double c[])从参数产生一个多项式
double modf(double value,double *iptr)将双精度数value分解成尾数和阶
double fmod(double x,double y) 返回x/y的余数
double frexp(double value,int *eptr) 将双精度数value分成尾数和阶
double atof(char *nptr) 将字符串nptr转换成浮点数并返回这个浮点数
double atoi(char *nptr) 将字符串nptr转换成整数并返回这个整数
double atol(char *nptr) 将字符串nptr转换成长整数并返回这个整数
char *ecvt(double value,int ndigit,int *decpt,int *sign)
将浮点数value转换成字符串并返回该字符串
char *fcvt(double value,int ndigit,int *decpt,int *sign)
将浮点数value转换成字符串并返回该字符串
char *gcvt(double value,int ndigit,char *buf)
将数value转换成字符串并存于buf中,并返回buf的指针
char *ultoa(unsigned long value,char *string,int radix)
将无符号整型数value转换成字符串并返回该字符串,radix为转换时所用基数
char *ltoa(long value,char *string,int radix)
将长整型数value转换成字符串并返回该字符串,radix为转换时所用基数
char *itoa(int value,char *string,int radix)
将整数value转换成字符串存入string,radix为转换时所用基数
double atof(char *nptr) 将字符串nptr转换成双精度数,并返回这个数,错误返回0
int atoi(char *nptr) 将字符串nptr转换成整型数, 并返回这个数,错误返回0
long atol(char *nptr) 将字符串nptr转换成长整型数,并返回这个数,错误返回0
double strtod(char *str,char **endptr)将字符串str转换成双精度数,并返回这个数,
long strtol(char *str,char **endptr,int base)将字符串str转换成长整型数,
并返回这个数,
int matherr(struct exception *e)
用户修改数学错误返回信息函数(没有必要使用)
double _matherr(_mexcep why,char *fun,double *arg1p,
double *arg2p,double retval)
用户修改数学错误返回信息函数(没有必要使用)
unsigned int _clear87() 清除浮点状态字并返回原来的浮点状态
void _fpreset() 重新初使化浮点数学程序包
unsigned int _status87() 返回浮点状态字
============目录函数(原型声明所在头文件为dir.h、dos.h)================
int chdir(char *path) 使指定的目录path(如:"C:\\WPS")变成当前的工作目录,成
功返回0
int findfirst(char *pathname,struct ffblk *ffblk,int attrib)查找指定的文件,成功
返回0
pathname为指定的目录名和文件名,如"C:\\WPS\\TXT"
ffblk为指定的保存文件信息的一个结构,定义如下:
┏━━━━━━━━━━━━━━━━━━┓
┃struct ffblk ┃
┃{ ┃
┃ char ff_reserved[21]; /*DOS保留字*/┃
┃ char ff_attrib; /*文件属性*/ ┃
┃ int ff_ftime; /*文件时间*/ ┃
┃ int ff_fdate; /*文件日期*/ ┃
┃ long ff_fsize; /*文件长度*/ ┃
┃ char ff_name[13]; /*文件名*/ ┃
┃} ┃
┗━━━━━━━━━━━━━━━━━━┛
attrib为文件属性,由以下字符代表
┏━━━━━━━━━┳━━━━━━━━┓
┃FA_RDONLY 只读文件┃FA_LABEL 卷标号┃
┃FA_HIDDEN 隐藏文件┃FA_DIREC 目录 ┃
┃FA_SYSTEM 系统文件┃FA_ARCH 档案 ┃
┗━━━━━━━━━┻━━━━━━━━┛
例:
struct ffblk ff;
findfirst("*.wps",&ff,FA_RDONLY);
int findnext(struct ffblk *ffblk) 取匹配finddirst的文件,成功返回0
void fumerge(char *path,char *drive,char *dir,char *name,char *ext)
此函数通过盘符drive(C:、A:等),路径dir(\TC、\BC\LIB等),
文件名name(TC、WPS等),扩展名ext(.EXE、.COM等)组成一个文件名
存与path中.
int fnsplit(char *path,char *drive,char *dir,char *name,char *ext)
此函数将文件名path分解成盘符drive(C:、A:等),路径dir(\TC、\BC\LIB等),
文件名name(TC、WPS等),扩展名ext(.EXE、.COM等),并分别存入相应的变量中.
int getcurdir(int drive,char *direc) 此函数返回指定驱动器的当前工作目录名称
drive 指定的驱动器(0=当前,1=A,2=B,3=C等)
direc 保存指定驱动器当前工作路径的变量 成功返回0
char *getcwd(char *buf,iint n) 此函数取当前工作目录并存入buf中,直到n个字
节长为为止.错误返回NULL
int getdisk() 取当前正在使用的驱动器,返回一个整数(0=A,1=B,2=C等)
int setdisk(int drive) 设置要使用的驱动器drive(0=A,1=B,2=C等),
返回可使用驱动器总数
int mkdir(char *pathname) 建立一个新的目录pathname,成功返回0
int rmdir(char *pathname) 删除一个目录pathname,成功返回0
char *mktemp(char *template) 构造一个当前目录上没有的文件名并存于template中
char *searchpath(char *pathname) 利用MSDOS找出文件filename所在路径,
,此函数使用DOS的PATH变量,未找到文件返回NULL
===========进程函数(原型声明所在头文件为stdlib.h、process.h)===========
void abort() 此函数通过调用具有出口代码3的_exit写一个终止信息于stderr,
并异常终止程序。无返回值
int exec…装入和运行其它程序
int execl( char *pathname,char *arg0,char *arg1,…,char *argn,NULL)
int execle( char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int execlp( char *pathname,char *arg0,char *arg1,…,NULL)
int execlpe(char *pathname,char *arg0,char *arg1,…,NULL,char *envp[])
int execv( char *pathname,char *argv[])
int execve( char *pathname,char *argv[],char *envp[])
int execvp( char *pathname,char *argv[])
int execvpe(char *pathname,char *argv[],char *envp[])
exec函数族装入并运行程序pathname,并将参数
arg0(arg1,arg2,argv[],envp[])传递给子程序,出错返回-1
在exec函数族中,后缀l、v、p、e添加到exec后,
所指定的函数将具有某种操作能力
有后缀 p时,函数可以利用DOS的PATH变量查找子程序文件。
l时,函数中被传递的参数个数固定。
v时,函数中被传递的参数个数不固定。
e时,函数传递指定参数envp,允许改变子进程的环境,
无后缀e时,子进程使用当前程序的环境。
void _exit(int status)终止当前程序,但不清理现场
void exit(int status) 终止当前程序,关闭所有文件,写缓冲区的输出(等待输出),
并调用任何寄存器的"出口函数",无返回值
int spawn…运行子程序
int spawnl( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL)
int spawnle( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int spawnlp( int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL)
int spawnlpe(int mode,char *pathname,char *arg0,char *arg1,…,
char *argn,NULL,char *envp[])
int spawnv( int mode,char *pathname,char *argv[])
int spawnve( int mode,char *pathname,char *argv[],char *envp[])
int spawnvp( int mode,char *pathname,char *argv[])
int spawnvpe(int mode,char *pathname,char *argv[],char *envp[])
spawn函数族在mode模式下运行子程序pathname,并将参数
arg0(arg1,arg2,argv[],envp[])传递给子程序.出错返回-1
mode为运行模式
mode为 P_WAIT 表示在子程序运行完后返回本程序
P_NOWAIT 表示在子程序运行时同时运行本程序(不可用)
P_OVERLAY表示在本程序退出后运行子程序
在spawn函数族中,后缀l、v、p、e添加到spawn后,
所指定的函数将具有某种操作能力
有后缀 p时, 函数利用DOS的PATH查找子程序文件
l时, 函数传递的参数个数固定.
v时, 函数传递的参数个数不固定.
e时, 指定参数envp可以传递给子程序,允许改变子程序运行环境.
当无后缀e时,子程序使用本程序的环境.
int system(char *command) 将MSDOS命令command传递给DOS执行
======转换子程序(函数原型所在头文件为math.h、stdlib.h、ctype.h、float.h)========
char *ecvt(double value,int ndigit,int *decpt,int *sign)
将浮点数value转换成字符串并返回该字符串
char *fcvt(double value,int ndigit,int *decpt,int *sign)
将浮点数value转换成字符串并返回该字符串
char *gcvt(double value,int ndigit,char *buf)
将数value转换成字符串并存于buf中,并返回buf的指针
char *ultoa(unsigned long value,char *string,int radix)
将无符号整型数value转换成字符串并返回该字符串,radix为转换时所用基数
char *ltoa(long value,char *string,int radix)
将长整型数value转换成字符串并返回该字符串,radix为转换时所用基数
char *itoa(int value,char *string,int radix)
将整数value转换成字符串存入string,radix为转换时所用基数
double atof(char *nptr) 将字符串nptr转换成双精度数,并返回这个数,错误返回0
int atoi(char *nptr) 将字符串nptr转换成整型数, 并返回这个数,错误返回0
long atol(char *nptr) 将字符串nptr转换成长整型数,并返回这个数,错误返回0
double strtod(char *str,char **endptr)将字符串str转换成双精度数,并返回这个数,
long strtol(char *str,char **endptr,int base)将字符串str转换成长整型数,
并返回这个数,
int toascii(int c) 返回c相应的ASCII
int tolower(int ch) 若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')
int _tolower(int ch) 返回ch相应的小写字母('a'-'z')
int toupper(int ch) 若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')
int _toupper(int ch) 返回ch相应的大写字母('A'-'Z')
㈢ C语言常用的函数有哪些
C语言库函数,常用库函数有:
1、scanf格式输入函数
2、printf格式输出函数
3、systemdos命令函数
4、sort排序
5、main主函数
6、fgets文件读取字符串函数
7、fputs文件写入字符串函数
8、fscanf文件格式读取函数
9、fprintf文件格式写入函数
10、fopen打开文件函数
11、getchar输入字符函数
12、putchar输出字符函数
13、malloc动态申请内存函数
14、free释放内存函数
15、abs求绝对值数学函数
16、sqrt求平方根数学函数
(3)cc语言库函数扩展阅读
语言组成:
1、数据类型
C的数据类型包括:整型、字符型、实型或浮点型(单精度和双精度)、枚举类型、数组类型、结构体类型、共用体类型、指针类型和空类型。
2、常量与变量
常量其值不可改变,符号常量名通常用大写。
变量是以某标识符为名字,其值可以改变的量。标识符是以字母或下划线开头的一串由字母、数字或下划线构成的序列,请注意第一个字符必须为字母或下划线,否则为不合法的变量名。变量在编译时为其分配相应存储单元。
3、数组
如果一个变量名后面跟着一个有数字的中括号,这个声明就是数组声明。字符串也是一种数组。它们以ASCII的NULL作为数组的结束。要特别注意的是,方括内的索引值是从0算起的。
4、指针
如果一个变量声明时在前面使用 * 号,表明这是个指针型变量。换句话说,该变量存储一个地址,而 *(此处特指单目运算符 * ,下同。C语言中另有 双目运算符 *) 则是取内容操作符,意思是取这个内存地址里存储的内容。指针是 C 语言区别于其他同时代高级语言的主要特征之一。
㈣ c语言里面的库函数是什么一个概念
库函数:顾名思义是把函数放到库里..是别人把一些常用到的函数编完放到一个文件里,供别人用.别人用的时候把它所在的文件名用#include<>加到里面就可以了.一般是放到lib文件里的。
一般是指编译器提供的可在c源程序中调用的函数。可分为两类一类是c语言标准规定的库函数一类是编译器特定的库函数。
由于版权原因库函数的源代码一般是不可见的但在头文件中你可以看到它对外的接口。什么是库函数语言的语句十分简单如果要使用语言的语句直接计算sin或cos函数就需要编写颇为复杂的程序。因为语言的语句中没有提供直接计算sin或cos函数的语句。又如为了显示一段文字我们在语言中也找不到显示语句只能使用库函数printf。
语言的库函数并不是语言本身的一部分它是由编译程序根据一般用户的需要编制并提供用户使用的一组程序。的库函数极大地方便了用户同时也补充了语言本身的不足。事实上在编写语言程序时应当尽可能多地使用库函数这样既可以提高程序的运行效率又可以提高编程的质量。
㈤ C语言的库函数放在什么地方(在什么目录里)
C语言的库函数在LIB目录里面。
库函数是将函数封装入库,供用户使用的一种方式。方法是把一些常用到的函数编完放到一个文件里,供不同的人进行调用。调用的时候把它所在的文件名用#include<>加到里面就可以了。一般是放到lib文件里的。
(5)cc语言库函数扩展阅读:
C语言的语句十分简单,如果要使用C语言的语句直接计算sin或cos函数,就需要编写颇为复杂的程序。因为C语言的语句中没有提供直接计算sin或cos函数的语句。又如为了显示一段文字,我们在C语言中也找不到显示语句,只能使用库函数printf。
C语言的库函数并不是C语言本身的一部分,它是由编译程序根据一般用户的需要编制并提供用户使用的一组程序。C的库函数极大地方便了用户,同时也补充了C语言本身的不足。事实上,在编写C语言程序时,应当尽可能多地使用库函数,这样既可以提高程序的运行效率,又可以提高编程的质量。
㈥ C语言库函数如何编写
自己可以编写一个头文件的,而且编写好之后放到编译器安装目录下的include目录里面,在以后编写程序的时候就可以#include <filename.h>了。比如编写一个头文件:
color.h:
#include <Windows.h>
void SetColor (size_t num)
{
HANDLE Consolehwnd;
Consolehwnd = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute (Consolehwnd, num);
}
在以后的程序中#inlcude <color.h>是可以的。但是注意只能在自己的编译器中运行哦。
㈦ C语言中什么是库函数
库函数(Library function)是把函数放到库里,供别人使用的一种方式。.方法是把一些常用到的函数编完放到一个文件里,供不同的人进行调用。调用的时候把它所在的文件名用#include>加到里面就可以了。一般是放到lib文件里的。
一般是指编译器提供的可在c源程序中调用的函数。可分为两类,一类是c语言标准规定的库函数,一类是编译器特定的库函数。
由于版权原因,库函数的源代码一般是不可见的,但在头文件中你可以看到它对外的接口
库函数简介。
C语言的语句十分简单,如果要使用C语言的语句直接计算sin或cos函数,就需要编写颇为复杂的程序。因为C语言的语句中没有提供直接计算sin或cos函数的语句。又如为了显示一段文字,我们在C语言中也找不到显示语句,只能使用库函数printf。
C语言的库函数并不是C语言本身的一部分,它是由编译程序根据一般用户的需要编制并提供用户使用的一组程序。C的库函数极大地方便了用户,同时也补充了C语言本身的不足。事实上,在编写C语言程序时,应当尽可能多地使用库函数,这样既可以提高程序的运行效率,又可以提高编程的质量。
这里调用的是静态库。
函数库:函数库是由系统建立的具有一定功能的函数的集合。库中存放函数的名称和对应的目标代码,以及连接过程中所需的重定位信息。用户也可以根据自己的需要建立自己的用户函数库。
库函数:存放在函数库中的函数。库函数具有明确的功能、入口调用参数和返回值。
连接程序:将编译程序生成的目标文件连接在一起生成一个可执行文件。
头文件:有时也称为包含文件。C语言库函数与用户程序之间进行信息通信时要使用的数据和变量,在使用某一库函数时,都要在程序中嵌入(用#include)该函数对应的头文件。
由于C语言编译系统应提供的函数库尚无国际标准。不同版本的C语言具有不同的库函数,用户使用时应查阅有关版本的C的库函数参考手册。我们以Turbo C为例简介一下C的库函数,并附录中给出了Turbo C的部分常用库函数。
㈧ 如何学习C语言的库函数
1. 最好是先看看标准文档,在cppreference.com这网站也能查看,打开文档里面有各标准库函数、宏的列表以及详细介绍,C和C++的都有,如英文读不懂的话可以选择中文版。内置的系列函数,查找文档就一目了然,还有非常详尽的参数、功能、返回值等说明以及各种格式化说明符的用法。
2. 对于初学C语言底层实现并不需要深入了解,如想了解的话可以研究一下glibc之类的实现。
㈨ c语言常用库函数有哪些
C语言的标准库函数有数百个,分布在不同的库文件中,目前绝大多数系统和程序肯定兼容的是C99标准,但2011年已经发布了更新的版本,有些遗留系统不一定支持最新的特性。
不同函数应用场合不一样,说不说哪些更常用,就看你所做工作的性质了。
通常来说,至少在基础编程时,stdio中的输入输出(可能是控制台的、也可能是文件的)、stdlib中的各种通用工具(如分配堆内存)、string中的字符串处理、time中的日期时间处理、math中的数学函数都算是比较常用的。
㈩ C语言库函数的相关概念
函数名:abort
功 能:异常终止一个进程
函数与形参类型:
void abort(void);
程序例:
#include <stdio.h>
#include <stdlib.h> int main(void)
{
printf(Calling abort()
);
abort();
return 0; /* This is never reached */
} 函数名:abs
功 能:计算整数num的值。返回整数num的绝对值
函数与参数类型:
int abs(num)
int num;
程序例:
#include <stdio.h>
#include <math.h> int main(void)
{
int number = -1234; printf(number: %d absolute value: %d
, number, abs(number));
return 0;
} 函数名: absread, abswirte
功 能:绝对磁盘扇区读、写数据
函数与形参类型:
int absread(int drive, int nsects, int sectno, void *buffer);
int abswrite(int drive, int nsects, in tsectno, void *buffer);
程序例:
/* absread example */ #include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dos.h> int main(void)
{
int i, strt, ch_out, sector;
char buf[512]; printf(Insert a diskette into drive A and press any key
);
getch();
sector = 0;
if (absread(0, 1, sector, &buf) != 0)
{
perror(Disk problem);
exit(1);
}
printf(Read OK
);
strt = 3;
for (i=0; i<80; i++)
{
ch_out = buf[strt+i];
putchar(ch_out);
}
printf(
);
return(0);
} 函数名:access
功 能:确定文件的访问权限
函数与形参类型:
int access(const char *filename, int amode);
程序例:
#include <stdio.h>
#include <io.h> int file_exists(char *filename); int main(void)
{
printf(Does NOTEXIST.FIL exist: %s
,
file_exists(NOTEXISTS.FIL) ? YES : NO);
return 0;
} int file_exists(char *filename)
{
return (access(filename, 0) == 0);
} 函数名: acos
功 能:计算并返回arccos(x)值、要求-1<=X<=1
函数与形参类型:
double acos(x)
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
, x, result);
return 0;
} 函数名:allocmem
功 能:分配DOS存储段
函数与形参类型:
int allocmem(unsigned size, 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 == -1)
printf(Allocated memory at segment: %x
, segp);
else
printf(Failed: maximum number of paragraphs available is %u
,
stat); return 0;
} 函数名:arc
功 能:画一弧线
函数与形参类型:
void far arc(int x, int y, int stangle, int endangle, int radius);
程序例:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100; /* initialize graphics and local variables */
initgraph(&gdriver, &gmode, ); /* read result of initialization */
errorcode = graphresult(); /* an error occurred */
if (errorcode != grOk)
{
printf(Graphics error: %s
, grapherrormsg(errorcode));
printf(Press any key to halt:);
getch(); exit(1); /* terminate with an error code */
} midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor()); /* draw arc */
arc(midx, midy, stangle, endangle, radius); /* clean up */
getch();
closegraph();
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
, str); return 0;
} 函数名::asin
功 能::计算并返回arcsin(x)值、要求-1<=X<=1
函数与形参类型:
double asin(x)
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
, x, result);
return(0);
} 函数名: assert
功 能:测试一个条件并可能使程序终止
函数与形参类型:
void assert(int test);
程序例:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h> struct ITEM {
int key;
int value;
}; /* add item to list, make sure list is not null */
void additem(struct ITEM *itemptr) {
assert(itemptr != NULL);
/* add item to list */
} int main(void)
{
additem(NULL);
return 0;
} 函数名:atan
功 能:计算并返回arctan(x)的值
函数与形参类型:
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
, x, result);
return(0);
} 函数名: atan2
功 能:计算并返回arctan(x/y)值
函数与形参类型:
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
, (y / x), result);
return 0;
} 函数名: atexit
功 能:注册终止函数
函数与形参类型:
int atexit(atexit_t func);
程序例:
#include <stdio.h>
#include <stdlib.h> void exit_fn1(void)
{
printf(Exit function #1 called
);
} void exit_fn2(void)
{
printf(Exit function #2 called
);
} int main(void)
{
/* post exit function #1 */
atexit(exit_fn1);
/* post exit function #2 */
atexit(exit_fn2);
return 0;
} 函数名: atof
功 能:把str指向的ASCⅡ字符串转换成一个double型整数返回双精度的结果
函数与形参类型:
double atof(str)
char*str;
程序例:
#include <stdlib.h>
#include <stdio.h> int main(void)
{
float f;
char *str = 12345.67; f = atof(str);
printf(string = %s float = %f
, str, f);
return 0;
} 函数名:atoi
功 能:
把str指向的ASCⅡz字符串转换成一个整数。返回整数结果
函数与参数类型:
double atoi(str )
char *str;
程序例:
#include <stdlib.h>
#include <stdio.h> int main(void)
{
int n;
char *str = 12345.67; n = atoi(str);
printf(string = %s integer = %d
, str, n);
return 0;
} 函数名:atol
功 能:
把字符串转换成长整型数 。返回长整数结果
函数与参数类型:
long atol(str )
char *str;
程序例:
#include <stdlib.h>
#include <stdio.h> int main(void)
{
long l;
char *str = 98765432; l = atol(lstr);
printf(string = %s integer = %ld
, str, l);
return(0);
} 函数名:mkdir
功 能:建立一个目录
用 法:
int mkdir(char *pathname);
程序例:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dir.h>
int main(void)
{
int status;
clrscr();
status = mkdir(asdfjklm);
(!status) ? (printf(Directory created
)) :
(printf(Unable to create directory
));
getch();
system(dir);
getch();
status = rmdir(asdfjklm);
(!status) ? (printf(Directory deleted
)) :
(perror(Unable to delete directory));
return 0;
} 函数名: mktemp
功 能:建立唯一的文件名
用 法:
char *mktemp(char *template);
程序例:
#include <dir.h>
#include <stdio.h>
int main(void)
{
/* fname defines the template for the
temporary file. */
char *fname = TXXXXXX, *ptr;
ptr = mktemp(fname);
printf(%s
,ptr);
return 0;
} 函数名: MK_FP
功 能:设置一个远指针
用 法:
void far *MK_FP(unsigned seg, unsigned off);
程序例:
#include <dos.h>
#include <graphics.h>
int main(void)
{
int gd, gm, i;
unsigned int far *screen;
detectgraph(&gd, &gm);
if (gd == HERCMONO)
screen = MK_FP(0xB000, 0);
else
screen = MK_FP(0xB800, 0);
for (i=0; i<26; i++)
screen[i] = 0x0700 + ('a' + i);
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
,
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);
} 函数名: moverel
功 能:将当前位置(CP)移动一相对距离
用 法:
void far moverel(int dx, int dy);
程序例:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, );
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf(Graphics error: %s
, grapherrormsg(errorcode));
printf(Press any key to halt:);
getch();
exit(1); /* terminate with an error code */
}
/* move the C.P. to location (20, 30) */
moveto(20, 30);
/* plot a pixel at the C.P. */
putpixel(getx(), gety(), getmaxcolor());
/* create and output a message at (20, 30) */
sprintf(msg, (%d, %d), getx(), gety());
outtextxy(20, 30, msg);
/* move to a point a relative distance */
/* away from the current value of C.P. */
moverel(100, 100);
/* plot a pixel at the C.P. */
putpixel(getx(), gety(), getmaxcolor());
/* create and output a message at C.P. */
sprintf(msg, (%d, %d), getx(), gety());
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
} 函数名: movetext
功 能:将屏幕文本从一个矩形区域拷贝到另一个矩形区域
用 法:
int movetext(int left, int top, int right, int bottom,
int newleft, int newtop);
程序例:
#include <conio.h>
#include <string.h>
int main(void)
{
char *str = This is a test string;
clrscr();
cputs(str);
getch();
movetext(1, 1, strlen(str), 2, 10, 10);
getch();
return 0;
} 函数名: moveto
功 能:将CP移到(x, y)
用 法:
void far moveto(int x, int y);
程序例:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, );
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf(Graphics error: %s
, grapherrormsg(errorcode));
printf(Press any key to halt:);
getch();
exit(1); /* terminate with an error code */
}
/* move the C.P. to location (20, 30) */
moveto(20, 30);
/* plot a pixel at the C.P. */
putpixel(getx(), gety(), getmaxcolor());
/* create and output a message at (20, 30) */
sprintf(msg, (%d, %d), getx(), gety());
outtextxy(20, 30, msg);
/* move to (100, 100) */
moveto(100, 100);
/* plot a pixel at the C.P. */
putpixel(getx(), gety(), getmaxcolor());
/* create and output a message at C.P. */
sprintf(msg, (%d, %d), getx(), gety());
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
} 函数名: movemem
功 能:移动一块字节
用 法:
void movemem(void *source, void *destin, unsigned len);
程序例:
#include <mem.h>
#include <alloc.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char *source = Borland International;
char *destination;
int length;
length = strlen(source);
destination = malloc(length + 1);
movmem(source,destination,length);
printf(%s
,destination);
return 0;
} 函数名: normvideo
功 能:选择正常亮度字符
用 法:
void normvideo(void);
程序例:
#include <conio.h>
int main(void)
{
normvideo();
cprintf(NORMAL Intensity Text
);
return 0;
} 函数名: nosound
功 能:关闭PC扬声器
用 法:
void nosound(void);
程序例:
/* Emits a 7-Hz tone for 10 seconds.
True story: 7 Hz is the resonant frequency of a chicken's skull cavity.
This was determined empirically in Australia, where a new factory
generating 7-Hz tones was located too close to a chicken ranch:
When the factory started up, all the chickens died.
Your PC may not be able to emit a 7-Hz tone.
*/
int main(void)
{
sound(7);
delay(10000);
nosound();
}