Input/Output

From <stdio.h>
printf
int printf( const char *format, arg0,…,argN )If printing fails, the value
-1is 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
stdinmust be a string formatted asstring_with_format_specifiersThe 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
scanfWhitespace 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 = 10f = 0.3n = 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
formatwith the variablesarg0,...,argNand writes it in the stringsReturns 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
sReturns the number of specified variables read from
sand stored
getchar
int getchar(void)Reads a character from
stdinand returns itIf there is a read error (
stdinerror), it returnsEOF
putchar
int putchar(int c)Sends the character
ctostdoutand returns the character writtenIf there is a write error (
stdouterror), it returnsEOF
gets
char *gets(char *s)Reads a full line from the keyboard via
stdinReturns the read line in
s(the newline character is not included in the string)If there is a read error (
stdinerror), it returnsNULL
puts
int puts(const char *s)Sends the string
stostdout(with the addition of a newline )Returns a non-negative number if the write is successful
If there is a write error (
stdouterror), it returnsEOF
Last updated