C语言,scanf问题与char的 [英] C Language, scanf issue with char's

查看:286
本文介绍了C语言,scanf问题与char的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c创建一个非常基本的程序。我使用scanf从控制台获取输入,但是当我使用它的char它只是似乎跳过它。

I am using c to create a very basic programme. I using scanf to get input from the console but when I use it with a char it just seems to 'skip' it.

这是代码:

#include <stdio.h>
#include <string.h>


int main(){

    char name[20];
    char yn;

    printf("Welcome to Benjamin's first C programme! \n\n");


     do{

         printf("What is your name? \t");
         scanf("%s", name);
         printf("\n");

         printf("Is your name %s [y/n]?", name);
         scanf("%c", &yn);
         printf("\n");

     } while(yn != 'y');
}


推荐答案

第二个字符串格式 scanf()

scanf(" %c", &yn);

此空格将会在第一个输入后接受您输入的换行符

This space will catch the newline that you type after the first input

这篇关于C语言,scanf问题与char的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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