惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - Lunais

恢复 git stash drop 丢失的内容 Git技巧:彻底重置本地仓库与远程同步,同时保留Stash内容 信源编码和信道编码区别 Leetcode 一组数据差值之和最大 5G 3gpp协议 eclipse调试配置 find查找 浮点类型(float、double)在内存中的存储 yield [基础函数]memset用法 python2处理表格文件按照规则多行输出(中文路径,print) linux系统objdump输出动态库.so文件和静态库.a中符号表 python复制删除文件&修改目录&执行bat(wins) Linux C下的正则表达式 kill eclipse C语言之表达式运算整体提升 查找函数对比:findall,search,match Linux backtrace() git本地协同
scanf
Lunais · 2023-11-28 · via 博客园 - Lunais

在C语言中,scanf函数从标准输入(键盘)读取用户输入的数据。这是一个常用的函数,但也有很多注意事项和细微差别需要了解。

1.基本语法

int scanf(const char *format, ...);
参数:

format
:一个字符串,指定输入的格式。
...
:可变参数,根据格式字符串来确定类型和数量。

2.返回值:

该函数返回读取并成功转换的项目数量。如果发生错误或到达文件末尾,则返回一个负值。

例子:

int a;

float b;

char c[10];

scanf("%d %f %s", &a, &b, c);

在这个例子中,我们读取三种类型的数据:一个整数、一个浮点数和一个字符串。每个数据类型都需要一个对应的变量,并且使用 &操作符来获取变量的地址,因为 

scanf 需要指向内存中的位置存储读取的数据。

注意:

  • 在读取字符串时,scanf 会读取空格之前的所有字符,直到遇到换行符或者空格。因此,如果你的输入中包含多个单词,它们会被视为一个整体。
  • 如果你想读取多个单词,1.可以使用多个 %s 格式,每个格式后面都跟一个空格。2.配合while语句与scanf的返回值。
  • char d[10], e[10];
    scanf("%s %s", &d, &e);
  • char str[20];
    while(scanf("%s",str) == 1)//sfs sett fd a ,死循环不会停止
    {
        printf("%s ",str);//依次输出sfs sett fd a
    }
  • 在读取数字时,scanf会读取空格之前的数字,直到遇到换行符或者空格。
  •      int n, i;
         printf("Enter the number of integers: ");
         scanf("%d", &n);//终端输入3 4 5 6时,读取n的值遇到空格停止,此时n==3;其余的4 5 6仍然保存在缓冲区中,等待下一个scanf语句
         int a[101];
         printf("Enter %d numbers separated by spaces: ", n);
         for(i = 0; i < n; i++) 
        {
             scanf("%d", &a[i]);//a[i]依次保存了数字4,5,6
         }
  • scanf 在读取数据时是非常容易出错的。如果用户输入的数据类型与你在 scanf 中指定的不匹配,程序可能会崩溃或者出现未定义的行为。因此,始终要检查 scanf 的返回值,以确保读取的数据是正确的。在读取字符串时,scanf会自动添加一个空字符 \0来结束字符串。因此,你需要为字符串分配足够的内存,以便存储这个空字符

 3.一串以空格分隔的数字和字符串的读取

case1:利用非format格式的字符(串)作为结束标志退出scanf

i.e.   12 24 223

while (scanf("%d", &number) == 1)
{
  printf("You entered: %d\n", number);
}

3个数字都满足%d,scanf成功读取后,会等待终端继续输入,无法退出

在: 12 24 223 abc 情况下 abc不是%d类型,会返回0而退出while,此时abc就是一个非format格式的字符(串)的结束标志

case2:利用formal格式的数字作为结束标志退出scanf

i.e. 12 24 223 0

while (scanf("%d", &number) == 1)
{
  if(0 == number) break;//遇到结束标志符退出
  printf("You entered: %d\n", number);
}

case3:利用数字个数n控制读取的数字个数

 i.e.   3 12 24 223 //第一个数字表示,后续数字的个数

int n,m;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
  scanf("%d", &m);
}

case4:读取一串以空格分隔的字符串()

i.e. saf dsfeg ljlk

char str[100];
float arr[100];
printf("请输入一段文本(不超过99个字符):");
fgets(str, 100, stdin); // 从标准输入读取字符串
printf("你输入的是:%s\n", str); // 打印读取的字符串
str[strlen(str)-1] = '\0';//处理掉fgets读取的\n
int sum = 0, count = 0;
char *token;
char *saveptr;
for (token = strtok_r(str, " ", &saveptr); token; token = strtok_r(NULL, " ", &saveptr))
{
  printf("%s\n", token);
  arr[count] = atof(token);
  sum += arr[count];
  count++;
}