Scanf导致c程序崩溃 [英] Scanf causes c program to crash

查看:187
本文介绍了Scanf导致c程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的问题导致我的整个程序在第一次输入时崩溃。如果我删除输入,程序工作正常,但一旦我添加scanf到代码,并输入输入的程序崩溃。

This simple issue is causing my entire program to crash during the first input. If I remove the input, the program works fine but once I add scanf into the code and enter the input the program crashes.

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

#define MAXEMPS 3


// stub program code
int main (void){
    char answer;

    do
    {

        printf("\n Do you have another(Y/N): ");
        scanf("%c",&answer);
    }while(answer == 'Y' || answer == 'y');

    getchar();
    printf("  Press any key ... ");
    return 0;
} // main


推荐答案

scanf的变量地址:

You must pass the address of the variable to scanf:

 scanf("%c", &answer);

这篇关于Scanf导致c程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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