Input/Output

From <stdio.h>
printf
int printf( const char *format, arg0,…,argN )
If printing fails, the value
-1
is returnedotherwise, it returns the number of characters printed (i.e., the number of characters that make up the printed string)
printf("string_with_format_specifiers", variables_associated_to_specifiers)
string_with_format_specifiers
: formatted string is sent to the screen (stdout
)The specifiers within the string are replaced by their corresponding associated values
Format specifiers
%[param_no$][flags][width][.precision]conversion
[param_no$]
It allows specifying which variable to associate with a format specifier (based on the order in which the variables are passed).
Valid only on systems based on the POSIX standard
[flags]
-
Left-aligned
+
Add the +
symbol for positive values
' '
Add a space for positive values
0
Pad with 0
at the beginning
#
Always print the decimal point, even if there are no digits after it (only for float
and double
)
[width]
Number of characters that should be written for that particular format specifier
[.precision]
In the case of float and double, it specifies the number of digits after the decimal point
conversion
| s
| char[]
| string |
| c
| char
| character |
| d,i
| short
, int
, long
, long long
, char
| signed integer in decimal notation. Add l
for long and ll
for long long |
| u
| short
, int
, long
, long long
, char
| unsigned integer in decimal notation. Add l
for long and ll
for long long |
| x
| short
, int
, long
, long long
, char
| integer in hexadecimal notation (x
lowercase, X
uppercase) |
| e
| float
, double
| decimal number in exponential notation |
| f
| float
, double
| decimal number in decimal notation |
| a
| float
, double
| decimal number in hexadecimal fractional notation with base-2 exponent (a
lowercase, A
uppercase) |
| p
| pointer | memory address stored in a pointer |
scanf
int scanf(const char *format, arg0, …, argN )
It returns the number of values (among those specified) that were actually read by the method from stdin
scanf("string_with_format_specifiers",variables_associated_to_specifiers_addresses)
The input to
stdin
must be a string formatted asstring_with_format_specifiers
The values entered in the positions occupied by the specifiers within the string must respect both the format of the string and the type of the specifier
The values corresponding to the specifiers are assigned to the variables whose addresses are passed to
scanf
Whitespace characters (space, tab, etc.) are, in most cases, ignored
There is no concept of precision in numeric conversion
Format Specifiers
%[width].conversion
[width]
: maximum number of characters that must be written for that particular specifier
Examples
int m,n;
float f;
scanf("%d%f&d", &m, &f, &n);
If the user inserts 10.3 4 6
as input, the scanf interprets:
m = 10
f = 0.3
n = 4
if (scanf("%d", &num) != 1)
{
/* Display error message. */
}
If a value in decimal format is not provided as input, an error message is displayed.
sprintf
int sprintf( char* s, const char *format, arg0, …, argN)
Populates the string
format
with the variablesarg0,...,argN
and writes it in the strings
Returns the number of characters written to
s
(excluding the null terminator)
sscanf
int sscanf( char* s, const char *format, arg0, …, argN)
Reads input from the string
s
Returns the number of specified variables read from
s
and stored
getchar
int getchar(void)
Reads a character from
stdin
and returns itIf there is a read error (
stdin
error), it returnsEOF
putchar
int putchar(int c)
Sends the character
c
tostdout
and returns the character writtenIf there is a write error (
stdout
error), it returnsEOF
gets
char *gets(char *s)
Reads a full line from the keyboard via
stdin
Returns the read line in
s
(the newline character is not included in the string)If there is a read error (
stdin
error), it returnsNULL
puts
int puts(const char *s)
Sends the string
s
tostdout
(with the addition of a newline )Returns a non-negative number if the write is successful
If there is a write error (
stdout
error), it returnsEOF
Last updated