The C Programming language Chapter 4 Input and Output #黑日工作室 ◆非常感谢天肆掠市建学院 周锦姝老师
The C Programming Language Chapter 4 Input and Output #黑日工作室# 非常感谢 天津城市建设学院 周锦姝老师
The C Programming language Chapter 4 Input and Output Chapter 4 Input and Output C has no lo statements √# include a Standard input and output U Formatted output --- Printf u Formatted input -- Scanf 日 Examples
The C Programming Language Chapter 4 Input and Output ✓C has no I/O statements ✓ #include Chapter 4 Input and Output ❑ Standard input and output ❑ Formatted output --- Printf ❑ Formatted input --- Scanf ❑ Examples
The C Programming language Chapter 4 Input and Output 44.1 Standard input and output How to put a character on the standard output (normally the display) putchar(c c may be character constants, variables and expressions /*ch41.c* #include main 运行结果:A char a 65:a=B B putchar(); putchar('in); putchar(a)
The C Programming Language Chapter 4 Input and Output /*ch4_1.c*/ #include main( ) { int c; char a; c=65; a='B'; putchar(c); putchar('\n'); putchar(a); } 运行结果:A B 4.1 Standard input and output ❖How to put a character on the standard output (normally the display) ➢ putchar( c ) ➢ c may be character constants,variables and expressions
The C Programming language Chapter 4 Input and Output .s How to read one character at a time from the standard input (normally the keyboard getchar () /*ch42.c* #include w, mainO i int c 运行结果 printf("Enter a character Enter a character: A J A--->hex 1 getchar printf("%oc--->hex%n",c, c)
The C Programming Language Chapter 4 Input and Output getchar( ) 例 /*ch4_2.c*/ #include main() { int c; printf("Enter a character:"); c=getchar(); printf("%c--->hex%x\n",c,c); } 运行结果: Enter a character:A A--->hex41 ❖How to read one character at a time from the standard input (normally the keyboard)
The C Programming language Chapter 4 Input and Output 4.2 Formatted output -- Printf printf (char *format, argl, arg2,..) printf converts, formats, and prints its arguments on the standard output under control of the format printf max=%d, min=%05d\n,a, b) 沙 Notice: The fol例ita ects: printf(%od %dn, a, b) Ordinar printf(a=%d, b=%d\n?", a, b); eam, Convers输出结果:34 and printing of the a=3.b=4 %o modifier conversion character
The C Programming Language Chapter 4 Input and Output ➢ printf (char *format, arg1,arg2, ...) ➢ printf converts ,formats,and prints its arguments on the standard output under control of the format 4.2 Formatted output --- Printf Notice : The format string contains two types of objects: ▪Ordinary characters: copied to the output stream; ▪Conversion specifications: cause conversion and printing of the next successive arguments to printf. % [modifier] conversion character 例 int a=3,b=4; printf(“%d %d\n”,a,b); printf(“a=%d , b=%d\n”,a,b); 输出结果: 3 4 a=3, b=4 printf (“max=%d,min=%5d\n” , a , b);
The C Programming language Chapter 4 Input and Output Basic Printf Conversions d decimal integer int a=567: printf(" %od, a) 567 x.X unsigend hexadecimal integer a=255: printf( %x, a) f unsigend octal integer inta=65 printf(%oo”a) 101 unsigend decimal integer int a=567: printf(%",a) 567 single character char a=65: printf("%oc", a) A M S string printf( %",ABC) ABC eE floating 例main 677890e+002 f floati f unsigned int uF65535 printf(u%od n", u) 567.789000 G 567.789 输出结果:u printf(90%) 111111111111111165535
The C Programming Language Chapter 4 Input and Output d,i decimal integer x,X unsigend hexadecimal integer o unsigend octal integer u unsigend decimal integer c single character s string e,E floating point (exponential form) f floating point (decimal form) g,G the shorter between f and e %% % Basic Printf Conversions int a=567;printf ( “%d”,a); 567 int a=255;printf(“%x”,a); ff int a=65;printf(“%o”,a); 101 int a=567;printf(“%u”,a); 567 char a=65;printf(“%c”,a); A printf(“%s”,“ABC”); ABC float a=567.789; printf(“%e”,a); 5.677890e+002 float a=567.789; printf(“%f”,a); 567.789000 float a=567.789; printf(“%g”,a); 567.789 printf(“%%”); % 例 main() { unsigned int u=65535; printf(”u=%d\n",u); } 输出结果:u=-1 11 11 11 11 11 11 11 11 65535
The C Programming language Chapter 4 Input and Output Modifies Modifies Function Specifies the minimum field width. >m, printed completely <m, padded on the left Specifies the number of digits after the decimal point of a floating-point value Specifies the maximum number of characters to be printed from a string Specifies left adjustment of the converted argument Specifies that the number will al ways be printed with a sign 0# Specifies padding to the field width with leading zero For o or x the first digit will be o or OX Before d, o, x, u, specifies long Before e, f, g, specifies double
The C Programming Language Chapter 4 Input and Output Modifies Function m Specifies the minimum field width. >m,printed completely ; <m ,padded on the left. .n Specifies the number of digits after the decimal point of a floating-point value. Specifies the maximum number of characters to be printed from a string. – Specifies left adjustment of the converted argument. + Specifies that the number will always be printed with a sign. 0 Specifies padding to the field width with leading zero. # For o or x ,the first digit will be 0 or 0x l Before d,o,x,u, specifies long Before e,f,g, specifies double Modifies
The C Programming language Chapter 4 Input and Output 皿n例inta=1234; float f=123.456; char ch=a printf( %8d, %2dn, a, a) printf( %f, %8f, %8.1f,%o2f,%.2en', f, f, f, f) printf( %o3cn', ch); 运行 1234.1234 static char a[=Hello, world! printf( %osn%15sn%10.5s n%2.5sn%3s n, a, a, a, a, a) 运行结果: Hello, world! Hello world! Hello Hello Hel
The C Programming Language Chapter 4 Input and Output 例 int a=1234; float f=123.456; char ch=‘a’; printf(“%8d,%2d\n”,a,a); printf(“%f,%8f,%8.1f,%.2f,%.2e\n”,f,f,f,f,f); printf(“%3c\n”,ch); 运行 1234,1234 结果: 123.456000,123.456000, 123.5,123.46,1.23e+002 a static char a[]=“Hello,world!” printf(“%s\n%15s\n%10.5s\n%2.5s\n%.3s\n”,a,a,a,a,a); 运行结果:Hello,world! Hello,world! Hello Hello Hel m.n
The C Programming language Chapter 4 Input and Output 例inta=1234; float f=.456 static char c[=Hello, world printf( %8d,%o-8dn,, a, a) printf( %102f, %0-10.1fn',f,f) printf( %105s,%-10.3sn',c,c); 运行结果:1234,1234 123.46.123.5 Hello. hel
The C Programming Language Chapter 4 Input and Output 例 int a=1234; float f=123.456; static char c[]=“Hello,world!”; printf(“%8d,%-8d\n”,a,a); printf(“%10.2f,%-10.1f\n”,f,f); printf(“%10.5s,%-10.3s\n”,c,c); 运行结果: 1234,1234 123.46,123.5 Hello,Hel -
The C Programming Language Chapter 4 Input and Output 0、+例inta=1234 float f=123.456 printf(%08dn', a) /00001234 printf( 010.2fn”,f);/000012346 printf(0+8dna);/000+1234 printf0+10.2fn,f);,/00+123.46 # 例inta=123; printf( %o0, %o, %X, %fXn, a, a, a, a) //173.0173.7B.0X7B 00000000000000010000000000000000 例 long a=65536; rin tfd,%8ldmn,a,a);/0.65536
The C Programming Language Chapter 4 Input and Output 例 int a=1234; float f=123.456; printf(“%08d\n”,a); printf(“%010.2f\n”,f); printf(“%0+8d\n”,a); printf(“0+10.2f\n”,f); 0 、+ 例 int a=123; printf(“%o,%#o,%X,%#X\n”,a,a,a,a); # 例 long a=65536; printf(“%d,%8ld\n”,a, a); l 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 //00001234 //0000123.46 //000+1234 //000+123.46 //173,0173,7B,0X7B //0, 65536