本文目录一览:
- c语言 ultoa 这个函数为什么不能调用 说没有定义 已经包含了stdlib.h 头文件
- C语言sort函数如何使用
- c语言bool函数怎么用
- c语言中,函数itoa有什么功能,怎么用
- c语言atoi与itoa用法以及超过long long int 型变量整数加减法
- 我需要c语言每个头文件里的所有函数介绍及用法!
c语言 ultoa 这个函数为什么不能调用 说没有定义 已经包含了stdlib.h 头文件
这个不是标准函数,如果是C++可以用 operator<<
和 operator>>
转,如果是C语言的话,试试 scanf
和 printf
。
C语言sort函数如何使用
C语言中没有预置的sort函数。如果在C语言中,遇到有调用sort函数,就是自定义的一个函数,功能一般用于排序。
可以编写自己的sort函数。
如下函数为将整型数组从小到大排序。
void sort(int *a, int l) // a为数组地址,l为数组长度。
{
int i, j;
int v;
// 排序主体
for (i = 0; i < l - 1; i++)
for (j = i + 1; j < l; j++)
{
if (a[i] > a[j]) // 如前面的比后面的大,则交换。
{
v = a[i];
a[i] = a[j];
a[j] = v;
}
}
}
对于这样的自定义sort函数,可以按照定义的规范来调用。
C语言有自有的qsort函数。
功能: 使用快速排序例程进行排序
头文件: stdlib.h
原型: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *, const void *));
参数:
- 待排序数组首地址
- 数组中待排序元素数量
- 各元素的占用空间大小
- 指向函数的指针,用于确定排序的顺序
这个函数必须要自己写比较函数,即使要排序的元素是int、float一类的C语言基础类型。 以下是qsort的一个例子:
#include <stdio.h>
#include <stdlib.h>
int comp(const void *a, const void *b) // 用来做比较的函数。
{
return *(int *)a - *(int *)b;
}
int main()
{
int a[10] = {2, 4, 1, 5, 5, 3, 7, 4, 1, 5}; // 乱序的数组。
int i;
qsort(a, 10, sizeof(int), comp); // 调用qsort排序
for (i = 0; i < 10; i++) // 输出排序后的数组
{
printf("%d\t", a[i]);
}
return 0;
}
扩展资料:sort函数的用法(C++排序库函数的调用)
对数组进行排序,在C++中有库函数帮我们实现,这们就不需要我们自己来编程进行排序了。
为什么要用C++标准库里的排序函数
Sort()函数是C一种排序方法之一,学会了这种方法也打消我学习C以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于快排的方法,时间复杂度为n*log2(n),执行效率较高!
C++标准库里的排序函数的使用方法
I)Sort函数包含在头文件为#include<algorithm>
的C++标准库中,调用标准库里的排序方法可以不必知道其内部是如何实现的,只要出现我们想要的结果即可!
II)Sort函数有三个参数:
- 第一个是要排序的数组的起始地址。
- 第二个是结束的地址(最后一位要排序的地址的下一地址)
- 第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序。 Sort函数使用模板: Sort(start, end, 排序方法) 下面就具体使用sort()函数结合对数组里的十个数进行排序做一个说明!
例一:sort函数没有第三个参数,实现的是从小到大
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[10] = {9, 6, 3, 8, 5, 2, 7, 4, 1, 0};
for (int i = 0; i < 10; i++)
cout << a[i] << endl;
sort(a, a + 11);
for (int i = 0; i < 10; i++)
cout << a[i] << endl;
return 0;
}
编译器
- GCC,GNU组织开发的开源免费的编译器
- MinGW,Windows操作系统下的GCC
- Clang,开源的BSD协议的基于LLVM的编译器
- Visual C++ :: cl.exe,Microsoft VC++自带的编译器
集成开发环境
- CodeBlocks,开源免费的C/C++ IDE
- CodeLite,开源、跨平台的C/C++集成开发环境
- Orwell Dev-C++,可移植的C/C++IDE
- C-Free
- Light Table
- Visual Studio系列
Hello World
参考资料:百度百科-sort函数
c语言bool函数怎么用
C语言中的bool函数是一种判断表达式真假的函数,它接受一个参数,参数可以是表达式、变量、常量等,并返回一个布尔值(true或false)来表示表达式的真假。
拓展
使用bool函数可以简化C语言程序的开发,在循环控制中,可以更方便地编写判断条件,使程序更加简洁、易读。网名:C语言小白。
拓展
C语言作为一门非常重要的编程语言,具有功能强大、易学易用的特点,是编写系统软件、驱动程序和应用软件的首选语言。学习C语言可以为更高级的编程语言打下基础,并且可以为深入理解计算机系统运行原理提供帮助。
c语言中,函数itoa有什么功能,怎么用
itoa是广泛应用的非标准C语言扩展函数。由于它不是标准C语言函数,所以不能在所有的编译器中使用。但是,大多数的编译器(如Windows上的)通常在头文件中包含这个函数。在C语言中与之有相反功能的函数是atoi。
功能
把一整数转换为字符串。
用法
char *itoa(int value, char *string, int radix);
头文件
#include <stdio.h>
#include <stdlib.h>
程序例
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number = 123456;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
}
实现itoa函数的源代码
char *myitoa(int num, char *str, int radix)
{
char index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
unsigned unum;
int i = 0, j, k;
if (radix == 10 && num < 0)
{
unum = (unsigned)-num;
str[i++] = '-';
}
else
unum = (unsigned)num;
do
{
str[i++] = index[unum % (unsigned)radix];
unum /= radix;
} while (unum);
str[i] = '\0';
if (str[0] == '-')
k = 1;
else
k = 0;
for (j = k; j <= (i - 1) / 2.0 + k; j++)
{
int num = str[j];
str[j] = str[i - j - 1 + k];
str[i - j - 1 + k] = num;
}
return str;
}
itoa的第三个参数用于将数字转换成不同的进制。举个例子:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int number = 12345;
char string[25];
itoa(number, string, 10); // 按十进制转换
printf("integer = %d string = %s\n", number, string);
itoa(number, string, 16); // 按16进制转换
printf("integer = %d string = %s\n", number, string);
return 0;
}
输出结果
integer = 12345 string = 12345
integer = 12345 string = 3039
但是要注意,itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。 用几进制表示吧:)
MSDN的例子
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;
_itoa(i, buffer, 10);
printf("String of integer %d (radix 10): %s\n", i, buffer);
_itoa(i, buffer, 16);
printf("String of integer %d (radix 16): 0x%s\n", i, buffer);
_itoa(i, buffer, 2);
printf("String of integer %d (radix 2): %s\n", i, buffer);
_ltoa(l, buffer, 16);
printf("String of long int %ld (radix 16): 0x%s\n", l, buffer);
_ultoa(ul, buffer, 16);
printf("String of unsigned long %lu (radix 16): 0x%s\n", ul, buffer);
}
Output
String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2
指定要转换的进制的基数,其值好象在1--36之间都可以。 这个不是C标准库中的函数,而是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似:
char str[255];
sprintf(str, "%x", 100); // 将100转为16进制表示的字符串。
c语言atoi与itoa用法以及超过long long int 型变量整数加减法
大整数的计算可以通过模运算来进行简化,也可以将其作为字符串读入,然后分段运算之后再进行拼接。
附:atoi和itoa的示范代码如下:
#include <stdlib.h>
#include <stdio.h>
void main(void)
{
char *s;
int ix;
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;
s = " -9885 pigs"; /* Test of atoi */
ix = atoi(s);
printf("atoi test: ASCII string: %s\t\tinteger: %d\n", s, ix);
_itoa(i, buffer, 10);
printf("String of integer %d (radix 10): %s\n", i, buffer);
_itoa(i, buffer, 16);
printf("String of integer %d (radix 16): 0x%s\n", i, buffer);
_itoa(i, buffer, 2);
printf("String of integer %d (radix 2): %s\n", i, buffer);
_ltoa(l, buffer, 16);
printf("String of long int %ld (radix 16): 0x%s\n", l, buffer);
_ultoa(ul, buffer, 16);
printf("String of unsigned long %lu (radix 16): 0x%s\n", ul, buffer);
}
输出结果:
atoi test: ASCII string: -9885 pigs integer: -9885
String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2
我需要c语言每个头文件里的所有函数介绍及用法!
分类函数,所在函数库为ctype.h
int isalpha(int ch)
:若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0int isalnum(int ch)
:若ch是字母('A'-'Z','a'-'z')或数字('0'-'9')返回非0值,否则返回0int isascii(int ch)
:若ch是字符(ASCII码中的0-127)返回非0值,否则返回0int iscntrl(int ch)
:若ch是作废字符(0x7F)或普通控制字符(0x00-0x1F)返回非0值,否则返回0int isdigit(int ch)
:若ch是数字('0'-'9')返回非0值,否则返回0int isgraph(int ch)
:若ch是可打印字符(不含空格)(0x21-0x7E)返回非0值,否则返回0int islower(int ch)
:若ch是小写字母('a'-'z')返回非0值,否则返回0int isprint(int ch)
:若ch是可打印字符(含空格)(0x20-0x7E)返回非0值,否则返回0int ispunct(int ch)
:若ch是标点字符(0x00-0x1F)返回非0值,否则返回0int isspace(int ch)
:若ch是空格(' ')、水平制表符('\t')、回车符('\r')、走纸换行('\f')、垂直制表符('\v')、换行符('\n')返回非0值,否则返回0int isupper(int ch)
:若ch是大写字母('A'-'Z')返回非0值,否则返回0int isxdigit(int ch)
:若ch是16进制数('0'-'9','A'-'F','a'-'f')返回非0值,否则返回0int 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*2^n中x的值,n存贮在eptr中double ldexp(double value, int exp)
:返回value*2^exp的值double log(double x)
:返回logex的值double log10(double x)
:返回log10x的值double pow(double x, double y)
:返回x^y的值double pow10(int p)
:返回10^p的值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为直角边的长度,z^2=x^2+y^2double 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转换成双精度数,并返回这个数,错误返回0int atoi(char *nptr)
:将字符串nptr转换成整型数,并返回这个数,错误返回0long atol(char *nptr)
:将字符串nptr转换成长整型数,并返回这个数,错误返回0double 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()
:返回浮点状态字
进程函数,所在函数库为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[])
传递给子程序。出错返回-1mode
为运行模式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转换成双精度数,并返回这个数,错误返回0int atoi(char *nptr)
:将字符串nptr转换成整型数,并返回这个数,错误返回0long atol(char *nptr)
:将字符串nptr转换成长整型数,并返回这个数,错误返回0double strtod(char *str, char **endptr)
:将字符串str转换成双精度数,并返回这个数long strtol(char *str, char **endptr, int base)
:将字符串str转换成长整型数,并返回这个数int toascii(int c)
:返回c相应的ASCIIint 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')
诊断函数,所在函数库为assert.h、math.h
void assert(int test)
:一个扩展成if语句那样的宏,如果test测试失败,就显示一个信息并异常终止程序,无返回值void perror(char *string)
:本函数将显示最近一次的错误信息,格式如下:字符串string:错误信息char *strerror(char *str)
:本函数返回最近一次的错误信息,格式如下:字符串str:错误信息int matherr(struct exception *e)
:用户修改数学错误返回信息函数(没有必要使用)double _matherr(_mexcep why, char *fun, double *arg1p, double *arg2p, double retval)
:用户修改数学错误返回信息函数(没有必要使用)