scanf("%3c", &c);

My girlfriend CC asked me a question about C programming language today, “What does scanf("%mc", &c) mean?” When I heard this question, I immediately thought that it should resemble printf("%mc", c), which means writing character c in m-chars width to standard output. Therefore, scanf("%mc", &c) should mean reading character c in m-chars width from standard input and I wrote a simple test file as following:

# include<stdio.h>int main(){char c;scanf("%3c", &c);printf("%c\n", c);return 0;
}

Really simple, right? And it passed the compilation perfectly, no errors and no warnings, under gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1). Just run it. However, after I input abc and clicked Enter, I got the following output:

a
*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)

a was easy to understand, but what was the meaning of the next two lines? It seemed that I got into a big problem. So I googled it and got one possible reason from “*** stack smashing detected ***: ./a.out terminated Aborted (core dumped)” - array inserion. scanf("%3c", &c); tried to write something into unallocated memory space. From Stack smashing detected, we could know scanf("%3c", &c); triggering the protection mechanism used by gcc is the most possible reason for this error.

In conclusion, scanf() just reads something from standard input into memory without checking the buffer overflow. Therefore we must be careful when using it to avoid some unexpected errors.


I tried to see the neighbors of c using the following code.

#include <stdio.h>int main(){char c;scanf("%3c", &c);char *p = &c;printf("%d,%d,%d\n", *(p - 1), *p, *(p + 1));return 0;
}

Run it, input abc and click the Enter. I saw:

0,97,-81
  • Where does -81 come from?
  • Why no “terminal”?

With the two questions, I reran the code several times with the same steps. Ans I found that the output was always like 0,91,x where x is an integer approximately between -128 and 127. Moreover, the following code had the same effect with the above, in which I modified scanf("%3c", &c); into scanf("%c", &c);.

#include <stdio.h>int main(){char c;scanf("%c", &c);char *p = &c;printf("%d,%d,%d\n", *(p - 1), *p, *(p + 1));return 0;
}
  • Let’s answer the first question. As the memory space behind the pointer p points was unallocated, its value stayed unsure. When we tried to printf its value in decimal, we could see the value between -128 and 127, the decimal domain of an 8-bit.
  • As for the “terminal”, I cannot figure it out. I just created a pointer to c and the error disappeared. Maybe it just didn’t trigger the protection mechanism used by gcc.

scanf(3c,c);相关推荐

  1. scanf(“%3c%3c“, a, b)

    问题 在书中看到这样的描述: 我们按照这个来操作一下: 跟我们期望中有点不一样,我们希望他的结果是:a = a b = d,为什么会这样呢 ? 解答 这跟a b在内存中的地址是有关的 我们先看一下a ...

  2. c语言scanf %4c,scanf(%3c%4c,a,b);当输入ABCDEFGH时 a b的值分别是?怎么来的?

    满意答案 538084otb 2017.02.21 采纳率:53%    等级:8 已帮助:61人 scanf在不同编译器上传参顺序不一样,大部分都自右向左 ,但有些编译器我无 法找到规律 scanf ...

  3. linux系统中scanf函数,Linux下scanf宽度控制问题

    scanf在不同编译器上传参顺序不一样,大部分都自右向左 ,但有些编译器我无 法找到规律 scanf("%3c%2c",&ch1,&ch2); printf(&qu ...

  4. c语言%3c与 的优先级,C语言程序设计第3章顺序程序设计.ppt

    C语言程序设计,主讲:王奇志,,4 课时,第3章 顺序程序设计,<C语言程序设计>,导航目录,知识点.重点.难点,知识点: 数据的存储 C中常见的类型 变量和常量 运算符和表达式 重 点: ...

  5. c语言中1%3c%3c2 什么意思,c语言练习题

    篇一:C语言考试题库及答案 C语言理论上机考试选择题部分(共200题,仅针对11级定向专业) 1.下面程序的输出是___D______ #include void main() { int k=11; ...

  6. java的 %3c%3c 运算符_scanf(%3c%3c, a, b)

    问题 在书中看到这样的描述: 我们按照这个来操作一下: 代码 #include int main(int argc, char const *argv[]) { char a, b; scanf(&q ...

  7. c+语言:1%3c%3c,C语言教程第4章1课件

    第4章最简单的C程序设计-顺序程序设计,4.1C语句概述,分五类:1.空语句;(考点),空语句也可用作循环语句中的循环体,表示循环体什么也不做.这一点将在第6章详细讲解.,2.9种控制语句if()~e ...

  8. c语言scanf函数作用,C语言scanf函数应用问题解答

    C语言scanf函数应用问题解答 C语言的输入是由系统提供的库函数完成的.scanf函数是C语言中最常用且功能最强的输入函数,但该函数如使用不慎,就会出现错误或得不到预想的结果.以下结果都是基于VC+ ...

  9. c oracle代码,实现单一源代码 - Oracle® Developer Studio 12.5:C 用户指南

    8.2 实现单一源代码 以下各节介绍可用于编写支持 32 位和 64 位编译的单一源代码的一些可用资源. 8.2.1 派生类型 使用系统派生类型使代码对于 32 位和 64 位编译环境均安全,这是一种 ...

最新文章

  1. 号外号外 你和python大牛的差距有多少?
  2. R语言构建xgboost模型:使用xgb.DMatrix保存、加载数据集、使用getinfo函数抽取xgb.DMatrix结构中的数据
  3. 英特尔第七任CEO敲定 斯旺为何受到董事会青睐?
  4. boost::to_string用法的测试程序
  5. 运维监控之Nagios实战(五)Nagios QL3.11
  6. 类和对象编程(八):指向类的指针
  7. 8位可控加减法电路设计_C++手撕底层:位、字节、原码、反码、补码的深入理解...
  8. Sql server 行列转换
  9. 嵌入式可视化编程软件选哪个好?(可视化编程平台介绍、测评与选择)【Scratch、Mind+、Mixly】
  10. 管理学定律七:责任分散效应与帕金森定律
  11. 2018毕业设计需求分析
  12. Win 开机自动启动 WSL 服务
  13. 如何在web配置一个高效采集数据的DTM?
  14. 在东京,月薪25万日元能过上什么样的日子啊
  15. 快速隐藏所有桌面图标快捷键是什么?
  16. Java数据结构与算法
  17. 2016年中山市信息学竞赛暨全国信息学联赛成绩表(普及组)
  18. HPE MSA存储的高性能连接解决方案
  19. RFB与RDP的区别
  20. 计算机专业英语(二)缩略词全称与解释

热门文章

  1. rtx2060什么水平_老平台升级RTX2060,——性能,功耗,静音都一步到位!
  2. 单片机工程师面试题小计
  3. 红外额温枪方案开发技术支持
  4. TaWRKY19/61/82激活糖转运蛋白TaSTP3从而增强小麦条锈病敏感性
  5. VISTA注册ActiveX控件出现 0x80040200的处理方法
  6. 阿里云windows服务器重置密码并连接远程桌面
  7. 尖峰在线培训 http://www.jianfengedu.com/
  8. 多个excel合并到一个excel的不同sheet中
  9. 纤亿通的光模块通常需要经过哪些测试流程?
  10. TexturePacker的用法