could someone please check my code and comments (and therefore my thinking) in the following program I think all is correct.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(void)
{
char msg[20]; /* 0 to 19 = 20 elements the last element should never be accessed */
/* its reseved for the \0 so in this case 0 to 18 = 19 elements are */
/* accessable */
/* read a string from a stream ie stdin, stops reading when it reads n-1 chars */
fgets(msg, 19, stdin); /* or a newline character whichever comes first */
/* fgets retains the \n character which is also copied to the array which is */
/* terminated by an \0 */
printf("%s",msg);
return 0;
}
/* The array msg has 20 elements 0 to 19 this 19th elements should never be accessed */
/* because it stores the \0(NULL) now fgets reads n-1 chars ie 19-1=18 chars including */
/* the \n and the \0 therefor fgets has 18 input chars including the \n */
/* we could exter as many chars as we wanted these would not be stored in the array */
/* therefore not going past the array bounds but they would still be in the input buffer*/
thanks.