函数名: abs 功  能: 求整数的绝对值
用 法: int abs(int i);
程序例:
#include <stdio.h>
#include <math.h> int main(void)
{
int number = -; printf("number: %d absolute value: %d/n", number, abs(number));
return ;
}
函数名: atof
功 能: 把字符串转换成浮点数
用 法: double atof(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h> int main(void)
{
float f;
char *str = "12345.67"; f = atof(str);
printf("string = %s float = %f/n", str, f);
return ;
} 函数名: atoi
功 能: 把字符串转换成长整型数
用 法: int atoi(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h> int main(void)
{
int n;
char *str = "12345.67"; n = atoi(str);
printf("string = %s integer = %d/n", str, n);
return ;
} 函数名: atol
功 能: 把字符串转换成长整型数
用 法: long atol(const char *nptr);
程序例: #include <stdlib.h>
#include <stdio.h> int main(void)
{
long l;
char *str = ""; l = atol(lstr);
printf("string = %s integer = %ld/n", str, l);
return();
} 函数名: bsearch
功 能: 二分法搜索
用 法: void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *));
程序例: #include <stdlib.h>
#include <stdio.h> #define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) int numarray[] = {, , , , , }; int numeric (const int *p1, const int *p2)
{
return(*p1 - *p2);
} int lookup(int key)
{
int *itemptr; /* The cast of (int(*)(const void *,const void*))
is needed to avoid a type mismatch error at
compile time */
itemptr = bsearch (&key, numarray, NELEMS(numarray),
sizeof(int), (int(*)(const void *,const void *))numeric);
return (itemptr != NULL);
} int main(void)
{
if (lookup())
printf("512 is in the table./n");
else
printf("512 isn't in the table./n"); return ;
}
函数名: 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 ;
} 函数名: div 功 能: 将两个整数相除, 返回商和余数
用 法: div_t (int number, int denom);
程序例: #include <stdlib.h>
#include <stdio.h> div_t x; int main(void)
{
x = div(,);
printf("10 div 3 = %d remainder %d/n", x.quot, x.rem); return ;
}
函数名: 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 = ; 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= ;
string = ecvt(value,ndig,&dec,&sign);
printf("string = %s dec = %d sign = %d/n",
string, dec, sign); value = 0.6789e5; /* scientific
notation */
ndig = ;
string = ecvt(value,ndig,&dec,&sign);
printf("string = %s dec = %d/
sign = %d/n", string, dec, sign); return ;
} 函数名: exp 功 能: 指数函数
用 法: 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 ;
} 函数名: 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 ;
} 函数名: 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 = ; 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= ;
string = ecvt(value,ndig,&dec,&sign);
printf("string = %s dec = %d sign = %d/n",
string, dec, sign); value = 0.6789e5; /* scientific
notation */
ndig = ;
string = ecvt(value,ndig,&dec,&sign);
printf("string = %s dec = %d/
sign = %d/n", string, dec, sign); return ;
} 函数名: 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 ;
} 函数名: fmod
功 能: 计算x对y的模, 即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 ;
}
函数名: gcvt
功 能: 把浮点数转换成字符串
用 法: char *gcvt(double value, int ndigit, char *buf);
程序例: #include <stdlib.h>
#include <stdio.h> int main(void)
{
char str[];
double num;
int sig = ; /* 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();
}
函数名: itoa
功 能: 把一整数转换为字符串
用 法: char *itoa(int value, char *string, int radix);
程序例: #include <stdlib.h>
#include <stdio.h> int main(void)
{
int number = ;
char string[]; itoa(number, string, );
printf("integer = %d string = %s/n", number, string);
return ;
} 函数名: 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 ;
} 函数名: ldexp
功 能: 计算value*2的幂
用 法: double ldexp(double value, int exp);
程序例: #include <stdio.h>
#include <math.h> int main(void)
{
double value;
double x = ; /* ldexp raises 2 by a power of 3
then multiplies the result by 2 */
value = ldexp(x,);
printf("The ldexp value is: %lf/n",
value); return ;
} 函数名: 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 ;
} 函数名: 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( *x - *y );
} int main(void)
{
int array[] = {, , , , };
size_t nelem = ;
int key;
int *result; key = ;
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 ;
} 函数名: log 功 能: 对数函数ln(x)
用 法: double log(double x);
程序例: #include <math.h>
#include <stdio.h> int main(void)
{
double result;
double x = 8.6872; result = log(x);
printf("The natural log of %lf is %lf/n", x, result); return ;
} 函数名: 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 ;
}
函数名: 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( *x - *y );
} int main(void)
{
int array[] = {, , , , };
size_t nelem = ;
int key;
int *result; key = ;
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 ;
} 函数名: 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 ;
}
函数名: 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) - );
printf("Buffer after memset: %s/n", buffer);
return ;
}
函数名: pow 功 能: 指数函数(x的y次方)
用 法: 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 ;
} 函数名: pow10
功 能: 指数函数(10的p次方)
用 法: 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 ;
} 函数名: 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[][] = { "cat", "car", "cab", "cap", "can" }; int main(void)
{
int x; qsort((void *)list, , sizeof(list[]), sort_function);
for (x = ; x < ; x++)
printf("%s/n", list[x]);
return ;
} int sort_function( const void *a, const void *b)
{
return( strcmp(a,b) );
} 函数名: 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 ;
}
函数名: sscanf
功 能: 执行从字符串中的格式化输入
用 法: int sscanf(char *string, char *format[,argument,...]);
程序例: #include <stdio.h>
#include <conio.h> int main(void)
{
char label[];
char name[];
int entries = ;
int loop, age;
double salary; struct Entry_struct
{
char name[];
int age;
float salary;
} entry[]; /* 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=;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=;loop<entries;++loop)
printf("%4d | %-20s | %5d | %15.2lf/n",
loop + ,
entry[loop].name,
entry[loop].age,
entry[loop].salary);
printf("-----------------------------------------------------/n");
return ;
}
函数名: stpcpy
功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char *source);
程序例: #include <stdio.h>
#include <string.h> int main(void)
{
char string[];
char *str1 = "abcdefghi"; stpcpy(string, str1);
printf("%s/n", string);
return ;
} 函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
程序例: #include <string.h>
#include <stdio.h> int main(void)
{
char destination[];
char *blank = " ", *c = "C++", *Borland = "Borland"; strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c); printf("%s/n", destination);
return ;
} 函数名: strchr
功 能: 在一个串中查找给定字符的第一个匹配之处/
用 法: char *strchr(char *str, char c);
程序例: #include <string.h>
#include <stdio.h> int main(void)
{
char string[];
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 ;
} 函数名: 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 > )
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 > )
printf("buffer 2 is greater than buffer 3/n");
else
printf("buffer 2 is less than buffer 3/n"); return ;
}
函数名: strcpy
功 能: 串拷贝 用 法: char *strcpy(char *str1, char *str2);
程序例: #include <stdio.h>
#include <string.h> int main(void)
{
char string[];
char *str1 = "abcdefghi"; strcpy(string, str1);
printf("%s/n", string);
return ;
} 函数名: 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 ;
}
函数名: strset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strset(char *str, char c);
程序例: #include <stdio.h>
#include <string.h> int main(void)
{
char string[] = "";
char symbol = 'c'; printf("Before strset(): %s/n", string);
strset(string, symbol);
printf("After strset(): %s/n", string);
return ;
} 函数名: 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 ;
} 函数名: strtod
功 能: 将字符串转换为double型值
用 法: double strtod(char *str, char **endptr);
程序例: #include <stdio.h>
#include <stdlib.h> int main(void)
{
char input[], *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 ;
}
函数名: strtol
功 能: 将串转换为长整数
用 法: long strtol(char *str, char **endptr, int base);
程序例: #include <stdlib.h>
#include <stdio.h> int main(void)
{
char *string = "", *endptr;
long lnumber; /* strtol converts string to long integer */
lnumber = strtol(string, &endptr, );
printf("string = %s long = %ld/n", string, lnumber); return ;
} 函数名: 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 ;
} 函数名: swab
功 能: 交换字节
用 法: void swab (char *from, char *to, int nbytes);
程序例: #include <stdlib.h>
#include <stdio.h>
#include <string.h> char source[] = "rFna koBlrna d";
char target[]; int main(void)
{
swab(source, target, strlen(source));
printf("This is target: %s/n", target);
return ;
} 函数名: 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=; i<length; i++)
{
string[i] = tolower(string[i]);
}
printf("%s/n",string); return ;
} 函数名: 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=; i<length; i++)
{
string[i] = toupper(string[i]);
} printf("%s/n",string); return ;
}

