as we have used \n condition in while loop. \n is a new line character so until we get a newline character then loop will run then why at last we are putting a[i]=’\0’; instead of using ‘\n’ as newline character we are using ‘\0’ .why???
What is the difference between '\0' and '\n'
@Abhishmu5
null character is put just for terminating the character array. because our character array size can go till 1000.
Even if you don’t do this, code will work, it just a good practice of coding.
I hope it clears you doubt, please mark this doubt as resolved if you understood it.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
what is ‘\n’ id null character then what is ‘\0’
hi @Abhishmu5
we write while(ch!=’\n’)
to read file till the end of line
suppose input file is
asdf dfsafa #@@!#$
so our character array contains all this things
right
now when we write
cout<<str<<endl;
it will print all the stuff(asdf dfsafa #@@!#$ ) and also print some garbage after that
your can try that
why??
because cout will print string until it get ‘\0’
as soon as cout get ‘\0’ it understood that now string is completed and stop printing
but in this case we don’t put ‘\0’
so it print garbage till he get ‘\0’
so to avoid this and to get rid of that garbage we have to right
str[i]=’\0’ at the end
i hope you understood now
thanks