> Where does the newline character in the following program?

Where does the newline character in the following program?

Posted at: 2014-12-18 
If I write a program to read a name, and I enter "Bob(newline)" then 99% of the time, I DON'T want the newline stored. For this reason gets() throws the newline away. On the rare (very rare) occasions that I do want a newline stored in a string, I can append it to the end of the string immediately after reading the string.

This same situation applies in every language I've ever used - string input always discards the newline at the end of the input string.

gets() replaces the newline character with the null character.

If you want the newline character saved in the string use fgets().

code 1:

#include

int main()

{

char s[500];



gets(s);

puts(s);



printf("A");



return 0;

}

code 2:

#include

int main()

{

char s[500]={'a','n','o','n','\n'};



puts(s);

printf("A");



return 0;



}

In the 1st code i entered anon and pressed enter to finish inputting it. The newline character is supposed to be part of the string. But it does not print it. Why it is? if sting is terminated by pressing enter in what index the null character will be. how it is done? and how i can input a newline character into a string

But if i use the second code then it works. please explain.