Bot
Bot

Reputation: 59

My command window crashes after i input a name. C

So that's my code, but every time I run it the command window crashes right after I input a name. I'm using Visual Basic, so it gives me a warning when I try to use:

scanf() 

so instead i decided to use:

scanf_s

I also tried to change the %s in scanf_s("%s\n", name) to %c. It stopped crashing and executed the program but when it prints the name, it would just be a bunch of broken text.

#include <stdio.h>

int main(void) {
   char name[30];

    printf("Enter your name:\n");
    scanf_s("%s\n", &name);
    printf("%s\n", name);
}

Upvotes: 0

Views: 52

Answers (1)

savram
savram

Reputation: 580

Change the scanf line to

scanf("%29s", name);

Since your array has 30 positions, it shouldn't read more than 29. The last one is for the null terminating char

Upvotes: 1

Related Questions