ACM实用C语言函数的更多相关文章

  1. R语言函数化编程笔记1

    R语言函数化编程笔记1 notes:有一个不错的网站叫做stack overflow,有问题可以从上面找或者搜索答案,会有大佬相助. 在github上面可以找到很多R的扩展包,如果自己额修改被接受,那 ...

  2. 从linux0.11中起动部分代码看汇编调用c语言函数

    上一篇分析了c语言的函数调用栈情况,知道了c语言的函数调用机制后,我们来看一下,linux0.11中起动部分的代码是如何从汇编跳入c语言函数的.在LINUX 0.11中的head.s文件中会看到如下一 ...

  3. C语言(函数)学习之strstr strcasestr

    C语言(函数)学习之[strstr]&[strcasestr]一.strstr函数使用[1]函数原型char*strstr(constchar*haystack,constchar*needl ...

  4. C语言函数sscanf()的用法

    从文件读取数据是一件很麻烦的事,所幸有sscanf()函数. C语言函数sscanf()的用法 sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: int sscanf( st ...

  5. 不可或缺 Windows Native (6) - C 语言: 函数

    [源码下载] 不可或缺 Windows Native (6) - C 语言: 函数 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h # ...

  6. C#委托与C语言函数指针及函数指针数组

    C#委托与C语言函数指针及函数指针数组 在使用C#时总会为委托而感到疑惑,但现在总新温习了一遍C语言后,才真正理解的委托. 其实委托就类似于C/C++里的函数指针,在函数传参时传递的是函数指针,在调用 ...

  7. swift1.2语言函数和闭包函数介绍

    swift1.2语言函数和闭包函数介绍 在编程中,随着处理问题的越来越复杂,代码量飞速增加.其中,大量的代码往往相互重复或者近似重复.如果不采有效方式加以解决,代码将很难维护. swift1.2语言函 ...

  8. Swift 1.1语言函数参数的特殊情况本地参数名外部参数名

    Swift 1.1语言函数参数的特殊情况本地参数名外部参数名 7.4  函数参数的特殊情况 声明定义有参函数时,为函数的每一个参数都定义了参数名称.根据参数名定义的形式不同,函数参数包括本地参数和外部 ...

  9. 10个不太为人所知的,但实用的PHP函数

    10个不太为人所知的,但实用的PHP函数 您的评价:       较差  收藏该经验     阅读目录 php_check_syntax highlight_string show_source ph ...

随机推荐

  1. fzu 2039 Pets (简单二分图 + (最大流 || 二分图))

    Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There ar ...

  2. linux程序设计——网络信息(第十五章)

    15.3    网络信息 当眼下为止,客户和server程序一直是吧地址和port号编译到它们自己的内部. 对于一个更通用的server和客户程序来说.能够通过网络信息函数来决定应该使用的地址和por ...

  3. EasyNVR NVR网页无插件直播在兼容宇视NVR RTSP流媒体时PLAY过程对Scale的兼容

    前一段在维护EasyNVR客户的过程中遇到一个问题,在接入宇视NVR的时候,就是明明在vlc中能非常正常播放的视频流,却用EasyRTSPClient RTSP客户端拉流的协议交互过程中,PLAY命令 ...

  4. springmvc的过滤器和拦截器

    1 什么是过滤器 过滤器是过滤数据,比如过滤低俗文字,修改字符编码等. 2 什么是拦截器 拦截器中可以用来向ModelAndView中添加通用的数据.这样的好处是对于所有网页的公用部分就不需要在每个c ...

  5. 题解 P3389 【【模板】高斯消元法】

    题解 P3389 [[模板]高斯消元法] 看到大家都没有重载运算符,那我就重载一下运算符给大家娱乐一下 我使用的是高斯-约旦消元法,这种方法是精度最高的(相对地) 一句话解释高斯约旦消元法: 通过加减 ...

  6. ios之编码规范具体说明

    iOS代码规范: 所有代码规范所有遵循苹果sdk的原则,不清楚的请訪问苹果SDK文档或下载官方Demo查看. 1.project部分: 将项目中每一个功能模块相应的源文件放入同一目录下,使用虚拟目录. ...

  7. JVM GC调优一则--增大Eden Space提高性能

    版权声明:本文为横云断岭原创文章,未经博主同意不得转载.微信公众号:横云断岭的专栏 https://blog.csdn.net/hengyunabc/article/details/24924843 ...

  8. Java for LeetCode 096 Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. linux下编译安装python

    从官网下载指定的源码包 https://www.python.org/downloads/source/ 把源码文件以二进制方式上传到linux服务器 安装python需要用到gcc工具,首先查看gc ...

  10. 通过崩溃trace来查找问题原因

    从友盟中, 我们可能会得到如下信息: Application received signal SIGSEGV (null) ( 0 CoreFoundation 0x359348a7 __except ...