stdout, ( ).
() :
int putchar(int c);
c , .
:
putchar(A);
() :
int puts(const char * s);
s , 0, \n ( ), - , \n EOF . EOF (End Of File) , stdio.h, -1.
:
puts(Hello world);
( ):
int printf(const char *format, );
, format - , , , . , , , . , , . () .
%. :
%[flags][width][.prec][h:l:L]type
flags :
- - ( ),
+ - ( +),
# - , type;
width , ;
prec , , ;
h:l:L , , short, long (double);
type - , :
d, i - int, ;
o - int, ( 0 );
x, X - int, ( 0x 0X ) c abcdef ABCDEF;
u - unsigned int, ;
c - int ( char), , ;
s - char *, , 0;
f - double, ;
e, E - double, ( e E);
|
|
g, G - double, %/%E %f;
p - void *, ;
% - , %.
#, . g, G, f, e , . # X, 0 0. , 0.
, printf_s, , printf. printf_s printf , printf_s , printf , NULL.
:
#include <stdio.h>
#include <stdlib.h>
void main()
{
double x=10.55, y=-12.333;
int a1=10, a2=20;
char Str[]="Hello world";
char Ch='A';
printf("x=%fy=%fa1=%da2=%d", x, y, a1, a2);
// x=10.550000y=12.333000a1=10a2=20
printf("\nx=%8.2fy=%-8.2fa1=%+4da2=%3d", x, y, a1, a2);
// x= 10.55y=-12.33 a1= +10a2= 20
printf("\n%s %c %%\n", Str, Ch); // Hello world A %
system("pause"); //
}
stdin
stdin, . , Enter.
:
int getchar();
. :
char Ch;
Ch=getchar();
:
char * gets(char * s);
s , , \n ( Enter), 0, NULL .
:
char Str[64];
printf( :); gets(Str);
, Microsoft Visual ++ 2013 gets ( ), , gets gets_s.
, , 1.
1
gets_s , ++, , .
gets_s.
char Str1[32];
printf("Stroka1: ");
gets_s(Str1); // ,
char *Str2 = (char *)malloc(32);
|
|
printf("Stroka2: ");
gets_s(Str2, 31); // ,
:
int scanf(const char * format, );
, printf, format , , , - , EOF . , , , , .
%, :
%[*][width][h:l:L]type
* - , , , ;
width , ;
[h:l:L] - , printf;
type - , :
d - int * ( ), ;
i - int *, , (0 ) (0x 0X ) ;
o - int *, ( );
x, X - int *, ( 0x/0X );
u - unsigned int *, ;
c - char *, ;
s - char *, , ( ) 0, , , ;
f, e, E, g, G - float *, , ;
% - , %, , %.
, Microsoft Visual ++ 2013 scanf ( ), , scanf scanf_s . , , , . 1.
scanf, scanf_s , c, s (. ).
:
#include <stdio.h>
#include <stdlib.h>
void main()
{
float x;
int a;
char Ch;
double X;
char Str[64];
printf("x, a, Ch: ");
scanf_s("%f %d %c", &x, &a, &Ch, 1);
// scanf scanf("%f %d %c", &x, &a, &Ch);
// format
printf("x=%f a=%d Ch=%c", x, a, Ch);
printf("\nX="); scanf_s("%lf", &X); // double
printf("Str: ");
scanf_s("%s", Str, 63); // , ,
//
// scanf scanf("%s", Str);
printf("propusk: "); scanf_s("%*d.%d", &a);
// 10.12 a 12
printf("X=%f Str: %s a=%d\n", X, Str, a);
system("pause"); //
}