这是求三个数中的最大数
#include <reg52.h>
#include <stdio.h>
void uart_init()
{
SCON = 0x50;
TMOD =(TMOD & 0xf)/ 0x20;
TH1 = 221;
TR1 =1;
TI =1;
}
int max(int a,int b,int c)
{ int temp;
temp=a>b?a:b;
temp=temp>c?temp:c;
return temp;
}
void main()
{
int x=10,y=40,z=30,f=50,result=0;
uart_init();
result=max(x,y,z);
printf("x=%d,y=%d,z=%d,",x,y,z);
printf("\nthe largest number is %d.",result);
while (1)
{}
}
最后输出:
X=10,Y=40,Z=30,
The largest number is 40.
如果我装文中所有出现过的int 换成char后,
#include <reg52.h>
#include <stdio.h>
void uart_init()
{
SCON = 0x50;
TMOD =(TMOD & 0xf)/ 0x20;
TH1 = 221;
TR1 =1;
TI =1;
}
char max(char a,char b,char c)
{ char temp;
temp=a>b?a:b;
temp=temp>c?temp:c;
return temp;
}
void main()
{
char x=10,y=40,z=30,f=50,result=0;
uart_init();
result=max(x,y,z);
printf("x=%d,y=%d,z=%d,",x,y,z);
printf("\nthe largest number is %d.",result);
while (1)
{}
}
最后输出:
X=2600,Y=7680,Z=0,
The largest number is 10280.
也就是说我要想用到printf输出,就要定义int变量。那我现在用char,为什么结果是2600呢。这个怎么来的,就算按字符转ASCII那也应该是3130才对啊,还请指教一下。
那也不行啊:
结果变成:
X=
,Y=(,Z=,
the largest number is (.