آموزش زبان c – توابع ورودی و خروجی
DESCRIPTION
آموزش زبان C – توابع ورودی و خروجیTRANSCRIPT
-
42 C 3: 43
3
C
:
1. printf() .2. printf() .3. scanf() .4. printf() scanf() .5. getchar() .6. putchar() .7. getche() .8. getch() .9. gets() puts() .
C . stdio.h . scanf printf . . getchar putchar . gets puts . . . . printf() . . . . printf ("control string", arguments list) ;
printf ("control string", arg1, arg2,, argn) ;
. . "%" . type conversion "%" . . printf 31 . 31 printf ()
. %c
. %d
. %i
.%f
floating-point . %e
floating-point %f %e ( ) . %g
. %u
. %s
8 . %o
16 . %x
%o , %x , %u , %d ( ld). L
. %p
% . %%
( 31 .#include
main ()
{ int x =31 ;
float y =148.5 ; char z[10] = {"PayamNoor"};
printf(" %d %f %s " , x , y , z) ;
} %s ,%f , %d . .
31 148.5 PayamNoor
.
(( 32 .
#includemain ()
{
float x=2.0 , y=3 ;
printf("%f %f %f %f %d", x , y , x+y , x*y, x) ;
} .
2.0000003.0000005.0000006.000000 2
. . .(( 33 .
#include
main ()
{ double x=50.0 , y=0.25 ; printf(" %f %f %f %f\n ", x , y , x*y , x / y) ;
printf(" %e %e %e %e ", x , y , x*y , x / y) ;
}
printf %f %e . "\n" . .
50.000000 0.250000 12.500000 200.000000
5.000000e+01 2.500000e-01 1.250000e+01 2.000000e+02
x/y , x*y , y , x floating point "%e" . 6 . . . ( ) ( ). ( ) % . . . m "0" . %04d . floating point "." . %10.4f .
(( 34 .
#includemain ()
{
int x = 12345 ; float y =345.125 ; printf("3d %5d %8d\n\n", x , x , x) ; printf("%3f %10f %13f\n", y , y , y) ; printf("%3e %10e %13e\n", y , y , y) ; printf("%g %10g %13g \n", x , x , x) ;
printf("%3g %10g %13g ", x , x , x) ; } .
12345 12345 12345
345.125000 345.125000 345.125000
3.45125e+02 3.45125e+02 3.45125e+02
345.125 345.125 345.125
345.125 345.125 345.125
( ) . ( 5 ). . . blank . ( ). . f e . %g . . .
floating point . precision . "." . precision ( ) precision "." . floating point precision .
(( 35 . #include
main ()
{
float x =123.456 ; printf("%7f %7.3f %7.1f\n", x , x , x) ; printf("%12e %12.5e %12.3e", x , x , x) ;
}
.
123.456000 123.456 123.5
1.234560e+02 1.23456e+02 1.235e+02 f . . ( ) . e . 3 . ( ) .( . .
printf(" %.4f ", x) ;
flag . flag (%) . flag . flag C 32 . 32 Flag
Flag
. ._
(+ -) . flag .+
blank . .0
blank . (+) . blankspace
0 0x .#( 0- x-)
floating-point "." . g .#( e- g-, f-,)
( 36 . #include
main ()
{
int x =123 ; float y =45.0 , z = -6.7 ; printf(": %5d %7.0f %10.1e: \n", x , y , y) ; printf(": %-5d %-7.0f %-10.1e: \n", x , y , z) ; printf(": %+5d %+7.0f %+10.1e: \n", x , y , z) ; printf(": %7.0f %#7.0f %7g %#g: " , y , y , z , z) ;
}
":" .
: 123 45 -6.7+00:
: 123 45 -6.7e+00:
: +123 +45 -6.7e+00:
:+123 +45 -6.7e+00:
: 45 45. -6.7 -6.500000:
. "-" . "+" . . ( ) . "-" "+" . "-" "+" . "#" 45 "g" .
(( 37 8 10 16 . #include
main ()
{ int x = 1234 , y = 0155 , z = 0xa06b ;
printf(": %6u %6o %6x: \n", x , y , z) ;
printf(": %-6u %-6o %-6x: \n", x , y , z) ;
printf(": %#6u %#6o %#6X: \n" , x , y , z) ;
printf(": %06u %06o %06X: ", x , y , z) ;
}
":" .
: 1234 155 a06b:
:1234 155 a06b:: 1234 0155 0XA06B::001234 000155 00A06B: 10 8 16 . "-" . "#" . 8 16 "0" "0x" . "X" 16 ( 0XA06B) . "0" . . "X" 16 .
( scanf()
C scanf . scanf printf . . printf . scanf ("control string", arguments list) ;
scanf ("control string", argl , arg2 ,, arg n) ;
printf . printf . printf %s, %c , %f , %d . "&" .
"&" C ( ) .
( 38 & scanf .
#include
main ()
{ int x ;
char name[6] ;
scanf("%d" , &x) ;
scanf("%s", name) ;
printf("%d %s", x , name) ;
}
scanf x . scanf & name "book" book .
( 33 . 33 scanf ()
.%c
( 10) .%d
.%i
.%u
(floating_point) .%f , %e, %g
(short integer) .%h
. non_white_space white_space ( "\0" ).%s
8 .%0
16 .%x , %X
.%p
( 39 79 .
#include
main () /* read a line of text */
{
char line[80] ;
int count , k ;
/* read in the line */
for (k=0 ; line[k]=getchar ()!=\n ; + +k) count = k ;
for (k=0 ; k
main ()
{
float x = 123.456 ;
printf ("%f %.3f %7.2f" , x , x , x) ;
}6. .
7. .
8. .. format string
. control string