Scanf函数输入字符串

Scanf函数输入字符串

#define _CRT_SECURE_NO_WARNINGS   //#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char a[100] = { 0 };
    scanf("%s", a);
    char b[100] = { 0 };
    scanf("%s", b);
    char c[200] = { 0 };
    int index = 0;
    while (a[index])
    {
        c[index] = a[index];
        index++;
    }
    int index_b = 0;
    while (b[index_b])
    {
        c[index + index_b] = b[index_b];
        index_b++;
    }
    printf(" %s\n ",c);

    system("pause");
    return  0;
}

运行结果:

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[10] = { 0 };
    scanf("%s",c);
    printf("%s",c);

    system("pause");
    return 0;
}

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

 

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

 

posted on 2016-12-18 18:43  zzdoit  阅读(823)  评论(0编辑  收藏  举报

导航