C语言小程序

1、猜数字游戏

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//猜数字游戏
int main()
{
	int guess = 0;
	//生成随机数
    //0~99 -->  1~100
	int ret = rand() % 100 + 1;//生成随机数的函数
	printf("请猜数字 ");
	while (1)
	{
		scanf("%d", &guess);
		if (guess < ret)
		{
			printf("你猜小了!\n");
		}
		else if(guess > ret)
		{
			printf("你猜大了!\n");
		}
		else if (guess == ret)
		{
			printf("恭喜你,猜中了!\n");
			break;
		}
	}
	return 0;
}

2、goto 语句

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
again:
	printf("hello world!\n");
	printf("hello!\n");
	goto again;//跳转到again符号
	return 0;
}//死循环
//goto语句可以滥用,但尽量少用
//goto不能跳出自己的函数
//常见用法,终止深度嵌入语句,例如在多个循环嵌入

3、关机小程序

//关机程序,电脑运行起来后,1分钟内关机

//如果输入你好,取消关机

在cmd中关机 : shutdown -s -t 60 //windows在60秒后关机

                          shutdown -a   //取消关机

#define _CRT_SECURE_NO_WARNINGS
#incldue <stdio.h>
#include <string.h>
int main()
{
	system("shutdown -s -t 60");
	printf("请注意!你的电脑在60秒内关机,输入:你好,取消关机!\n");
	char input[20] = { 0 };
	scanf("%s", &input);
	again:
	if (strcmp(input,"你好")== 0)//字符串函数库,比较两个字符串是否相同
	{
		system("shutdown -a");
	}
	else
	{
		goto again;
	}
	return 0;
}

//string.h函数库
//求字符串长度        - strlen
//比较两个字符串的大小 - strcmp
//打印数据            - stccpy

www.plusplus.com是一个C++函数库使用指南的网址,下列将示范使用strcpy库函数

1:函数名   2:函数形式  3:参数   4:返回值       5:举例 

函数意思是将一个参数复制到另一参数上

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
	char arr[20] = "hello world";
	memset(arr, 'x', 5);
	printf("%s\n", arr);
	return 0;
}
//输出结果为:xxxxx world

memset(arr,‘c’,5)

可以看出memset就是设置内存的意思,将arr指针后面5-1个的设置为‘x’

 4.1、写一个函数找出两个整数的较大值

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int get_max(int x, int y)
{
	return x > y ? x : y;
}
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	int m = get_max(a, b);
	printf("%d\n", m);
	return 0;
}

 4.2交换函数

#define _CRT_SECURE_NO_WARNINGS
#incldue <stdio.h>
void Swap(int x, int y)
{
	int temp = x;
	x = y;
	y = temp;
}
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	printf("交换前 a=%d b=%d\n", a, b);
	Swap(a, b);
	printf("交换后 a=%d b=%d\n",a,b);
	return 0;
}

结果如下

! 发现没有完成交换

是因为函数调用参数是使用复制,开辟出新的空间赋值给参数,称为形参,所以函数内部改变的参数是改变新开辟出来的空间,而并没有改变原有的参数,成为实参。

#define _CRT_SECURE_NO_WARNINGS
#incldue <stdio.h>
void Swap(int *px, int *py)
{
	int temp = *px;
	*px = *py;
	*py = temp;
}
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	printf("交换前 a=%d b=%d\n", a, b);
	Swap(&a, &b);
	printf("交换后 a=%d b=%d\n",a,b);
	return 0;
}

结果为

4.3简单登入 

#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
int main()
{
	int i = 0;
	char password[20] = { 0 };
	for (i = 0; i < 3; i++)
	{
		printf("请输入密码:>");
		scanf("%s", password);
			if (strcmp(password,"asdfg")==0)
			{
				printf("登录成功!\n");
				break;
			}
			else
			{
				printf("密码错误!");
			}
	}
	if (3 == i)
	{
		printf("三次密码输入错误!\n");
	}
	return 0;
}

 4.4数组指针

#define _CRT_SECURE_NO_WARNINGS
void fun(int a[])
{
	printf("%zu", sizeof(a));//首元素的地址大小
}
int main()
{
	int a[10] = { 0 };
	printf("%zu\n", sizeof(a));//整个数组大小
	printf("%zu\n", sizeof(a[0]));//数组第0个元素的大小
	fun(a);
	return 0;
}

结果为

 

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值