函数:
一段具有某项功能的代码,是C语言中管理代码的单位。
把代码封装成一个个函数,可以方便的管理和调用代码。
函数分类:
标准库函数:C语言标准为委员会为C语言以函数形式提供的一些基础功能,被封装在libc.so库中,使用时需要包含头文件,函数名(参数)即可以调用。
int isalnum(int c);
功能:当以是数字、字母字符返回真
int isalpha(int c);
功能:当c是字母时返回真
int isdigit(int c);
功能:当c是数字字符时返回真
int islower(int c);
功能:当c是小写字母时返回真
int isupper(int c);
功能:当c是大写字母时返回真
int abs(int num);
功能:返回num的绝对值
以下函数被封装在libm.so库文件中。
double sqrt(double x);
功能:返回x的平方根
double pow(double x, double y);
功能:返回x的y次方
double floor(double arg);
功能:返回小于等于arg的最大整数
double ceil( double num );
功能:返回大于等于num的最小整数
double fabs(double arg);
功能:返回arg的给对值
void srand(unsigned seed);
功能:设置随机种子
int rand(void);
功能:返回一个随机数rand()%(b-a)+a
time_t time( time_t *time);
功能:返回自19701月1日 00:00:00 到当前时间过了多少秒。
int system(const char *command);
功能:调用系统命令
练习4:获取10个[100,1001)的随机数。
for(int i=0;i<10;i++)
{
srand(time(NULL));
rand()%(1001-100)+100; //相当于[100,1000]
}
练习5:随机出一组双色球彩票号。
6红:1~33
1蓝:1~16
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc,const char* argv[])
{
time_t t;
time(&t);
srand(time(NULL));
printf("红:");
for(int i=0; i<6; i++)
{
printf("%d ",rand()%(33-1+1)+1);
}
printf("\n蓝:");
printf("%d ",rand(