1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
//typedef别名 (type)强制转换 #include <stdio.h> int main(int argc, char const *argv[]) { //使real成为double的别名 typedef double real; real xiaoshu = 3.1415; printf("%f\n", xiaoshu); int num1 = 1.6 + 1.7; // 强制转换 int num2 = (int)1.6 + (int)1.7; printf("%d and %d\n", num1, num2); getchar(); return 0; } // while结合scanf #include <stdio.h> int main(int argc, char const *argv[]) { int num; printf("please key a num\n"); // 如果输入非整型scanf返回值!=1,退出循环 while( scanf("%d", &num) == 1 ) { printf("your key num = %d\n", num); printf("please key a num\n"); } getchar(); return 0; } // 浮点数比较,需要使用math.h中的fabs函数 #include <stdio.h> #include <math.h> int main(int argc, char const *argv[]) { double a = 3.1; double b = 3.2; // 浮点数不能用==比较,舍入误差可能造成两个逻辑上应该相等的数不相等 // 这个判断会输出neq if (a * b == 9.92) { printf("eq\n"); } else { printf("neq\n"); } // 可以使用fabs函数进行浮点数比较 // 这个判断会输出eq if (fabs(a * b - 9.92) < 0.0001) { printf("eq\n"); } else { printf("neq\n"); } getchar(); return 0; } // for循环中可以使用char类型自增 #include <stdio.h> int main(int argc, char const *argv[]) { char c; for (c = 'a'; c <= 'z'; ++c) { printf("the ascii value for %c is %d\n", c, c); } getchar(); return 0; } // please key a num会被输出一次,当输入13或非int时,退出循环 #include <stdio.h> int main(int argc, char const *argv[]) { int num, status; for (printf("please key a num\n"); num != 13;) { status = scanf("%d", &num); if (status != 1) { break; } printf("please key a num again\n"); } printf("haha 13 and not int can be out"); getchar(); getchar(); return 0; } // ,的使用 #include <stdio.h> int main(int argc, char const *argv[]) { // ,可以使表达式从左到右执行 int a,b; a = 13,14; b = (13,14); // 输出 a=13 and b=14 printf("a=%d and b=%d\n", a, b); const int FIRST_OZ = 37; const int NEXT_OZ = 23; int ounces, cost; printf("ounces cost\n"); // for中也可以用, 第一盎司37$,以后每盎司23$ // 1 $0.37 // 2 $0.60 // 3 $0.83 for (ounces = 1, cost = FIRST_OZ; ounces <= 3; ounces++, cost += NEXT_OZ) { printf("%5d $%4.2f\n", ounces, cost / 100.0); } getchar(); return 0; } |