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>
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)
The C Programming Language Chapter 4 Input and Output
/*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
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);
}
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
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
Notice
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
*
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);
1234567?
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 bscanf(“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);
abc?
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
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