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.