[Solved] Simple C code keeps crashing


scanf("%s\n", playerName); is wrong because %s call for char* data but playerName here is type char.

You have to make playerName an array of characters and set max length of input to avoid buffer overflow.

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

int main(void)
{
    char playerName[1024];
    int playerAge;

    printf("What's your name, and your age?\nYour name: ");
    scanf("%1023s\n", playerName); /* max length = # of elements - 1 for terminating null character */
    printf("Your age: ");
    scanf("%d\n", &playerAge);
    printf("Okay %s, you are %d years old!", playerName, playerAge);

    return 0;
}

solved Simple C code keeps crashing