I am trying to printf a simple string but I am not being able to.
#include <stdio.h>
int main(){
char *word;
scanf("%s", &word);
printf("%s\n", word);
return 0;
}
When I insert the word my code breaks.
It just stops the program execution but doesn't give me any error.
What am I doing wrong?
Problem 1: you need to allocate space for your word.
Problem 2: Your scanf() syntax is incorrect for a character array.
Problem 3: scanf("%s", ...) itself is susceptible to buffer overruns.
SUGGESTED ALTERNATIVE:
#include <stdio.h>
#define MAXLEN 80
int main(){
char word[MAXLEN];
fgets(word, MAXLEN, stdin);
printf("%s", word);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With