int main(int argc , char *argv[] , char *envp[]);
argc:传递的参数列表中参数的个数
argv:传递的参数列表
envp:环境变量
默认会给主函数传递一个参数:执行的命令
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
int main(int argc,char *argv[],char *envp[] )
{
int i=0;
for(;i<argc;i++)
{
printf("%s\n",argv[i]);
}
exit(0);
}
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
int main(int argc,char *argv[],char *envp[] )
{
int i=0;
for(;i<argc;i++)
{
printf("%s\n",argv[i]);
}
i=0;
while(envp[i] != NULL)
{
printf("%s\n",envp[i]);
i++;
}
exit(0);
}
scanf gets 从输入缓冲区中获取
printf puts 从输出缓冲区获取
#include<stdio.h>//本程序先睡眠5秒再打印输出helloworld
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
int main( )
{
printf("hello");
sleep(5);
printf("world\n");
exit(0);
}
输出缓冲区刷新的条件(从缓冲区刷到界面上)
1、程序结束
2、遇到“\n”
3、缓冲区满 缓冲区系统默认1024个字节
4、主动刷新 fflush(stdou)函数
操作系统通过I/O设备将字符输出,加入缓冲区回提高执行效率
_exit(0)直接将缓冲区中的东西释放,不会刷新缓冲区(从缓冲区刷到界面上)
int atexit(void(*fun) ()); 注册一个退出函数 void(*fun)是一个函数指针
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
void fun1()
{
printf("i am fun1\n");
}
void fun2()
{
printf("i am fun2\n");
}
int main( )
{
atexit(fun1);
atexit(fun2);
printf("hello\n");
sleep(3);
printf("world\n");
exit(0);
}