The standard console output function in C is printf. Its description is contained in the stdio.h header file. Using this function, you can display data or user messages to the console. C is case sensitive. For example, the two functions printf and scanf are different from the likes of Printf and Scanf. All characters in the printf and scanf functions must also be lowercase. One of the simplest examples of C printf to display the familiar hello world greeting is:
Defining printf group functions in stdio.h
The file "stdio.h" refers to the standard input / output library in C. The description of printf and similar functions is given in it as follows:
The listed functions load data from certain locations, convert them to a character string, and send them to the specified output streams.
Printf family of functions
The functions of the printf group in C serve for processing and formatted output of data to a standard stream. Moreover, the printf and vprintf functions write to the standard stdout stream, the fprintf and vfprintf functions send the values ββof the output arguments to some given output stream, and snprintf, sprintf, vsnprintf and vsprintf write data to the character string. All of these functions work using a format string that indicates the necessary argument conversions for output.
The fprintf function writes the result to the stream output stream. The sprintf function outputs to the buffer, which is a character string. The behavior of the function is not defined if the string to be output exceeds the size of the buffer array.
The snprintf function, like the previous one, writes data to a string buffer. The resulting character string ends with a null character, unless bufsz (buffer size) is non-zero. Otherwise, if bufsz is zero, then nothing is output to the buffer, and the buffer itself may well be a null pointer, but the return value (the number of bytes that should have been written) is still calculated.
The printf_s function generally acts in the same way as printf, with one exception. The main difference in the description of printf_s in C and printf is the following: the printf_s function checks the format string for valid characters, unlike printf, which only checks the format string for a null pointer.
Let's take a closer look at the printf function.
general description
In C, characters are printed through standard output by calling the printf function. The printf command in C formats the output data set, and it is sent to the standard output stream stdout. The values ββpassed as arguments to the function are displayed in the console according to the specified format string, which in turn contains two varieties of elements. The first variety is the characters that are displayed on the screen, and the elements that determine the features of the data format and are responsible for the method of representing the arguments in the output are of the second type.
When outputting variables from printf to C, special combinations of characters in the argument string are replaced with data converted in accordance with these characters, and for each data type there are its own specifications for the output format.
Function Type and Return Value
The printf function of type int returns an integer value indicating the number of characters printed on the screen. For example, you can assign:
int k = printf ("Hello% c% d% s", 'a', 11, "everyone!"),
and then the value of the variable k easily determines whether an error occurred during the output. If a negative value is returned (if the function returned "-1"), we can conclude that an error occurred during its execution.
Syntax and Dependencies
To use the printf function, you need to include the header file "stdio.h" as follows:
#include <stdio.h>
The function template looks like:
int printf (const char * format, ...)
Ellipsis refers to a list of arguments to be output. The printf function can be used with a different number of arguments, but the first one is always limited to double quotes on both sides, and each subsequent one must be separated from the previous comma. What is written in double quotes and is not a format specification is printed without changes, otherwise, if a specifier is encountered, the type of its value is copied.
Format specification task form:
% [flags] [width] [. position] [length] type
Formatting with printf to C output values
Reading the format string indicated in parentheses after the name of the called function occurs only in one direction: from left to right, and the first argument specified after this line itself is displayed only if the first specification is encountered. Until the format string ends, the specifications specified in it will initiate the conversion and printing of subsequent arguments. In the format string, the space character is considered as a regular character and is transmitted to the output in cases when it is not used in the expression of the format specification.
The "%" symbol indicates the beginning of the output format specification, followed by the format code. All fields in the specification are separate, defining the formatting conditions for numbers or characters.
The formatted printf output in C has its own characteristics. If the number of arguments listed exceeds the number of format specifications, they are skipped and not displayed. Otherwise, if there are more format specifications than the values ββin the list of arguments to be printed, the result of the function call is not determined.
For the explicit indication that the argument should be used in a count, it is possible to use "% m $" instead of "%" and "* m $" instead of "*", and m, an integer decimal value, indicates the position of the desired argument (indexing starts with one) .
Parameters
stream | The output stream for writing to a file |
buffer | Pointer to a character string for subsequent writing to it |
bufsz | Defines the number of characters allowed for writing: the maximum value is bufsz-1, and also a zero delimiter |
format | A pointer to a multibyte string with a null delimiter that defines how to interpret the output arguments |
Flags used in format stringFlag | Description |
- | Left alignment of the result in the output field |
+ | When a numerical value with a sign is displayed, β+β is forcibly printed before a positive value (by default, only β-β is displayed before a negative value) |
0 | For integers and floating-point numbers, leading zeros are used instead of the space characters to fill left digits in case the field width is specified that exceeds the length of the number. For integers, the flag is ignored if precision is explicitly specified. For other conversions using this flag, the behavior of the function is undefined. Flag "0" is not taken into account if flag "-" is present |
space | If the result of the output of an expression having a sign does not start with the symbol of this sign or is empty, then a space is added to the result. The space flag is ignored if the + flag is present. |
# | Alternate conversion form in progress |
Escape sequencesSequence | Result |
\ a | Sound signal |
\ n | Newline |
\ r | Returning the cursor to the beginning of the line |
\ t | Tab |
\ v | Vertical tab |
\ " | Double quote output |
\\ | Slash output |
Various format specifiers
Format specifier | Usage and description for printf C | Argument type |
% | The literal entry "%" | |
c | Single character output. The argument is converted to unsigned char. When using the modifier "l", the argument is converted to a string of characters | unsigned char |
s | Print a string of characters. The argument must be a pointer to the starting element of the char character array | char * |
d i | Signed decimal notation | int |
o | Output octal representation without signed integer value | unsigned int |
x X | Output a hexadecimal representation without a signed integer value. The characters "a", "b", "c", "d", "e", "f" are used to convert "x". And for the conversion of "X" - "A", "B", "C", "D", "E", "F" | unsigned int |
u | The output of the decimal conversion without a signed integer value. If the converted value and precision together are 0, then no characters are displayed | unsigned int |
f F | Output a decimal representation of a floating-point number that has a sign | double |
e E | The output of the decimal exponential representation of a floating-point number, rounded and transformed so that one digit remains before the comma, and the number of digits after the decimal point corresponds to the accuracy of the representation (by default, accuracy is 6, and if 0 is specified, the comma character is not displayed at all). The character "e" is displayed in upper or lower case depending on the conversion | double |
a A | Output a hexadecimal representation of a floating point number | double |
g G | Outputting a decimal representation of a floating point number or its decimal exponential representation depending on the value and accuracy | double |
n | Returns the number of elements printed by the printf function. The result is written to the variable pointed to by the argument. The specification may not contain flags, field width or precision | int * |
p | Pointer output | void * |
Field width modifier
An integer can be written in the format string in printf C after the percent sign and before the format command. It is a field width modifier and affects the presentation of the displayed data. The smallest field width intended for a value is determined by this number, and the presence of such a modifier, if the argument is less than the field allocated to it, causes spaces or zeros to be added to the result. The default placeholder is the space character, but you can set it to zero by writing it before the width specification. The modifier indicates the minimum width, and any value that exceeds this minimum will be printed without interference. A number, for example, consisting of less than eight characters and printed with the specification "% 08d", will be padded to the required eight characters with zeros.
Similar modifiers can also specify precision or alignment parameters.
Precision modifier
The precision modifier is used to determine the number of decimal places to print in the number representation. To add an accuracy modifier, you must put a dot after the specification of the field width and specify the desired accuracy value after it. The precision modifier is defined for the "e", "f", "a", "E", "A" and "F" formats. For integers, the modifier sets the number of displayed digits, adding zeros to the left digit, if necessary, and when outputting rational numbers, determines the required number of decimal places. In relation to string variables: the next number after the point in the precision modifier serves as a determinant of the maximum field length during output. For example, for a given specification of the format "% 4.8s" a string will be displayed whose length is in the range of four to eight characters, in case of exceeding the extreme characters will be omitted.
Other format modifiers
The default alignment is right justification, however this can be changed by putting a β-β sign after β%β. This format specification sets left alignment.
In addition, the printf function is able to distinguish between short and long types of output integer values. Valid qualifiers are: "o," "d," "u," "i," "x," and "X." The long value type is set by the modifier "l", and the short one is set by the modifier "h". For example, when outputting a long integer and a value of type short unsigned int, the format specifications look like "% ld" and "% hu", respectively.
Length | Description |
h | For types short or unsigned short |
l | For types long or unsigned long |
L | For type long double |
Examples
1. Description of printf C and the results of calling each function:
2. Displaying a simple dialog message:
printf ("message");
3. Program code:
Expressions specified in double quotation marks before format specifications are printed on the screen, as well as the arguments following the format string. The result of the above printf C functions, description of the output:
This example illustrates the output to the screen using various line formats, integer variables, characters, and floating point numbers.
Standard scanf input function and examples of its use
The scanf function is used to read keyboard input. The description of printf and scanf in C is provided in the header file "stdio.h".
scanf ("format specifiers", & value1, & value2, ...);
A simple example of working with the scanf function:
#include <stdio.h>
int main () {
int a;
float b;
scanf ("% d% f", & a, & b);
}