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();
}