为什么输入的字符串不起作用? [英] Why does not the input of a string work?

查看:84
本文介绍了为什么输入的字符串不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
int main()
{
        char temp[1024];
        if(getchar() != 'y')
        {
                printf("no options\n");
                return 1;
        }
        scanf(temp, "%s");
        printf("%s", temp);
}

我得到如下片段.我只想从用户输入两次.但是第一个输入有效,但是第二个输入直接跳过并且printf("%s", temp);打印意外字符.我该如何解决问题. 谢谢

I get snippet as below. I just want twice input from user. but the first input works, however the second directly skips and the printf("%s", temp); printed unexpected characters. How can I resolve the problem.. thanx

推荐答案

scanf的第一个参数是格式,第二个参数是缓冲区.你有倒退.尝试scanf( "%s", temp );.

The first parameter to scanf is the format, and the second is the buffer. You have it backwards. Try scanf( "%s", temp );.

这篇关于为什么输入的字符串不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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