So I am doing a problem from C How To Program 7th Edition by Deitel. The problem is 8.10 for those who might have access to the book.
In a nutshell, I am comparing strings up to n characters. The program funs fine however at the end it crashes. I cannot figure out why
Code:
#include <string.h>
#include <stdio.h>
int main(void){
char *string1, *string2;
int n, compare;
puts("Enter a string");
fgets(string1,20, stdin);
puts("Enter a string");
fgets(string2,20, stdin);
puts("Enter the amount of characters to compare");
scanf("%d",&n);
compare = strncmp(string1, string2, n);
if(compare == 0)
printf("Strings compares are equal\n");
else
printf("string 1 is %s String 2\n", (compare > 0) ? "greater than" : "less than");
printf("Ending ....\n");
return 0;
}