C C auto break case char const continue
不要使用C语言关键字作为标识符 C语言关键字列表 auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
3.整型变量 1) 整型变量的分类 基本型 以int表示。-32768~32767 (-215~215 -1) 短整型 以short int或short表示,取值范围同int型。 长整型 以long int或long表示。 -2147483648~2147483647 (-231~231 -1 ) 无符号型 用全部二进制位(bit)存放数据, 不能存放负数。 unsigned int与unsigned short: 0~65535(0~216 -1) unsigned long : 0~4294967295 (0~232 -1) 2) 整型变量的定义 int a, b; unsigned short c, d; long e, f ;
#include<stdio. h> void main( ) { int w, x=1, y=0, z=1; w=!y||++x&&++z; //!y短路++x&&++z printf("w=%d, x=%d, y=%d, z=%dn", w, x, y, z); x=z=1; y=0; //重置 w=y||++x&&++z; printf("w=%d, x=%d, y=%d, z=%dn", w, x, y, z); x=z=1; y=0; //重置 w=!y&&++x||++z; //!y&&++x短路++z printf("w=%d, x=%d, y=%d, z=%dn", w, x, y, z); x=z=1; y=0; //重置 w=y&&++x||++z; //y短路++x printf("w=%d, x=%d, y=%d, z=%dn", w, x, y, z); }
1. putchar() 语法: #include <stdio. h> int putchar( int ch ); 功能: putchar()函数把一个字符ch插入stdout(标准输出)流当前(光标) 位置、putchar()的返回值是被写的字符, 发生错误时返回EOF。 如果ch为控制字符则产生相应操作! 例2. 7 #include <stdio. h> void main( ) { char a, b; a=' o' ; b=' k' ; putchar(a); putchar(b); putchar(' n' ); putchar(a); putchar(' n' ); putchar(b); putchar(' n' ); }
③各域说明: ★ type (必要)字符:它确定了与之相关联的输出项是否为 一个字符[c]、一个数[d i u o x f e g ]或一个字符串[s]。 [c] : specifies a single-byte character. [d i] : Signed decimal integer. [u] : Unsigned decimal integer. [o] : Unsigned octal integer. [x] : Unsigned hexadecimal integer, using "abcdef. " [f] : Signed value having the form [ – ]dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. [e] : Signed value having the form [ – ]d. dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –.
#include <stdio. h> void main( void ) {int num 1=65, num 2=48; float f 1=1. 234, e 1=25. 6 e 3, e 2=25. 6 e 5; double df=123456789; char ch 1=' 101' , ch 2=' 0' ; printf("the result is: n%ct%dn", num 1, num 2); printf("%dt%cn", ch 1, ch 2); printf("0123456789 ABCDEF 012345n"); printf("%fn%lfn", f 1, df); printf("- - - n"); printf("%fn%en%gn", e 1, e 1); printf("- - - n"); printf("%fn%en%gn", e 2, e 2); printf("- - - n"); printf(ch 2==0? "%cn": "%sn%d, %dn", "china", num 1+=1); }
通常输出项的数目及数据类型要与对应的type字符相 匹配!printf()并没有将相应的输出项进行强制类型转换! 请分析下面程序的结果。 #include <stdio. h> void main( void ) {int num 1=65, num 2=-1; float f=1. 234; double df=5. 6789; char ch=' 101' ; printf("the result is: n"); printf("%f %c %on", f, num 1, ch); printf("%dtt%dn", num 1, num 2); printf("- - - -n"); printf("%ott%xn", num 1); printf("%ot%xn", num 2); printf("%un", num 2); printf("- - - -n"); printf("%ft%dn", num 1, f); printf("%ft%lfn", df, f); printf("- - - -n"); printf("%dn", num 1, num 2); printf("%dt%dn", num 1); }
#include <stdio. h> void main( void ) {int num 1=65, num 2=-1; float f=1. 236; char ch=' 101' ; printf("the result is: n"); printf("12345678n"); printf("%+5. 3 dt%-+5. 3 dn", num 1, num 2); printf("%#6. 3 xt%-#6 on", num 1); printf("%-+4. 3 dt%05 c, %-05 cn", 1234, ch); printf("- - - - - n"); printf("%+6. 2 ft%6. 2 sn", f, "CHINA"); }
例2. 8 main( ) {unsigned int a=65535; int b=-2; printf("a=%d, %o, %x, %un", a, a); printf("b=%d, %o, %x, %un", b, b); }
例2. 10 main( ) {printf("%s, %3 sn", "CHINA"); printf("%7. 2 s, %. 4 s, %-5. 3 sn", "CHINA", "CHINA"); } 输出结果为: CHINA, CHINA �����CH, CHIN, CHI�� 例2. 11 若f =123. 456,下列语句: 输出结果为:
实例一:输入学号姓名性别成绩并输出 #include<stdio. h> void main( ) { int num; char name[20]; char sex; float score; printf("your num ? "); scanf("%d 6 d", &num); //201201回车:buffer遗留有'r''n' printf("your name ? "); scanf("%s", name); //caiyihong回车: //跳过遗留'r''n',提取后、遗留有新的'r''n' printf("your sex ? "); methods 1: //rewind(stdin); //clear the keyboard buffer scanf("%c", &sex); //提取'n': buffer空! methods 2: //scanf(" %c", &sex); //%c前空格先"吃掉"遗留'r''n'; 再敲m回车 printf("your score ? "); scanf("%4 f", &score); //等待. . . ? printf("nnum: %6 dnname: %snsex: %cnscore: %4. 1 fn", num, name, sex, score); }
实例二、分析程序执行结果! #include<stdio. h> void main( ) { int num 1, num 2, num 3, num 4; int num 5, num 6, num 7, num 8; scanf("%2 d%*3 d%2 d", &num 1, &num 2); //1234567回车 printf("%d, %dn", num 1, num 2); rewind(stdin); scanf("%2 d%*3 d%2 d", &num 3, &num 4); //123 456回车 printf("%d, %dn", num 3, num 4); rewind(stdin); scanf("%2 d%*3 d%2 d", &num 5, &num 6); //12 34567回车 printf("%d, %dn", num 5, num 6); rewind(stdin); scanf("%2 d%*3 d%2 d", &num 7, &num 8); //12 a 34567回车 printf("%d, %dn", num 7, num 8); }
- Slides: 67