经典编程900例(c语言)(第十三篇)

本文集合了多个C语言编程实例,包括程序参数处理、环境变量读取、密码输入等功能,通过具体代码展示了如何实现这些常见任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例145:打印程序参数

#include <stdio.h>

int main (int argc, char **argv)
{
    while (*argv)
      printf ("%s\n", *argv++);

    return 0;
}

例146:程序终止时调用的函数atexit

#include <stdio.h>
#include <stdlib.h>

void first(void)
{
    printf("First function registered\n");
}

void second(void)
{
    printf("Second function registered\n");
}

int main(int argc, char const *argv[])
{
    printf("start main...\n");
    printf("end main...\n");

    // int atexit(void (*func)(void)) 
    // 当程序正常终止时,调用指定的函数func。
    atexit(first);
    atexit(second);

    // 程序运行结果
    // start main...
    // end main...
    // Second function registered
    // First function registered
    
    return 0;
}

例147:在音频接口输出(博主编译报错,似乎是老编译器)

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    char line[255];

    while (fgets(line, sizeof(line), stdin))
    {
        fputs(line, stdout);
        strcat(line, "\r");
        fputs(line, stdaux);
    }
    
    return 0;
}

例148:使用atoi函数

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int count;  
    int i;    

    // atoi将字符串转为整数
    count = atoi(argv[1]);

    for (i = 0; i < count; i++)
        putchar('7');

    return 0;
}

例149:计算输入字符个数包括换行

#include <stdio.h>

int main(int argc, char const *argv[])
{
    long character_count = 0; 

    getchar();

	// feof(stdin)到文件末尾就返回非0值 - windows文件结束符号(EOF)输入ctrl+z
    while (!feof(stdin))
    {
        getchar();
        character_count++;
    }

    printf("The number of redirected characters is %ld\n", character_count);

    return 0;
}

例150:计算程序参数个数

#include <stdio.h>

int main (int argc, char *argv[])
{
	// argc包含程序字符串,所以最小为1
    printf ("The number of command line entries is %d\n", argc); 

    return 0;
}

例151:打印系统环境变量

#include <stdio.h>

int main (int argc, char **argv, char **env)
{
    while (*env)
        printf("%s\n", *env++);

    return 0;
}

例152:打印环境变量(使用函数)

#include <stdlib.h>
#include <stdio.h>
#include <<direct.h>
// #include <dos.h> - gcc提示#warning "<dos.h> is obsolete; consider using <direct.h> instead."

int main(int argc, char const *argv[])
{
    int i;

    for (i = 0; environ[i]; i++)
        printf("%s\n", environ[i]);
    
    return 0;
}

例153:写一个隐式输入密码的函数

#include <stdio.h>
#include <conio.h>
#include <string.h>

#define BACKSPACE 8

char *get_password(const char *prompt)
{
    static char buffer[128];

    int i = 0;

    char letter = NULL;

    // 打印提示
    printf(prompt);

    while ((i < 127) && (letter != '\r'))
    {
        letter = getch();

        // 输入退格
        if (letter == BACKSPACE)
        {
            if (i > 0)
            {
                // 密码字符串“退格”
                buffer[--i] = NULL;

                // 光标退格
                putchar(BACKSPACE);
                putchar(' ');
                putchar(BACKSPACE);
            }
            else
                // 响铃
                putchar(7); 
        }
        else if (letter != '\r')
        {
            buffer[i++] = letter;
            putchar('*');
        }
    }
    buffer[i] = NULL;
    return (buffer);
}


int main(int argc, char const *argv[])
{
    char *password;

    // 调用函数, 传入提示用户输入的字符串,返回输入的字符串
    password = get_password("Enter Password: ");

    if (strcmp(password, "1001"))
        printf("\nPassword Incorrect\n");
    else
        printf("\nPassword OK\n");

    return 0;
}

例154:使用库函数中的隐式输入函数

#include <stdio.h>
#include <conio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    char *password;

    password = getpass("Enter Password:");

    if (strcmp(password, "1001"))
        printf("Password Incorrect\n");
    else 
        printf("Password OK\n");
    return 0;
}

例155:用户输入字符串和程序参数的综合使用

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    char string[256];

    // 从标准输入流中读取字符串
    while (fgets(string, sizeof(string), stdin))
        // 如果包含程序的第一个参数就打印输入的字符串
        if (strstr(string, argv[1]))
            fputs(string, stdout);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值