- 前置++的运算规律
int a=1,b=0;
b=(++a)+(++a); //第一个括号里的a为1,第二个括号里的a为2
printf("%d,%d\n",a,b);
// 3,6
- 强制转换的正确格式
int m=5;
printf("m/2=%d,m/2.0=%f\n",m/2,m/2.0);
printf("(float)m/2=%f\n",(float)m/2);
printf("(float)(m/2)=%f\n",(float)(m/2));
printf("float(m)/2=%f,float(m/2)=%f\n",float(m)/2,float(m/2));
printf("m=%d,m=%f\n",m,m);
//C:\Users\夜雨霖铃有点烦\Desktop\暑假备考\main.c|10|error: expected expression before 'float'|
改正需要printf(“float(m)/2=%f,float(m/2)=%f\n”,float(m)/2,float(m/2));
- 左移<< 负数符号位补1
int x;
x=-1;
x<<3;
printf("x=%d\n",x);
x=x<<3;
printf("x=%d\n",x);
//x=-1
//x=-8
- 输出格式的测试
float f1=100.15799;
printf("%2.1f\n%07.1f\n%8.1f\n",f1,f1,f1);
//100.2
//00100.2
// 100.2
- _ _ func _ _表示当前函数名字;
int f(void);
int gAll = 12;
int main(void)
{
printf("in %s gAll=%d\n",__func__,gAll);
f();
printf("in %s gAll=%d\n",__func__,gAll);
return 0;
}
int f(void)
{
printf("in %s gAll=%d\n",__func__,gAll);
gAll = 2;
printf("in %s gAll=%d\n",__func__,gAll);
return gAll;
}
//OUTPUT
/*in main gAll=12
in f gAll=12
in f gAll=2
in main gAll=2*/
- 宏定义
#define PI 3.14159
#define FORMAT "%f\n"
#define PI2 2*PI //pi*2
#define PRT printf("%f ",PI); \
printf("%f\n",PI2)
int main(void)
{
PRT;
return 0;
}
- 注释不影响宏定义;
- \ 表示这一行还没结束;
- 结构体变量,做函数返回值,注意 return !
struct date
{
int year;
int month;
int day;
};
struct date func(struct date p)
{
p.year = 2000;
p.month = 5;
p.day = 22;
return p;
}
main()
{
struct date d;
d.year = 1999;
d.month = 4;
d.day = 23;
printf(“%d,%d,%d\n”, d.year, d.month, d.day);
d = func(d);
printf(“%d,%d,%d\n”, d.year, d.month, d.day);
}
- \t 转义字符的用法注意
int main(void)
{
printf("abc\t123\n");
printf("c\t123");
return 0;
}
//第一行的123和第二行的123对齐了,也就意味着
//无论前面的一串字符长短,\t 都给推到下一个表格符里去了
//abc 123
//c 123
猜数字
- 随机数使用rand(),随机种子使用srand()
int magic;
int guess;
int counter=0;
unsigned int seed;
printf("Please enter the seed:");
scanf("%u",&seed);
magic=rand()%100+1; //将获取的随机数限制在1~100之间
do{
printf("Please guess a magic number:");
scanf("%d",&guess);
counter++;
if(guess>magic)
{
printf("Wrong ! Too high ! \n");
}
else if(guess<magic)
{
printf("Wrong ! Too low ! \n");
}
else
{
printf("Right ! \n");
printf("The number is:%d\n",magic);
}
}while(guess != magic);
printf("counter=%d\n",counter);
return 0;
- 将系统时间作为随机数发生器的种子,用time();需包含time.h的头文件
int magic;
int guess;
int counter=0;
srand(time(NULL)); //使用NULL作为函数的参数,原因见书95页
magic=rand()%100+1;
do{
int i=1;
printf("Please guess a magic number:");
i=scanf("%d",&guess);
counter++;
if(guess>magic)
{
printf("Wrong ! Too high ! \n");
}
else if(guess<magic)
{
printf("Wrong ! Too low ! \n");
}
else
{
printf("Right ! \n");
printf("The number is:%d\n",magic);
}
}while(guess != magic);
printf("counter=%d\n",counter);
return 0;
- 在玩家输入非数字字符时,允许玩家重新输入
int magic;
int guess;
int counter=0;
srand(time(NULL));
magic=rand()%100+1;
do{
int i=1;
printf("Please guess a magic number:");
i=scanf("%d",&guess);
fflush(stdin);
if(0==i)
{
printf("Please enter the right number !\n");
continue;
}
counter++;
if(guess>magic)
{
printf("Wrong ! Too high ! \n");
}
else if(guess<magic)
{
printf("Wrong ! Too low ! \n");
}
else
{
printf("Right ! \n");
printf("The number is:%d\n",magic);
}
}while(guess != magic);
printf("counter=%d\n",counter);
return 0;
判断是否相等的时候,可以把常量置于关系运算符的左边而将变量置于关系运算符的右边来避免把=(赋值运算符)和==(关系运算符)弄混淆的错误
注意fflush(stdin);
的重要性,如果没有,将在错误输入非数字字符后进入死循环,应为scanf()的输入格式%d只能读取数字,对于非数字字符每次scanf都会去尝试读取但读取失败,将字符继续保留在缓冲区,从而造成死循环。