C언어 > printf, fprintf, sprintf, snprintf, printf_s, fprintf_s

TODAY753 TOTAL2,273,004
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | C언어 | C언어 | printf, fprintf, sprintf, snprintf, printf_s, fprintf_s

페이지 정보

작성자 키트 작성일2017-09-12 13:50 조회1,794회 댓글0건

본문

 

printf, fprintf, sprintf, snprintf, printf_s, fprintf_s

Defined in header <stdio.h>
(1)
int printf( const char *format, ... );(until C99)
int printf( const char *restrict format, ... );(since C99)
(2)
int fprintf( FILE *stream, const char *format, ... );
(until C99)
int fprintf( FILE *restrict stream, const char *restrict format, ... );
(since C99)
(3)
int sprintf( char *buffer, const char *format, ... );
(until C99)
int sprintf( char *restrict buffer, const char *restrict format, ... );
(since C99)
int snprintf( char *restrict buffer, int bufsz, 
              const char *restrict format, ... );
(4)(since C99)
int printf_s(const char *restrict format, ...);
(5)(since C11)
int fprintf_s(FILE *restrict stream, const char *restrict format, ...);
(6)(since C11)
int sprintf_s(char *restrict buffer, rsize_t bufsz,
              const char *restrict format, ...);
(7)(since C11)
int snprintf_s(char *restrict buffer, rsize_t bufsz,
               const char *restrict format, ...);
(8)(since C11)

Loads the data from the given locations, converts them to character string equivalents and writes the results to a variety of sinks.

1) Writes the results to the output stream stdout.
2) Writes the results to the output stream stream.
3) Writes the results to a character string buffer. The behavior is undefined if the string to be written (plus the terminating null character) exceeds the size of the array pointed to by buffer.
4) Writes the results to a character string buffer. At most bufsz - 1 characters are written. The resulting character string will be terminated with a null character, unless bufsz is zero. If bufsz is zero, nothing is written and buffer may be a null pointer, however the return value (number of bytes that would be written) is still calculated and returned.
5-8) Same as (1-4), except that the following errors are detected at runtime and call the currently installedconstraint handler function:
  • the conversion specifier %n is present in format
  • any of the arguments corresponding to %s is a null pointer
  • format or buffer is a null pointer
  • bufsz is zero or greater than RSIZE_MAX
  • encoding errors occur in any of string and character conversion specifiers
  • (for sprintf_s only), the string to be stored in buffer (including the trailing null) would be exceed bufsz
As all bounds-checked functions, printf_sfprintf_ssprintf_s, and snrintf_s are only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdio.h>.

Parameters

stream-output file stream to write to
buffer-pointer to a character string to write to
bufsz-up to bufsz - 1 characters may be written, plus the null terminator
format-pointer to a null-terminated multibyte string specifying how to interpret the data.

The format string consists of ordinary multibyte characters (except %), which are copied unchanged into the output stream, and conversion specifications. Each conversion specification has the following format:

  • introductory % character
  • (optional) one or more flags that modify the behavior of the conversion:
  • -: the result of the conversion is left-justified within the field (by default it is right-justified)
  • +: the sign of signed conversions is always prepended to the result of the conversion (by default the result is preceded by minus only when it is negative)
  • space: if the result of a signed conversion does not start with a sign character, or is empty, space is prepended to the result. It is ignored if + flag is present.
  • # : alternative form of the conversion is performed. See the table below for exact effects otherwise the behavior is undefined.
  • 0 : for integer and floating point number conversions, leading zeros are used to pad the field instead of space characters. For integer numbers it is ignored if the precision is explicitly specified. For other conversions using this flag results in undefined behavior. It is ignored if -flag is present.
  • (optional) integer value or * that specifies minimum field width. The result is padded with spacecharacters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when * is used, the width is specified by an additional argument of type int. If the value of the argument is negative, it results with the - flag specified and positive field width. (Note: This is the minimum width: The value is never truncated.)
  • (optional) . followed by integer number or *, or neither that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int. If the value of this argument is negative, it is ignored. If neither a number nor * is used, the precision is taken as zero. See the table below for exact effects of precision.
  • (optional) length modifier that specifies the size of the argument
  • conversion format specifier

The following format specifiers are available:

Conversion
specifier
ExplanationArgument type
length modifierhhh(none)llljztL
%writes literal %. The full conversion specification must be %%.N/AN/AN/AN/AN/AN/AN/AN/AN/A
c
writes a single character.

The argument is first converted to unsigned char. If the lmodifier is used, the argument is first converted to a character string as if by %ls with a wchar_t[2] argument.

N/AN/A
int
wint_t
N/AN/AN/AN/AN/A
s
writes a character string

The argument must be a pointer to the initial element of an array of characters. Precision specifies the maximum number of bytes to be written. If Precision is not specified, writes every byte up to and not including the first null terminator. If the l specifier is used, the argument must be a pointer to the initial element of an array of wchar_t, which is converted to char array as if by a call towcrtomb with zero-initialized conversion state.

N/AN/A
char*
wchar_t*
N/AN/AN/AN/AN/A
d
i
converts a signed integer into decimal representation[-]dddd.

Precision specifies the minimum number of digits to appear. The default precision is 1.
If both the converted value and the precision are 0 the conversion results in no characters.

signed char
short
int

댓글 0

조회수 1,794

등록된 댓글이 없습니다.

C언어HOME > C언어 > C언어 목록

게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기