Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanf causes C program to crash

Tags:

c

crash

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
like image 738
BKCOHEN Avatar asked Dec 07 '22 18:12

BKCOHEN


1 Answers

You must pass the address of the variable to scanf:

 scanf("%c", &answer);
like image 51
Tudor Avatar answered Dec 24 '22 10:12

Tudor