Current location - Quotes Website - Famous sayings - What is the difference between adding "\0" after a string and not adding it?
What is the difference between adding "\0" after a string and not adding it?

It may be the same, it may be different, it may collapse. If the memory is not initialized, the values ??in the memory are random. It may contain '\0'. C recognizes the end of a string by judging '\0'. When the system reads a string, it just starts from the beginning of the character array and searches backward until it finds '\0'. If you don't add it, the system will have to look further back until it finds '\0'.

If there happens to be a '\0' after your string, the string will be read correctly. If there are '\0' after some bytes after the string, it goes without saying that the system will read a bunch of garbled characters in addition to your string. If the system keeps looking back and cannot find '\0' at all, and has read the operating system area ("forbidden area"), the program will crash. If it is run on Windows, it will definitely report the classic Microsoft saying. : "This program has performed an illegal operation and will be closed soon. If the problem persists, please contact the program's supplier." Now you understand, the supplier is you.