Why should i put

Thread: Why should i put \0 after a string?

after a string?

This is a discussion on Why should i put

Thread: Why should i put \0 after a string?

after a string?
within the C Programming forums, part of the General Programming Boards category; Why does he puts the

Thread: Why should i put \0 after a string?

after the string? Code: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> int main(void){ ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    Why should i put \0 after a string?

    Why does he puts the \0 after the string?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void){
    	char X[81], Y[81];
    	int i, length;
    
    		strcpy(X,"2 minutes to 12,00\nExtra line");
    		length = strlen(X);
    		printf("length: %d\n", length);
    		for(i=0; i<length; i++){
    			Y[i] = toupper(X[i]);
    		}
    		Y[length]='\0';
    		printf("%s\n\n", Y);
    return 0;
    }
    thanks...

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    35
    '\0' is known as the null character and is used by many string functions to denote the end of the string.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AntP View Post
    '\0' is known as the null character and is used by many string functions to denote the end of the string.
    ... and if it doesn't find a null, it will keep right on going... If your strings have garbage on the end... that's usually why.

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    In fact, it's not a string without the null character there (the formal definition of a string is “a contiguous sequence of characters terminated by and including the first null character.”)

    But the length of a string is defined as the number of bytes minus the null character, so when you're copying as in the example presented, the null character won't be seen; thus it must be attached manually.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    thank you all...i thought the null character can be copied! but i was wrong

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cas View Post
    In fact, it's not a string without the null character there (the formal definition of a string is a contiguous sequence of characters terminated by and including the first null character.)

    But the length of a string is defined as the number of bytes minus the null character, so when you're copying as in the example presented, the null character won't be seen; thus it must be attached manually.
    Well... unless you copy strlen(x) + 1 That will transport the null for you.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by CommonTater View Post
    Well... unless you copy strlen(x) + 1 That will transport the null for you.
    Very good thought!!!!!

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    '\0' is a null byte. It's often appended to the end of strings to remove the newline character recieved by input functions.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Babkockdood View Post
    '\0' is a null byte. It's often appended to the end of strings to remove the newline character recieved by input functions.
    Actually no... it's there to tell the various string manipulation functions to stop reading...

    If you had the great misfortune of creating a char* string with no null at the end (and it does happen) you might write 15 characters into it but strlen() might report 15,234 characters... all the way up until it found a null.

    Think how much fun that could be in a 64 bit system with 16gb ram?

  10. #10
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by CommonTater View Post
    Actually no... it's there to tell the various string manipulation functions to stop reading...

    If you had the great misfortune of creating a char* string with no null at the end (and it does happen) you might write 15 characters into it but strlen() might report 15,234 characters... all the way up until it found a null.

    Think how much fun that could be in a 64 bit system with 16gb ram?
    *giggles*

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by CommonTater View Post
    Actually no... it's there to tell the various string manipulation functions to stop reading...

    If you had the great misfortune of creating a char* string with no null at the end (and it does happen) you might write 15 characters into it but strlen() might report 15,234 characters... all the way up until it found a null.

    Think how much fun that could be in a 64 bit system with 16gb ram?
    Or more likely, it would crash with an access violation, trying to read memory it is not authorized to do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    So, memory is more complicated as i first have thought !

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brack View Post
    So, memory is more complicated as i first have thought !
    LOL... indeed, one does not mess with pointers lightly.


    Elysa... nobody said programming can't be fun....

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Oh, it's fun, when it works as intended. Avoid undefined behavior. It's usually no fun.
    Try to make your no-null-strlen function defined. Then it becomes fun. Or how about buffer overruns? Always fun stuff, if you can pull it off.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM