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

查看:22
本文介绍了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("
 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天全站免登陆