Devesh Pratap Devesh Pratap - 9 months ago 60
C Question

I am trying to terminate while() loop which is using fgets() function

char src[5][100];
size_t counter = 0 ; // counter for src as it is two dimensional
printf("Enter the string : ");
while( counter < 5 && (fgets(src[counter],100,stdin) != NULL) && src[counter][0] != '\0' ) {

I am trying to terminate this loop, one way is to take some kind of fixed input like quit or something, I don't want to use that.
Another way is to press ctrl + D to tell EOF , I don't want to do that too.


When fgets reads a line, it includes the terminating end line character \n. If a user just presses enter, fgets will return the string "\n" rather than the empty string. Try replacing the condition

src[counter][0] != '\0' 


src[counter][0] != '\n'