主函数传参&输出缓冲区&注册退出函数

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);
}

 

#include &lt;iostream&gt; // 引入输入输出流库 #include &lt;string&gt; // 支持字符串类型(备用) #include &lt;cstring&gt; // 提供C字符串函数支持,如strlen等 using namespace std; // 使用标准命名空间,简化std::调用 // 函数声明:统计字符在字符串中出现的次数并记录位置 // 参数:str - 被查找的字符串(C风格),xCh - 待查找字符,positions - 存储位置的整型数组指针 // 返回值:xCh在str中出现的次数 int countChar(const char* str, char xCh, int* positions); int main() { const int MAX_LEN = 1000; // 定义字符串最大长度 const int MAX_POS = 1000; // 定义位置数组最大容量(防止越界) char str[MAX_LEN]; // 字符数组,用于存储用户输入的字符串 char xCh; // 存储待查找的字符 int positions[MAX_POS]; // 整型数组,用于保存字符出现的所有位置索引 char choice; // 用户选择是否继续查询 // 使用do-while循环确保至少执行一次查找操作 do { // 提示用户输入字符串 cout &lt;&lt; &quot;请输入要查找的字符串: &quot;; cin.getline(str, MAX_LEN); // 安全读取一行,避免缓冲区溢出 // 提示用户输入待查找的字符 cout &lt;&lt; &quot;请输入要查找的字符: &quot;; cin &gt;&gt; xCh; // 读取单个字符 cin.ignore(); // 清除输入缓冲区中的换行符,防止干扰下一次getline // 调用countChar函数,获取字符出现的次数及位置 int count = countChar(str, xCh, positions); // 根据查找结果输出相应信息 if (count == 0) { // 若未找到该字符,给出提示 cout &lt;&lt; &quot;字符 &#39;&quot; &lt;&lt; xCh &lt;&lt; &quot;&#39; 在字符串中没有出现。&quot; &lt;&lt; endl; } else { // 找到字符时,输出出现次数具体位置 cout &lt;&lt; &quot;字符 &#39;&quot; &lt;&lt; xCh &lt;&lt; &quot;&#39; 出现了 &quot; &lt;&lt; count &lt;&lt; &quot; 次,位置分别为: &quot;; for (int i = 0; i &lt; count; ++i) { cout &lt;&lt; positions[i]; // 输出位置索引 if (i &lt; count - 1) cout &lt;&lt; &quot;, &quot;; // 多个位置之间用逗号分隔 } cout &lt;&lt; endl; } // 询问用户是否继续查找 cout &lt;&lt; &quot;是否继续查找?(y/n): &quot;; cin &gt;&gt; choice; // 读取用户选择 cin.ignore(); // 再次清除换行符,保持输入流干净 } while (choice == &#39;y&#39; || choice == &#39;Y&#39;); // 当用户输入&#39;y&#39;或&#39;Y&#39;时继续循环 return 0; // 程序正常结束 } // 函数定义:countChar // 功能:遍历字符串str,查找所有等于xCh的字符,记录其索引到positions数组中 // 参数说明同上 // 返回值:匹配成功的总次数 int countChar(const char* str, char xCh, int* positions) { int count = 0; // 初始化计数器为0 int index = 0; // 当前遍历的字符索引 // 遍历字符串直到遇到终止符&#39;\0&#39; while (str[index] != &#39;\0&#39;) { if (str[index] == xCh) { // 判断当前字符是否为目标字符 positions[count] = index; // 是则将当前位置存入positions数组 ++count; // 计数器加1 } ++index; // 移动到下一个字符 } return count; // 返回总共出现的次数 }
最新发布
12-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值