IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C - scanf -> 正文阅读

[C++知识库]C - scanf

C - scanf

int scanf ( const char * format, ... );

Read formatted data from stdin

Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments.
从 stdin 读取数据并根据 format 参数将它们存储到附加参数指向的位置。

The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.
附加参数应指向已分配的对象,其类型由 format 字符串中的相应格式说明符指定。

Return Value

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
成功时,该函数返回成功填充的参数列表的项目数。由于匹配失败、读取错误或到达文件末尾,此计数可能与预期的项目数匹配或更少 (甚至为零)。

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.
如果在读取时发生读取错误或到达文件末尾,则会设置正确的指示符 (feof or ferror)。并且,如果在任何数据可以成功读取之前发生任何一种情况,则返回 EOF。注意返回值是 int

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.
如果在解释宽字符时发生编码错误,该函数会将 errno 设置为 EILSEQ

Example

//============================================================================
// Name        : Yongqiang Cheng
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2020 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
	char str[80];
	int i;

	printf("Enter your family name: ");
	scanf("%79s", str);

	printf("Enter your age: ");
	scanf("%d", &i);

	printf("Mr. %s , %d years old.\n", str, i);
	printf("Enter a hexadecimal number: ");
	scanf("%x", &i);
	printf("You have entered %#x (%d).\n", i, i);

	return 0;
}

Enter your family name: yongqiang
Enter your age: 29
Mr. yongqiang , 29 years old.
Enter a hexadecimal number: ff
You have entered 0xff (255).
请按任意键继续. . .

在这里插入图片描述

Compatibility

Particular library implementations may support additional specifiers and sub-specifiers.
特定的库实现可能支持额外的说明符和子说明符。

Those listed here are supported by the latest C and C++ standards (both published in 2011), but those in yellow were introduced by C99 (only required for C++ implementations since C++11), and may not be supported by libraries that comply with older standards.
此处列出的内容受最新 C 和 C++ 标准 (均于 2011 年发布) 的支持,但黄色部分是由 C99 引入的 (自 C++11 以来仅 C++ 实现需要),并且可能不受符合标准的库的支持旧标准。

References

https://cplusplus.com/reference/cstdio/scanf/

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-29 11:27:16  更:2021-07-29 11:28:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 15:52:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码