首页 > 代码库 > c1(基础篇)
c1(基础篇)
int a= 10 ;
printf("a = %d\n", a);
char number = ‘H‘;
printf("number = %c\n", number);
//打印字符对应的 ASCII值(十进制)
printf("number = %d\n", number);
short c = 12;
printf("c = %hd\n", c);
long d = 1234678;
printf("d = %ld\n", d);
//float型数据,小数点后6位为有效位,其余的数随机生成
float e = 1.123456789123456;
printf("e = %.10f\n", e);
//double型数据,小数点后15位为有效位
double f = 3.1234567898765432123456789;
printf("f = %.20f\n", f);
//回车:\n
//tab:\t tab键相当于4个空格
//百分号:%%
//引号:\"
//转义字符
//格式控制符,5为位宽,0表示前面补充0
int style = 123;
printf("%05d\n", style);
//格式控制符2,-:表示左对齐,默认的事右对齐,.9表示小数点后的位数
float fl = 123.123456789;
printf("%-15.9f\n", fl);
//断点要打在要输出语句的下一行
//格式要齐:等号左右加空格,逗号后边加空格
//交换两个变量的值
//借助第三个变量
int a1 = 3;
int a2 = 5;
//int a3;
//a3 = a1;
//a1 = a2;
//a2 = a3;
//printf("a1 = %d, a2 = %d\n", a1, a2);//引号里外的语句格式要相同
//不借助第三个变量
a1 = a2 - a1;
a2 = a2 - a1;
a1 = a1 + a2;
printf("a1 = %d, a2 = %d\n", a1, a2);
//1. 赋值运算符 = (把等号右侧的值 赋给 等号左侧)
//2. 算术运算符 (加减乘除、取余)
//加
int m = 2, n = 3;
int sum = 0;
sum = m + n;
printf("sum = %d\n", sum);
//减
int minus = 0;
minus = n- m;
printf("minus = %d\n",minus);
//乘
int mul = 0;
mul = n * m;
printf("mul = %d\n", mul);
//除
//整数相除,小数点后的值被忽略掉
//如果想要算出是1.5,至少要保证 除号 两侧有一个是浮点型的数
int div = 0;
div = n / m;
printf("div = %d\n", div);
//取余
//一般只对整数取余
//被除数 / 除数 = 商
//除数不能为0
int rem = 0;
rem = n%m;
printf("rem = %d\n", rem);
//自增
n++;
++n;
//n = n + 1;
//自减
n--;
--n;
//相当于n = n - 1;
//原则:
//1. ++ 或 ——在前,先+1 或 —1,再运算
//2. ++ 或 ——在后,先运算,再+1 或 —1
int j = 5;
int k = 0;
k = j++;
//k = j;
//j = j + 1;
printf("k = %d, j = %d\n", k , j);
k = ++j;
//j = j + 1;
//k = j;
printf("k = %d, j = %d\n", k, j);
//运算符的优先级
int j1 = 2;
int k1 = 3;
int c1 = 0;
c1 = j1 +(++k1);
printf("j1 = %d, k1 = %d, c1 = %d\n", j1, k1, c1);
//复合运算
int k2 = 5;
int j2 =0;
j2 += k2;// j = j + k;
j2 -= k2;//j = j - k;
j2 *= k2;//j = j * k;
j2 /=k2;//j = j / k;
j2 %= k2;//j = j % k;
int x = 1, y = 2, z = 3;
int c = 10, k = 0;
x +=(++y);
y = x++;
z = x / y;
c %=y;
k = ++c;
printf("x = %d, y = %d, z = %d, c = %d, k = %d\n", x, y, z, c, k);
*/
// 表达式:常量, 变量, 运算符组成
//例如:x + 1, x + y
//语句:程序执行的最小单元,以分号;结束
//以分号来计算语句的数量
//输入函数
int g = 0, h = 5;
printf("请输入两个整数:");
//当遇到scanf,程序程序会等待用户在控制台输入
//确定输入,需要用大回车
scanf("%d %d", &g, &h);
printf("g = %d, h = %d\n", g, h);
//逗号后面要加空格
//单目运算符:不需要加空格(运算符左右两边都有值,如=,+,-)
//双目运算符:左右两侧要加空格(一边有值, 如++1)
return 0;
}
c1(基础篇)