找三个数中的最大值
代码如下
#include<stdio.h>
int Max(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int a = 0, b = 0, c = 0, max = 0;
scanf_s("%d %d %d", &a, &b, &c);
max = Max(Max(a, b), c);
printf("max=%d", max);
return 0;
}
找出三个数中的中间值
void swap(int* x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
int Mid(int a, int b, int c)
{
int tmp;
if (a > b)
{
swap(&a, &b);
}
if (b > c)
{
swap(&b, &c);
}
if (a > b)
{
swap(&a, &b);
}
return b;
}
int main()
{
int a = 0, b = 0, c = 0, mid = 0;
scanf_s("%d %d %d", &a, &b, &c);
mid = Mid(a, b, c);
printf("mid=%d", mid);
return 0;
}
计算字符串中各种类型的个数
int main()
{
char ch = 0;
int consum = 0, digsum = 0, capsum = 0, smasum = 0, othsum = 0;
while (ch = getchar(), ch != '\n')
{
if (iscntrl(ch))
{
consum += 1;
continue;
}
if (isdigit(ch))
{
digsum += 1;
continue;
}
if (isupper(ch))
{
capsum += 1;
continue;
}
if (islower(ch))
{
smasum += 1;
continue;
}
othsum += 1;
}
printf("consum: %d \n", consum);
printf("digsum: %d \n", digsum);
printf("capsum: %d \n", capsum);
printf("smasum: %d \n", smasum);
printf("othsum: %d \n", othsum);
return 0;
}
空指针不能进行解引用
定义指针时一定要初始化,从而避免野指针
形参不能改变实参的值