函数:
函数作用---模块化,代码重用,使用方便(printf)
函数定义(函数设计)
存储类型 数据类型 函数名(参数列表){函数体}
void show_picture(char *path)
{
显示图片;
}
函数参数
形式参数--函数
实际参数-函数被调用的时候传入的参数(初始化形式参数)
参数传递(传值,地址)
main函数传递参数
int main(int argc,char **argv);
int 返回值
int argc---程序运行时候传入参数个数(包括程序本身)
char **argv --程序运行时候传入参数列表
函数返回值(gcc---函数必须自己声明,gcc声明把所有函数的返回值设为int)
(多文件编程)mainpram.c math.c
gcc -o mainpram mainpram.c math.c
函数调用-中断(压栈)
函数递归-函数自己调用自己(必须有判断返回)
函数指针
库函数,字符串库函数,时间库函数
字符串库函数:
比较 strcmp strcmp(“hello”,“hello”)==0
拷贝 strcpy char name[32];strcpy(name,“had”);
字符串长度计算(不包含\0)strlen strlen(name);——字符串 “A”==‘A’‘\0’
字符串查找子串 strstr ——“google” “oo” strstr(“google”,“oo”) 如果找到就返回oo所在的位置,如果没找到就返回NULL
字符串拼接 strcat player /Users/geek-ios107/Desktop/46.mp4
char path[128]=“/Users/geek-ios107/Desktop/46.mp4”
strcat(path,“/”);—》”Users/geek-ios107/Desktop/“
strcat(path,“代号47.mp4”);—>/Users/gec-ios107/Desktop/代号47.mp4
man 函数名/命令-----函数使用说明
时间库函数(随机数)
获取当前时间
#include<time.h>
time_t time(time_t *tloc);
把时间转换字符串输出
char *ctime(const time_t *clock);
把秒时间转换为时间结构
struct tm*gmtime(const time_t *clock);世界时间
int tm_sec; /* seconds (0 - 60) */
int tm_min; /* minutes (0 - 59) */
int tm_hour; /* hours (0 - 23) */
int tm_mday; /* day of month (1 - 31) */
int tm_mon; /* month of year (0 - 11) */
int tm_year; /* year - 1900 */
int tm_wday; /* day of week (Sunday = 0) */
int tm_yday; /* day of year (0 - 365) */
int tm_isdst; /* is summer time in effect? */
char *tm_zone; /* abbreviation of timezone name */
long tm_gmtoff; /* offset from UTC in seconds */
获取随机数
#include<stdlib.h>
long random(void);
//设置随机数因子
void srandom(unsigned seed);
获取微秒时间
int gettimeofday(struct timeval,*restrict tp,void *restrict tzp);
#include <stdio.h>
int main(void)
{
printf("请输入行数:");
int row = 0;
scanf("%d", &row);
int i=0;
int j=0;
for(i=0; i<row; i++)
{
for(j=0; j<row-i-1; j++) {printf(" ");}
for(j=i; j>0; j--) {printf("%d",j+1);}
for(j=0; j<=i; j++) {printf("%d", j+1);}
printf("\n");
}
return 0;
}
#include "test3.h"
int main(void)
{
//平年,闰年 能被4整数且不能被100整除,能被400整除
//每月的天数31, 28/29,31,30,31,30,31,31,30,31,30,31
//2015-10-28
//2015-3-3;
int year=0, month=0, day=0;
scanf("%d-%d-%d", &year, &month, &day);
int allday = 0;
switch (month) {
case 12:allday += 30;
case 11:allday += 31;
case 10:allday += 30;
case 9:allday += 31;
case 8:allday += 31;
case 7:allday += 30;
case 6:allday += 31;
case 5:allday += 30;
case 4:allday += 31;
case 3:allday += 28;
case 2:allday += 31;
default: allday += day;
break;
}
if(month >= 3)
{
if((year%4==0 && year%100 !=0 ) || year%400 == 0)
{
allday += 1;
}
}
printf("%d\n", allday);
return 0;
}
#include "math.h"
int add(int a, int b)
{
return a+b;
}
double sub(double a, double b)
{
return a-b;
}
//函数递归---函数自己调用自己
//0--i的累加
int fun(int i)
{
if(i == 0) return 0;
return i+fun(i-1);
}
//菲
//1 1 2 3 5 8 13 21 34 ---i
int fdat(int i)
{
if(i==1 || i==2) return 1;
return fdat(i-1) + fdat(i-2);
}
#include <stdio.h>
#include<iostream.h>
#include <time.h>
#include <stdlib.h>
int main(void)
{
time_t t;
time(&t);
printf("%ld\n", t);
//把时间秒转换为日期
printf("%s\n", ctime(&t));
struct tm *timet = gmtime(&t);
printf("%d:%d:%d\n", timet->tm_hour, timet->tm_min, timet->tm_sec);
//取随机数
struct timeval tval;
gettimeofday(&tval, NULL);
srandom(tval.tv_usec);
printf("%ld\n", random()%100);
gettimeofday(&tval, NULL);
srandom(tval.tv_usec);
printf("%ld\n", random()%100);
gettimeofday(&tval, NULL);
srandom(tval.tv_usec);
printf("%ld\n", random()%100);
return 0;
}