The C Programming Language Chapter 4 Input and Output
#黑日工作室 #
非常感谢 天津城市建设学院周锦姝老师
The C Programming Language Chapter 4 Input and Output
C has no I/O statements
#include <stdio.h>
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
/*ch4_1.c*/
#include <stdio.h>
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
getchar( )

/*ch4_2.c*/
#include <stdio.h>
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
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
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 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
例 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
astatic 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
例 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
例 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
The C Programming Language Chapter 4 Input and Output
4.3 Formatted Input --- Scanf
scanf(char *format,arg1,arg2,...)
reads characters according to the specification
in format,and stores the results through the
remaining arguments(must be pointers)
Notice,
arg1,arg2,...,use address operator &
Conversion specifications,d,o,x,u,c,s,f,e
例 scanf(“%d”,&a);
输入,10?
则 a=10
例 scanf(“%x”,&a);
输入,11?
则 a=17
The C Programming Language Chapter 4 Input and Output
Modifies
l
Modifies Functions
h
m
*
Use before d,o,x,specifies short int
Use before d,o,x,specifies long int
Use before e,f,specifies double
Specifies the maximum field width
Assignment suppression character,the input field is skipped
例 scanf(“%4d%2d%2d”,&yy,&mm,&dd);
输入 19991015?
则 1999?yy,10?mm,15?dd
例 scanf(“%3d%*4d%f”,&k,&f);
输入 12345678765.43?
则 123?k,8765.43?f
例 scanf(“%2d %*3d %2d”,&a,&b);
输入 12 345 67?
则 12?a,67?b
例 scanf(“%3c%2c”,&c1,&c2);
输入 abcde?
则‘ ac1,?dc2
The C Programming Language Chapter 4 Input and Output
Input separators
– White space characters (blank,tab,newline …)
– Literal characters can appear in the scanf format
string ;they must mach the same characters in the input.
例 scanf(“%d%o%x”,&a,&b,&c);
printf(“a=%d,b=%d,c=%d\n”,a,b,c);
输入 123 123 123?
输出 a=123,b=83,c=291
例 scanf(“%d:%d:%d”,&h,&m,&s);
输入 12:30:45?
则 12?h,30?m,45?s
例 scanf(“%d,%d”,&a,&b)
输入 3,4?
则 3?a,4 b例 scanf(“a=%d,b=%d,c=%d”,&a,&b,&c);输入 a=12,b=24,c=36?
The C Programming Language Chapter 4 Input and Output
Notice:
When uses,%c”,blanks and escape
characters are valid input.
如 scanf(“%c%c%c”,&c1,&c2,&c3);
若输入 a b c?
则 a?c1,?c2,b?c3
An input field extends to the next white space character
(blank,tab,newline … ),or invalid input or until the
field width,is exhausted.
如 scanf(“%d%c%f”,&a,&b,&c);
若输入 1234a123o.26?
则 1234?a,?ab,123?c
The C Programming Language Chapter 4 Input and Output
4.4 Examples
/*ch3_12.c*/
#include <math.h>
#include <stdio.h>
main()
{ float a,b,c,s,area;
scanf("%f,%f,%f",&a,&b,&c);
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2f\n",a,b,c,s);
printf("area=%7.2f\n",area);
}
例 输入三角形边长,求面积输入,3,4,6?
输出,a= 3.00,b= 4.00,c= 6.00 s= 6.50
area= 5.33
File Inclusion
Declarations
Data input
Data output
The C Programming Language Chapter 4 Input and Output
例 从键盘输入大写字母,用小写字母输出
/*ch3_13.c*/
#include "stdio.h"
main()
{ char c1,c2;
c1=getchar();
printf("%c,%d\n",c1,c1);
c2=c1+32;
printf("%c,%d\n",c2,c2);
}
输入,A?
输出,A,65
a,97
The C Programming Language Chapter 4 Input and Output
/*ch3_14.c*/
#include <stdio.h>
#include <math.h>
main()
{ float a,b,c,disc,x1,x2,p,q;
scanf("a=%f,b=%f,c=%f",&a,&b,&c);
disc=b*b-4*a*c;
p=-b/(2*a); q=sqrt(disc)/(2*a);
x1=p+q; x2=p-q;
printf("\n\nx1=%5.2f\nx2=%5.2f\n",x1,x2);
} 输入,a=1,b=3,c=2?
输出,x1=-1.00
x2=-2.00