王道训练营-C语言-1

1.字符

#include <stdio.h>
#include <stdlib.h>

int main(){
	char c;
	c='
';//转义字符,换行,对应enter
	c='
';//回车,回到行首
	//scanf("%c",&c);//scanf拿不到退格键
	printf("%o
",123);//输出十进制123的八进制表示178
	printf("%x
",123);//输出十进制123的十六进制表示178
	printf("\\123
");// 输出:\123

	while(scanf("%c",&c)!=EOF){
		if(c=='
')
			break;
		printf("%c
",c-32);//小写字母转为大写字母
	}
	
	system("pause");
	return 0;
}

2.逻辑运算符

#include <stdio.h>
#include <stdlib.h>

int main(){
	int year;
	scanf("%d",&year);
	10==year&&printf("year is 10");	//逻辑与的短路运算,只有当输入为10的时候才执行逻辑与后面的语句
	10==year||printf("year is not 10");//逻辑或的短路运算,与逻辑与相反,可省略if语句
	system("pause");

}

3.位操作符

#include <stdio.h>
#include <stdlib.h>

int main(){
	short a,b;
	int m;
	m=1;
	a=60;
	b=13;
	
	//b=~a;
	printf("%d
",a&b);
	printf("%d
",a|b);
	printf("%d
",a^b);//按位异或
	printf("m<<10=%d
",m<<10);
	printf("m=%d
",m);//左移之后,m的值没变

	//正数
	//右移 除2 高位补0,低位丢弃
	m=200;
	printf("m>>1=%d
",m>>1);//右移就是除2,对于负数,需要先减1,再除2
	//负数
	//左移 乘2 高位补1,低位丢弃 
	m=-6;
	printf("m<<1=%d
",m<<1);//左移就是乘2

	system("pause");
}

4.自增自减运算符

#include <stdio.h>
#include <stdlib.h>

int main(){
	int a=10,b;
	b=a++; //其实是:b=a;a=a+1;
	printf("a=%d,b=%d
",a,b);
	b=++a;
	printf("a=%d,b=%d
",a,b);
	system("pause");

}

a=11,b=10
a=12,b=12

5.条件运算符

#include <stdio.h>
#include <stdlib.h>

int main(){
	int a,b,c,maxabc;
	scanf("%d%d",&a,&b,&c);
	c=a>b?a:b;//把ab中的大数赋值给c
	printf("a=%d,b=%d,c=%d
",a,b,c);
	maxabc=(a>b?a:b)>c?(a>b?a:b):c;//把abc中的大数赋值给maxabc
	printf("a=%d,b=%d,c=%d,maxabc=%d",a,b,c,maxabc);
	system("pause");
	//另外,逗号运算符
	//int i=24;
    //int n = (i++,i++,i++,i++); // n == 27
}

23 -45 12
a=23,b=-45,c=23
a=23,b=-45,c=23,maxabc=23请按任意键继续. . .

原文地址:https://www.cnblogs.com/zychen/p/7371689.html