概述
文章目录
- 1. 格式化输出
- 1.1 printf
- 1.2 fprintf
- 1.3 sprintf
- 1.4 snpirntf
- 1.5 函数手册
- 2. 格式化输入
- 2.1 scanf
- 2.2 fscanf
- 2.3 sscanf
1. 格式化输出
1.1 printf
int printf(const char *format, ...);
格式化字符串输出到 标准输出流
1.2 fprintf
int fprintf(FILE *stream, const char *format, ...);
格式化字符串输出到 文件流
1.3 sprintf
int sprintf(char *str, const char *format, ...);
格式化字符串 存放到内存缓冲
1.4 snpirntf
int snprintf(char *str, size_t size, const char *format, ...);
格式化字符串 存放到内存缓冲(带有边界检查)
1.5 函数手册
PRINTF(3) Linux Programmer's Manual PRINTF(3)
NAME
printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf,
vdprintf, vsprintf, vsnprintf - formatted output conversion
SYNOPSIS
#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int dprintf(int fd, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
#include <stdarg.h>
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vdprintf(int fd, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
snprintf(), vsnprintf():
_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE ||
_POSIX_C_SOURCE >= 200112L;
or cc -std=c99
dprintf(), vdprintf():
Since glibc 2.10:
_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
DESCRIPTION
The functions in the printf() family produce output according to a for‐
mat as described below. The functions printf() and vprintf() write
output to stdout, the standard output stream; fprintf() and vfprintf()
write output to the given output stream; sprintf(), snprintf(),
vsprintf() and vsnprintf() write to the character string str.
The function dprintf() is the same as fprintf(3) except that it outputs
to a file descriptor, fd, instead of to a stdio stream.
The functions snprintf() and vsnprintf() write at most size bytes
(including the terminating null byte ('