Scanf函数输入字符串

#define _CRT_SECURE_NO_WARNINGS   //#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h> int main()
{
char a[] = { };
scanf("%s", a);
char b[] = { };
scanf("%s", b);
char c[] = { };
int index = ;
while (a[index])
{
c[index] = a[index];
index++;
}
int index_b = ;
while (b[index_b])
{
c[index + index_b] = b[index_b];
index_b++;
}
printf(" %s\n ",c); system("pause");
return ;
}

运行结果:

scanf()在vs2015上会提示安全问题,两种方法屏蔽

第一种:#define _CRT_SECURE_NO_WARNINGS  第二种:#pragma warning(disable:4996)

为什么提示不安全,不让我们用scanf()这个函数呢?
看例子:
#define _CRT_SECURE_NO_WARNINGS   //#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h> int main()
{
char c[] = { };
scanf("%s",c);
printf("%s",c); system("pause");
return ;
}

当我输入超出了数组范围的值的情况下:

Run-Time Check Failure #2 - Stack around the variable 'c' was corrupted.  翻译:运行时检查失败# 2 -堆栈变量“c”是损坏的。
这就是“缓冲区溢出”

Scanf函数输入字符串的更多相关文章

  1. 关于C中scanf()函数读取字符串的问题

    #include <stdio.h> int main(void) { ]; scanf("%s", s_name); printf("Hello, %s!\ ...

  2. 为什么C语言Scanf函数对字符串不要加 取地址运算符&

    原文1:http://www.360doc.com/content/16/0515/11/19455598_559288667.shtml 原文2:https://zhidao.baidu.com/q ...

  3. scanf函数之扫描集

    前言 %[]是scanf函数不常用到的格式字符--扫描集(scanset),它的用法很简单,但却能巧妙地解决一些输入问题.(由于书籍里和网上对此格式字符的介绍很少且篇幅较小,本文可能会有些许错误,请读 ...

  4. C语言gets雨scanf函数的用法

    /*1.不同点: scanf不能接受空格.制表符Tab.回车等: 而gets能够接受空格.制表符Tab和回车等: 2.相同点:  字符串接受结束后自动加'\0'. 使用scanf("%s&q ...

  5. C语言Scanf函数

    C语言的scanf函数 一.变量的内存分析 (一)字节与地址 ①. 内存以字节为单位 每个字节都有自己的内存地址,根据地址就可以找到该字节.整个内存相当于一整个酒店,而酒店以房间为单位,在这里每个房间 ...

  6. C++入门经典-例2.5-使用附加格式说明scanf函数的格式输入

    1:代码如下: // 2.5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" int main() { long iLong; /*长整型变 ...

  7. scanf函数与输入缓冲区

    本文链接:http://www.cnblogs.com/xxNote/p/4008668.html 今天看书的时候遇到scanf函数与缓冲区的问题,产生了一些猜想即:应该有一个指针来记录缓冲区中读取到 ...

  8. 38 写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

    题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. public class _038PrintLength { public static void main(Stri ...

  9. 写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度

    import java.util.Scanner; /** * [程序38] * * 题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. * * @author Jame ...

随机推荐

  1. Linxu IO测试软件

    fio 安装 apt-get install fio fdisk -l Device Boot Start End Blocks Id System/dev/sda1 * 2048 968390655 ...

  2. 字符串--java中判断字符串是否为数字的方法的几种方法?

    ava中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...

  3. ubuntu16.04解决播放swf视频文件问题

    使用下面 sudo apt-get install swfdec-gnome

  4. xml中的非法字符

    今使用Jdom生成xml文件的时候,总是出现0x0,0x8为非法字符,经过搜索,问题原因及解决方法如下: 原因:xml中需要过滤的字符分为两类,一类是不允许出现在xml中的字符,这些字符不在xml的定 ...

  5. NGUI之UILabel

    重要属性说明 overflow: Shrink Content: 意味着内容会自动被缩小以便适应区域.它在你使用动态字体的同时使用Keep      Crisp设置时有效,让字体变小,而不是缩放内容. ...

  6. 8. String to Integer (atoi)

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  7. 51. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  8. 多个字段用and和or时要注意用括号。

    多个字段用and和or时要注意用括号. 新技能get! create table wly_test (name1 varchar2(10),number1 number(6),score1 numbe ...

  9. JavaScript笔记基础篇(一)

    一. 常用正则表达式汇总以及部分问题解决方案 正则匹配: var str = "This is my test"; var test = new RegExp("test ...

  10. stl 存放对象析构问题

    vector内数据使用结构体的话是深拷贝,vector内的数据会拷贝一份保存,vector内数据不会丢失.如果vector内数据是指针的话是进行浅拷贝,数据超出作用域后会自动析构,vector内所指向 ...