C语言中常见函数,以及应用

目录

1.函数getchar()与putchar()

2.函数system()与strcmp()

3.sqrt()函数


1.函数getchar()与putchar()

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main()
{
	int ch;
	while((ch=getchar())!=EOF)//EOF - end of file -文件的结束标志
	{
	
		putchar(ch);// putchar() = printf("%c", ch);
	}
	
	return 0;
	
}

对于getchar(),putchar()的应用:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int main()
{
	int ch;
	
	char password[] = {0};
	printf("请输入密码");
	scanf("%s",password);
	int ch1;
	while ((ch1 = getchar()) != '\n')
	{
		ch1 = getchar();
	}//这段代码的作用是清除缓存区的数据

	printf("请确认密码(Y/N)");
	
	ch = getchar();
	if (ch == 'Y')
	{
		printf("确认成功");
	}
	else
	{
		printf("确认失败");
	}
	return 0;
	
}

2.函数system()与strcmp()

system()函数:执行系统命令

strcmp()函数:将俩个字符串比较,如果相同返回0

实例:关机小程序

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


//关机程序
int main()
{ 
	char input[20] = { 0 };
    system("shutdown -s -t 60");

    //system()函数:执行系统命令
    //需要头文件<stdlib.h>
    //shutdown关机 -s设置关机 -t设置关机时间 60 设置关机时间60秒

	again:
	printf("请注意,你的电脑将在1分钟内关机,输入:我是猪,取消关机\n");
	scanf("%s",input);
	
    //俩个字符串比较不能使用“==”,应该使用strcmp() 全称string compare
	//strcmp()函数:将俩个字符串比较,如果相同返回0
	//使用strcmp()函数需要引用头文件#include <string.h>
	
	if (strcmp(input,"我是猪")==0)
	{
		system("shutdown -a");
	}
	else
	{
		goto	again;
	}
	return 0;
}

3.sqrt()函数

sqrt()是求平方根函数 

例如:在查找素数(质数)的程序中可以用到

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>

int main()
{ 
	
	int i;
	int j;
	int count=0;
	for (i=101;i<=200;i+=2)
	{
		int flag = 1;
		for (j = 2; j <=sqrt(i); j++)
		{
			if (i%j==0)
			{
				flag = 0;
				break; 
			}
			
		}
		if (flag) {
			count++;
			printf("素数%d\t", i);
			
		}
		
	}
	
	printf("素数%d\t", count);

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值