为什么不通过scanf_s存储char值,它取值为'\0' [英] why are not store char value by scanf_s its taking the value is '\0'

查看:87
本文介绍了为什么不通过scanf_s存储char值,它取值为'\0'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i我在visual 2012.i工作有问题,请帮助我。



hi all,

i am working in visual 2012.i have a problem,please help me.

#include "stdafx.h"
#include "conio.h"
#include "stdio.h"

int _tmain(int argc, _TCHAR* argv[])
{
    char x;
    printf("Enter Data\n");
    scanf_s("%c",&x);
    fflush(stdin);
    printf("%c",x);

    return 0;
}





当我给任何一个char值时,它取'\ 0'并且不打印任何东西。很开心在这里。

如何存储char值。请帮助我。



谢谢



when i am giving any char value its taking '\0' and does not print anything.what happning here.
how store a char value.please help me.

Thanks

推荐答案

请参阅文档: MSDN scanf_s [ ^ ]并提供附加长度参数(如示例中所示......)

See the documentation: MSDN scanf_s[^] and provide the additional length parameter (as they show in the example...)
printf("Enter Data\n");
scanf_s("%c",&x, 1);
fflush(stdin);


你的问题不是很清楚,但我想你是说你输入了一个值并试图回声它与printf()...



首先,你的scanf_s()语句缺少一个长度字段作为第三个参数。语法规则是这里 [ ^ ]和其他 [ ^ ]。该行应该是

Your question is not quite clear, but I think you are saying that you input a value and are trying to echo it with printf()...

First of all, your scanf_s() statement is missing a length field as the third parameter. Syntax rules are here[^] and here[^]. The line should be
scanf_s("%c", &x, sizeof(x));



您可能还想在printf语句的末尾添加'\ n'。我会这样做:


You probably also want to add a '\n' at the end of the printf statement. I would do it as:

printf("Input char is <%c>\n", x);


这篇关于为什么不通过scanf_s存储char值,它取值为'\0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