Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I read this code in a C book. I wonder that if gets(str) transforms the Enter button into a 0 at the end of a string how the Enter character ("\n") is checked to see if the string is vacant(while it should be 0 indeed)? I appreciate your consideration.
do{
        printf(":");
        gets(str);
        strcat(str,"\n");
        if(*str!='\n') fputs(str,fp);
    }while(*str!='\n');


What I have tried:

I wrote some code and saw that each string really ends with a zero, but I don't know how this code works correctly despite of the theory!
much thanks.
Posted
Updated 2-Mar-23 3:24am

See gets, _getws | Microsoft Learn[^] for full details.
 
Share this answer
 
The '\0' character is nothing to do with ENTER - it's a generic "end of string" marker in C. Every string you try to print (or otherwise handle) should have one at the end to tell the system where to stop printing!
If it is missing, the system just keeps on printing characters until it reaches one.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900