What does the symbol mean in a string-literal?











up vote
47
down vote

favorite
19












Consider following code:



char str = "Hello";


What is the length of str array, and with how much 0s it is ending?










share|improve this question




















  • 3




    @CodyGray: Why have you removed the C++ tag? This has changed the correctness of some of the answers and subsequent comments?
    – CB Bailey
    Jan 17 '11 at 9:20






  • 3




    @CodyGray: C strings are still often used in C++. As for answers not being correct, that was my point; I downvoted an incorrect answer and you magically made it correct thus invalidating my downvote.
    – CB Bailey
    Jan 17 '11 at 9:27






  • 2




    @Charles Bailey I am mainly using C++, but of course I want to know is there any difference between C and C++ . That's why I add C++ tag.
    – UmmaGumma
    Jan 17 '11 at 9:38






  • 2




    @Ashot: Next time, please add that to your question. It's difficult to infer things like that from tags, and I'm really bad at reading people's minds. @Charles: Ultimately, I suppose, my aim was to reduce ambiguity. It seems you're arguing my side when you say the correctness of answers hinges on the tags in use. Like I said, feel free to rollback my edits if you disagree.
    – Cody Gray
    Jan 17 '11 at 9:41






  • 3




    @CodyGray: At one particular point in the cycle of edits an answer suggested that something like char a[2] = "OK"; was OK. I immediately downvoted but as I was writing my comment I double checked the tags and noted that the question was now only about C.
    – CB Bailey
    Jan 17 '11 at 9:52















up vote
47
down vote

favorite
19












Consider following code:



char str = "Hello";


What is the length of str array, and with how much 0s it is ending?










share|improve this question




















  • 3




    @CodyGray: Why have you removed the C++ tag? This has changed the correctness of some of the answers and subsequent comments?
    – CB Bailey
    Jan 17 '11 at 9:20






  • 3




    @CodyGray: C strings are still often used in C++. As for answers not being correct, that was my point; I downvoted an incorrect answer and you magically made it correct thus invalidating my downvote.
    – CB Bailey
    Jan 17 '11 at 9:27






  • 2




    @Charles Bailey I am mainly using C++, but of course I want to know is there any difference between C and C++ . That's why I add C++ tag.
    – UmmaGumma
    Jan 17 '11 at 9:38






  • 2




    @Ashot: Next time, please add that to your question. It's difficult to infer things like that from tags, and I'm really bad at reading people's minds. @Charles: Ultimately, I suppose, my aim was to reduce ambiguity. It seems you're arguing my side when you say the correctness of answers hinges on the tags in use. Like I said, feel free to rollback my edits if you disagree.
    – Cody Gray
    Jan 17 '11 at 9:41






  • 3




    @CodyGray: At one particular point in the cycle of edits an answer suggested that something like char a[2] = "OK"; was OK. I immediately downvoted but as I was writing my comment I double checked the tags and noted that the question was now only about C.
    – CB Bailey
    Jan 17 '11 at 9:52













up vote
47
down vote

favorite
19









up vote
47
down vote

favorite
19






19





Consider following code:



char str = "Hello";


What is the length of str array, and with how much 0s it is ending?










share|improve this question















Consider following code:



char str = "Hello";


What is the length of str array, and with how much 0s it is ending?







c++ c string escaping string-literals






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:53









Deduplicator

33.7k64787




33.7k64787










asked Jan 17 '11 at 9:02









UmmaGumma

4,14512341




4,14512341








  • 3




    @CodyGray: Why have you removed the C++ tag? This has changed the correctness of some of the answers and subsequent comments?
    – CB Bailey
    Jan 17 '11 at 9:20






  • 3




    @CodyGray: C strings are still often used in C++. As for answers not being correct, that was my point; I downvoted an incorrect answer and you magically made it correct thus invalidating my downvote.
    – CB Bailey
    Jan 17 '11 at 9:27






  • 2




    @Charles Bailey I am mainly using C++, but of course I want to know is there any difference between C and C++ . That's why I add C++ tag.
    – UmmaGumma
    Jan 17 '11 at 9:38






  • 2




    @Ashot: Next time, please add that to your question. It's difficult to infer things like that from tags, and I'm really bad at reading people's minds. @Charles: Ultimately, I suppose, my aim was to reduce ambiguity. It seems you're arguing my side when you say the correctness of answers hinges on the tags in use. Like I said, feel free to rollback my edits if you disagree.
    – Cody Gray
    Jan 17 '11 at 9:41






  • 3




    @CodyGray: At one particular point in the cycle of edits an answer suggested that something like char a[2] = "OK"; was OK. I immediately downvoted but as I was writing my comment I double checked the tags and noted that the question was now only about C.
    – CB Bailey
    Jan 17 '11 at 9:52














  • 3




    @CodyGray: Why have you removed the C++ tag? This has changed the correctness of some of the answers and subsequent comments?
    – CB Bailey
    Jan 17 '11 at 9:20






  • 3




    @CodyGray: C strings are still often used in C++. As for answers not being correct, that was my point; I downvoted an incorrect answer and you magically made it correct thus invalidating my downvote.
    – CB Bailey
    Jan 17 '11 at 9:27






  • 2




    @Charles Bailey I am mainly using C++, but of course I want to know is there any difference between C and C++ . That's why I add C++ tag.
    – UmmaGumma
    Jan 17 '11 at 9:38






  • 2




    @Ashot: Next time, please add that to your question. It's difficult to infer things like that from tags, and I'm really bad at reading people's minds. @Charles: Ultimately, I suppose, my aim was to reduce ambiguity. It seems you're arguing my side when you say the correctness of answers hinges on the tags in use. Like I said, feel free to rollback my edits if you disagree.
    – Cody Gray
    Jan 17 '11 at 9:41






  • 3




    @CodyGray: At one particular point in the cycle of edits an answer suggested that something like char a[2] = "OK"; was OK. I immediately downvoted but as I was writing my comment I double checked the tags and noted that the question was now only about C.
    – CB Bailey
    Jan 17 '11 at 9:52








3




3




@CodyGray: Why have you removed the C++ tag? This has changed the correctness of some of the answers and subsequent comments?
– CB Bailey
Jan 17 '11 at 9:20




@CodyGray: Why have you removed the C++ tag? This has changed the correctness of some of the answers and subsequent comments?
– CB Bailey
Jan 17 '11 at 9:20




3




3




@CodyGray: C strings are still often used in C++. As for answers not being correct, that was my point; I downvoted an incorrect answer and you magically made it correct thus invalidating my downvote.
– CB Bailey
Jan 17 '11 at 9:27




@CodyGray: C strings are still often used in C++. As for answers not being correct, that was my point; I downvoted an incorrect answer and you magically made it correct thus invalidating my downvote.
– CB Bailey
Jan 17 '11 at 9:27




2




2




@Charles Bailey I am mainly using C++, but of course I want to know is there any difference between C and C++ . That's why I add C++ tag.
– UmmaGumma
Jan 17 '11 at 9:38




@Charles Bailey I am mainly using C++, but of course I want to know is there any difference between C and C++ . That's why I add C++ tag.
– UmmaGumma
Jan 17 '11 at 9:38




2




2




@Ashot: Next time, please add that to your question. It's difficult to infer things like that from tags, and I'm really bad at reading people's minds. @Charles: Ultimately, I suppose, my aim was to reduce ambiguity. It seems you're arguing my side when you say the correctness of answers hinges on the tags in use. Like I said, feel free to rollback my edits if you disagree.
– Cody Gray
Jan 17 '11 at 9:41




@Ashot: Next time, please add that to your question. It's difficult to infer things like that from tags, and I'm really bad at reading people's minds. @Charles: Ultimately, I suppose, my aim was to reduce ambiguity. It seems you're arguing my side when you say the correctness of answers hinges on the tags in use. Like I said, feel free to rollback my edits if you disagree.
– Cody Gray
Jan 17 '11 at 9:41




3




3




@CodyGray: At one particular point in the cycle of edits an answer suggested that something like char a[2] = "OK"; was OK. I immediately downvoted but as I was writing my comment I double checked the tags and noted that the question was now only about C.
– CB Bailey
Jan 17 '11 at 9:52




@CodyGray: At one particular point in the cycle of edits an answer suggested that something like char a[2] = "OK"; was OK. I immediately downvoted but as I was writing my comment I double checked the tags and noted that the question was now only about C.
– CB Bailey
Jan 17 '11 at 9:52












6 Answers
6






active

oldest

votes

















up vote
82
down vote



accepted










sizeof str is 7 - five bytes for the "Hello" text, plus the explicit NUL terminator, plus the implicit NUL terminator.



strlen(str) is 5 - the five "Hello" bytes only.



The key here is that the implicit nul terminator is always added - even if the string literal just happens to end with . Of course, strlen just stops at the first - it can't tell the difference.



There is one exception to the implicit NUL terminator rule - if you explicitly specify the array size, the string will be truncated to fit:



char str[6] = "Hello"; // strlen(str) = 5, sizeof(str) = 6 (with one NUL)
char str[7] = "Hello"; // strlen(str) = 5, sizeof(str) = 7 (with two NULs)
char str[8] = "Hello"; // strlen(str) = 5, sizeof(str) = 8 (with three NULs per C99 6.7.8.21)


This is, however, rarely useful, and prone to miscalculating the string length and ending up with an unterminated string. It is also forbidden in C++.






share|improve this answer



















  • 5




    You should add that this kind of truncation is only valid in C, not in C++.
    – fredoverflow
    Jan 17 '11 at 9:15






  • 3




    Your char [8] example seems wrong. If the OP had used char str[8] = { 'H', 'e', 'l', 'l', 'o', '', '' }; the remaining character's value would _not_ be undefined, it would be zero (so that you can sanely initialize, e.g. int arr[100] = { 0 }` to be all zeroes). I don't see why it would be any different for "Hello" than it is for the long form, unless the standard explicitly makes an exception for this case (which would seem very strange to me.)
    – Chris Lutz
    Jan 17 '11 at 9:19










  • @Chris, yes, I updated it presumably while you were writing your response :)
    – bdonlan
    Jan 17 '11 at 9:50






  • 1




    Incidentally, the paragraph in question: If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    – bdonlan
    Jan 17 '11 at 9:50






  • 1




    FYI, null character (also null terminator), abbreviated NUL, therefore the term "NUL terminator" is little bit confusing for me.
    – V-SHY
    Feb 17 '15 at 3:58


















up vote
10
down vote













The length of the array is 7, the NUL character still counts as a character and the string is still terminated with an implicit



See this link to see a working example



Note that had you declared str as char str[6]= "Hello"; the length would be 6 because the implicit NUL is only added if it can fit (which it can't in this example.)




§ 6.7.8/p14
An array of
character type may be initialized by a
character string literal, optionally
enclosed in braces. Sucessive
characters of the character string
literal (including the terminating
null character if there is room or if
the array is of unknown size)
initialize the elements of the array.




Examples



char str = "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
char str[5]= "Hello"; /* sizeof == 5, str is "Hello" with no NUL (no longer a C-string, just an array of char). This may trigger compiler warning */
char str[6]= "Hello"; /* sizeof == 6, Explicit NUL only */
char str[7]= "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
char str[8]= "Hello"; /* sizeof == 8, Explicit + two Implicit NUL */





share|improve this answer























  • The length of the "string" (as C functions view strings) is 5. The sizeof operator returns 7.
    – Chris Lutz
    Jan 17 '11 at 9:07






  • 2




    @ChrisLutz: The question asked was "What is the length of str array " (my emphasis) so this answer is correct.
    – CB Bailey
    Jan 17 '11 at 9:11








  • 1




    @Chris Ok, I'll concede that the C vernacular has different meanings for length and size with respect to strings. In my answer I was referring to the latter.
    – SiegeX
    Jan 17 '11 at 9:12


















up vote
5
down vote













Banging my usual drum solo of JUST TRY IT, here's how you can answer questions like that in the future:



$ cat junk.c
#include <stdio.h>

char* string = "Hello";

int main(int argv, char** argc)
{
printf("-->%s<--n", string);
}
$ gcc -S junk.c
$ cat junk.s


... eliding the unnecessary parts ...



.LC0:
.string "Hello"
.string ""


...



.LC1:
.string "-->%s<--n"


...



Note here how the string I used for printf is just "-->%s<---n" while the global string is in two parts: "Hello" and "". The GNU assembler also terminates strings with an implicit NUL character, so the fact that the first string (.LC0) is in those two parts indicates that there are two NULs. The string is thus 7 bytes long. Generally if you really want to know what your compiler is doing with a certain hunk of code, isolate it in a dummy example like this and see what it's doing using -S (for GNU -- MSVC has a flag too for assembler output but I don't know it off-hand). You'll learn a lot about how your code works (or fails to work as the case may be) and you'll get an answer quickly that is 100% guaranteed to match the tools and environment you're working in.






share|improve this answer

















  • 2




    ... unless the thing we're testing happens to be undefined behavior, in which case the answer might only be 100% guaranteed to match the tools and environment at the moment it's tested. Furthermore, if the thing we're testing is implementation-defined, then to really get the answer, we'd have to test it on all possible implementations. (And we'd also have to know it's implementation-defined, but if we already knew that, we wouldn't have had to ask.) Furthermore, to test in this way, we'll need to know the rules for GNU assembler as well as the language we're actually trying to work in.
    – Rob Kennedy
    Jan 17 '11 at 15:12


















up vote
3
down vote














What is the length of str array, and with how much 0s it is ending?




Let's find out:



int main() {
char str = "Hello";
int length = sizeof str / sizeof str[0];
// "sizeof array" is the bytes for the whole array (must use a real array, not
// a pointer), divide by "sizeof array[0]" (sometimes sizeof *array is used)
// to get the number of items in the array
printf("array length: %dn", length);
printf("last 3 bytes: %02x %02x %02xn",
str[length - 3], str[length - 2], str[length - 1]);
return 0;
}





share|improve this answer




























    up vote
    3
    down vote













    Specifically, I want to mention one situation, by which you may confuse.



    What is the difference between "" and ""?



    The answer is that "" represents in array is {0 0} and "" is {0}.



    Because "" is still a string literal and it will also add "" at the end of it. And "" is empty but also add "".



    Understanding of this will help you understand "" deeply.






    share|improve this answer























    • In other words, the empty-string is still null-terminated in C.
      – David C. Rankin
      Oct 28 '15 at 7:58


















    up vote
    0
    down vote













    char str= "Hello";


    That would be 7 bytes.



    In memory it'd be:



    48 65 6C 6C 6F 00 00
    H e l l o


    Edit:




    • What does the symbol mean in a C string?

      It's the "end" of a string. A null character. In memory, it's actually a Zero. Usually functions that handle char arrays look for this character, as this is the end of the message. I'll put an example at the end.


    • What is the length of str array? (Answered before the edit part)

      7


    • and with how much 0s it is ending?

      You array has two "spaces" with zero; str[5]=str[6]=''=0



    Extra example:

    Let's assume you have a function that prints the content of that text array.
    You could define it as:



    char str[40];


    Now, you could change the content of that array (I won't get into details on how to), so that it contains the message: "This is just a printing test"
    In memory, you should have something like:



    54 68 69 73 20 69 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
    69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


    So you print that char array. And then you want a new message. Let's say just "Hello"



    48 65 6c 6c 6f 00 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
    69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


    Notice the 00 on str[5]. That's how the print function will know how much it actually needs to send, despite the actual longitude of the vector and the whole content.






    share|improve this answer























    • You are not answering the original question "what does the symbol mean". Please expand your answer to address the original question.
      – Michal
      Nov 8 at 20:28










    • Other answers already mention that str is an array of size 7, including the accepted answer from seven years ago. Why repeat it yet again (without adding anything new)?
      – melpomene
      Nov 8 at 20:37










    • @Michal, you do realize the original post has 3 questions, right?
      – L. Lopez
      Nov 10 at 18:18










    • @melpomene. I do apologize for that. I expanded the answer and hopefully it clarifies further and adds more, as you seem to want.
      – L. Lopez
      Nov 10 at 18:20










    protected by melpomene Nov 8 at 20:35



    Thank you for your interest in this question.
    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



    Would you like to answer one of these unanswered questions instead?














    6 Answers
    6






    active

    oldest

    votes








    6 Answers
    6






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    82
    down vote



    accepted










    sizeof str is 7 - five bytes for the "Hello" text, plus the explicit NUL terminator, plus the implicit NUL terminator.



    strlen(str) is 5 - the five "Hello" bytes only.



    The key here is that the implicit nul terminator is always added - even if the string literal just happens to end with . Of course, strlen just stops at the first - it can't tell the difference.



    There is one exception to the implicit NUL terminator rule - if you explicitly specify the array size, the string will be truncated to fit:



    char str[6] = "Hello"; // strlen(str) = 5, sizeof(str) = 6 (with one NUL)
    char str[7] = "Hello"; // strlen(str) = 5, sizeof(str) = 7 (with two NULs)
    char str[8] = "Hello"; // strlen(str) = 5, sizeof(str) = 8 (with three NULs per C99 6.7.8.21)


    This is, however, rarely useful, and prone to miscalculating the string length and ending up with an unterminated string. It is also forbidden in C++.






    share|improve this answer



















    • 5




      You should add that this kind of truncation is only valid in C, not in C++.
      – fredoverflow
      Jan 17 '11 at 9:15






    • 3




      Your char [8] example seems wrong. If the OP had used char str[8] = { 'H', 'e', 'l', 'l', 'o', '', '' }; the remaining character's value would _not_ be undefined, it would be zero (so that you can sanely initialize, e.g. int arr[100] = { 0 }` to be all zeroes). I don't see why it would be any different for "Hello" than it is for the long form, unless the standard explicitly makes an exception for this case (which would seem very strange to me.)
      – Chris Lutz
      Jan 17 '11 at 9:19










    • @Chris, yes, I updated it presumably while you were writing your response :)
      – bdonlan
      Jan 17 '11 at 9:50






    • 1




      Incidentally, the paragraph in question: If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
      – bdonlan
      Jan 17 '11 at 9:50






    • 1




      FYI, null character (also null terminator), abbreviated NUL, therefore the term "NUL terminator" is little bit confusing for me.
      – V-SHY
      Feb 17 '15 at 3:58















    up vote
    82
    down vote



    accepted










    sizeof str is 7 - five bytes for the "Hello" text, plus the explicit NUL terminator, plus the implicit NUL terminator.



    strlen(str) is 5 - the five "Hello" bytes only.



    The key here is that the implicit nul terminator is always added - even if the string literal just happens to end with . Of course, strlen just stops at the first - it can't tell the difference.



    There is one exception to the implicit NUL terminator rule - if you explicitly specify the array size, the string will be truncated to fit:



    char str[6] = "Hello"; // strlen(str) = 5, sizeof(str) = 6 (with one NUL)
    char str[7] = "Hello"; // strlen(str) = 5, sizeof(str) = 7 (with two NULs)
    char str[8] = "Hello"; // strlen(str) = 5, sizeof(str) = 8 (with three NULs per C99 6.7.8.21)


    This is, however, rarely useful, and prone to miscalculating the string length and ending up with an unterminated string. It is also forbidden in C++.






    share|improve this answer



















    • 5




      You should add that this kind of truncation is only valid in C, not in C++.
      – fredoverflow
      Jan 17 '11 at 9:15






    • 3




      Your char [8] example seems wrong. If the OP had used char str[8] = { 'H', 'e', 'l', 'l', 'o', '', '' }; the remaining character's value would _not_ be undefined, it would be zero (so that you can sanely initialize, e.g. int arr[100] = { 0 }` to be all zeroes). I don't see why it would be any different for "Hello" than it is for the long form, unless the standard explicitly makes an exception for this case (which would seem very strange to me.)
      – Chris Lutz
      Jan 17 '11 at 9:19










    • @Chris, yes, I updated it presumably while you were writing your response :)
      – bdonlan
      Jan 17 '11 at 9:50






    • 1




      Incidentally, the paragraph in question: If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
      – bdonlan
      Jan 17 '11 at 9:50






    • 1




      FYI, null character (also null terminator), abbreviated NUL, therefore the term "NUL terminator" is little bit confusing for me.
      – V-SHY
      Feb 17 '15 at 3:58













    up vote
    82
    down vote



    accepted







    up vote
    82
    down vote



    accepted






    sizeof str is 7 - five bytes for the "Hello" text, plus the explicit NUL terminator, plus the implicit NUL terminator.



    strlen(str) is 5 - the five "Hello" bytes only.



    The key here is that the implicit nul terminator is always added - even if the string literal just happens to end with . Of course, strlen just stops at the first - it can't tell the difference.



    There is one exception to the implicit NUL terminator rule - if you explicitly specify the array size, the string will be truncated to fit:



    char str[6] = "Hello"; // strlen(str) = 5, sizeof(str) = 6 (with one NUL)
    char str[7] = "Hello"; // strlen(str) = 5, sizeof(str) = 7 (with two NULs)
    char str[8] = "Hello"; // strlen(str) = 5, sizeof(str) = 8 (with three NULs per C99 6.7.8.21)


    This is, however, rarely useful, and prone to miscalculating the string length and ending up with an unterminated string. It is also forbidden in C++.






    share|improve this answer














    sizeof str is 7 - five bytes for the "Hello" text, plus the explicit NUL terminator, plus the implicit NUL terminator.



    strlen(str) is 5 - the five "Hello" bytes only.



    The key here is that the implicit nul terminator is always added - even if the string literal just happens to end with . Of course, strlen just stops at the first - it can't tell the difference.



    There is one exception to the implicit NUL terminator rule - if you explicitly specify the array size, the string will be truncated to fit:



    char str[6] = "Hello"; // strlen(str) = 5, sizeof(str) = 6 (with one NUL)
    char str[7] = "Hello"; // strlen(str) = 5, sizeof(str) = 7 (with two NULs)
    char str[8] = "Hello"; // strlen(str) = 5, sizeof(str) = 8 (with three NULs per C99 6.7.8.21)


    This is, however, rarely useful, and prone to miscalculating the string length and ending up with an unterminated string. It is also forbidden in C++.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 17 '11 at 9:17

























    answered Jan 17 '11 at 9:05









    bdonlan

    172k21220297




    172k21220297








    • 5




      You should add that this kind of truncation is only valid in C, not in C++.
      – fredoverflow
      Jan 17 '11 at 9:15






    • 3




      Your char [8] example seems wrong. If the OP had used char str[8] = { 'H', 'e', 'l', 'l', 'o', '', '' }; the remaining character's value would _not_ be undefined, it would be zero (so that you can sanely initialize, e.g. int arr[100] = { 0 }` to be all zeroes). I don't see why it would be any different for "Hello" than it is for the long form, unless the standard explicitly makes an exception for this case (which would seem very strange to me.)
      – Chris Lutz
      Jan 17 '11 at 9:19










    • @Chris, yes, I updated it presumably while you were writing your response :)
      – bdonlan
      Jan 17 '11 at 9:50






    • 1




      Incidentally, the paragraph in question: If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
      – bdonlan
      Jan 17 '11 at 9:50






    • 1




      FYI, null character (also null terminator), abbreviated NUL, therefore the term "NUL terminator" is little bit confusing for me.
      – V-SHY
      Feb 17 '15 at 3:58














    • 5




      You should add that this kind of truncation is only valid in C, not in C++.
      – fredoverflow
      Jan 17 '11 at 9:15






    • 3




      Your char [8] example seems wrong. If the OP had used char str[8] = { 'H', 'e', 'l', 'l', 'o', '', '' }; the remaining character's value would _not_ be undefined, it would be zero (so that you can sanely initialize, e.g. int arr[100] = { 0 }` to be all zeroes). I don't see why it would be any different for "Hello" than it is for the long form, unless the standard explicitly makes an exception for this case (which would seem very strange to me.)
      – Chris Lutz
      Jan 17 '11 at 9:19










    • @Chris, yes, I updated it presumably while you were writing your response :)
      – bdonlan
      Jan 17 '11 at 9:50






    • 1




      Incidentally, the paragraph in question: If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
      – bdonlan
      Jan 17 '11 at 9:50






    • 1




      FYI, null character (also null terminator), abbreviated NUL, therefore the term "NUL terminator" is little bit confusing for me.
      – V-SHY
      Feb 17 '15 at 3:58








    5




    5




    You should add that this kind of truncation is only valid in C, not in C++.
    – fredoverflow
    Jan 17 '11 at 9:15




    You should add that this kind of truncation is only valid in C, not in C++.
    – fredoverflow
    Jan 17 '11 at 9:15




    3




    3




    Your char [8] example seems wrong. If the OP had used char str[8] = { 'H', 'e', 'l', 'l', 'o', '', '' }; the remaining character's value would _not_ be undefined, it would be zero (so that you can sanely initialize, e.g. int arr[100] = { 0 }` to be all zeroes). I don't see why it would be any different for "Hello" than it is for the long form, unless the standard explicitly makes an exception for this case (which would seem very strange to me.)
    – Chris Lutz
    Jan 17 '11 at 9:19




    Your char [8] example seems wrong. If the OP had used char str[8] = { 'H', 'e', 'l', 'l', 'o', '', '' }; the remaining character's value would _not_ be undefined, it would be zero (so that you can sanely initialize, e.g. int arr[100] = { 0 }` to be all zeroes). I don't see why it would be any different for "Hello" than it is for the long form, unless the standard explicitly makes an exception for this case (which would seem very strange to me.)
    – Chris Lutz
    Jan 17 '11 at 9:19












    @Chris, yes, I updated it presumably while you were writing your response :)
    – bdonlan
    Jan 17 '11 at 9:50




    @Chris, yes, I updated it presumably while you were writing your response :)
    – bdonlan
    Jan 17 '11 at 9:50




    1




    1




    Incidentally, the paragraph in question: If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    – bdonlan
    Jan 17 '11 at 9:50




    Incidentally, the paragraph in question: If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    – bdonlan
    Jan 17 '11 at 9:50




    1




    1




    FYI, null character (also null terminator), abbreviated NUL, therefore the term "NUL terminator" is little bit confusing for me.
    – V-SHY
    Feb 17 '15 at 3:58




    FYI, null character (also null terminator), abbreviated NUL, therefore the term "NUL terminator" is little bit confusing for me.
    – V-SHY
    Feb 17 '15 at 3:58












    up vote
    10
    down vote













    The length of the array is 7, the NUL character still counts as a character and the string is still terminated with an implicit



    See this link to see a working example



    Note that had you declared str as char str[6]= "Hello"; the length would be 6 because the implicit NUL is only added if it can fit (which it can't in this example.)




    § 6.7.8/p14
    An array of
    character type may be initialized by a
    character string literal, optionally
    enclosed in braces. Sucessive
    characters of the character string
    literal (including the terminating
    null character if there is room or if
    the array is of unknown size)
    initialize the elements of the array.




    Examples



    char str = "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
    char str[5]= "Hello"; /* sizeof == 5, str is "Hello" with no NUL (no longer a C-string, just an array of char). This may trigger compiler warning */
    char str[6]= "Hello"; /* sizeof == 6, Explicit NUL only */
    char str[7]= "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
    char str[8]= "Hello"; /* sizeof == 8, Explicit + two Implicit NUL */





    share|improve this answer























    • The length of the "string" (as C functions view strings) is 5. The sizeof operator returns 7.
      – Chris Lutz
      Jan 17 '11 at 9:07






    • 2




      @ChrisLutz: The question asked was "What is the length of str array " (my emphasis) so this answer is correct.
      – CB Bailey
      Jan 17 '11 at 9:11








    • 1




      @Chris Ok, I'll concede that the C vernacular has different meanings for length and size with respect to strings. In my answer I was referring to the latter.
      – SiegeX
      Jan 17 '11 at 9:12















    up vote
    10
    down vote













    The length of the array is 7, the NUL character still counts as a character and the string is still terminated with an implicit



    See this link to see a working example



    Note that had you declared str as char str[6]= "Hello"; the length would be 6 because the implicit NUL is only added if it can fit (which it can't in this example.)




    § 6.7.8/p14
    An array of
    character type may be initialized by a
    character string literal, optionally
    enclosed in braces. Sucessive
    characters of the character string
    literal (including the terminating
    null character if there is room or if
    the array is of unknown size)
    initialize the elements of the array.




    Examples



    char str = "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
    char str[5]= "Hello"; /* sizeof == 5, str is "Hello" with no NUL (no longer a C-string, just an array of char). This may trigger compiler warning */
    char str[6]= "Hello"; /* sizeof == 6, Explicit NUL only */
    char str[7]= "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
    char str[8]= "Hello"; /* sizeof == 8, Explicit + two Implicit NUL */





    share|improve this answer























    • The length of the "string" (as C functions view strings) is 5. The sizeof operator returns 7.
      – Chris Lutz
      Jan 17 '11 at 9:07






    • 2




      @ChrisLutz: The question asked was "What is the length of str array " (my emphasis) so this answer is correct.
      – CB Bailey
      Jan 17 '11 at 9:11








    • 1




      @Chris Ok, I'll concede that the C vernacular has different meanings for length and size with respect to strings. In my answer I was referring to the latter.
      – SiegeX
      Jan 17 '11 at 9:12













    up vote
    10
    down vote










    up vote
    10
    down vote









    The length of the array is 7, the NUL character still counts as a character and the string is still terminated with an implicit



    See this link to see a working example



    Note that had you declared str as char str[6]= "Hello"; the length would be 6 because the implicit NUL is only added if it can fit (which it can't in this example.)




    § 6.7.8/p14
    An array of
    character type may be initialized by a
    character string literal, optionally
    enclosed in braces. Sucessive
    characters of the character string
    literal (including the terminating
    null character if there is room or if
    the array is of unknown size)
    initialize the elements of the array.




    Examples



    char str = "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
    char str[5]= "Hello"; /* sizeof == 5, str is "Hello" with no NUL (no longer a C-string, just an array of char). This may trigger compiler warning */
    char str[6]= "Hello"; /* sizeof == 6, Explicit NUL only */
    char str[7]= "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
    char str[8]= "Hello"; /* sizeof == 8, Explicit + two Implicit NUL */





    share|improve this answer














    The length of the array is 7, the NUL character still counts as a character and the string is still terminated with an implicit



    See this link to see a working example



    Note that had you declared str as char str[6]= "Hello"; the length would be 6 because the implicit NUL is only added if it can fit (which it can't in this example.)




    § 6.7.8/p14
    An array of
    character type may be initialized by a
    character string literal, optionally
    enclosed in braces. Sucessive
    characters of the character string
    literal (including the terminating
    null character if there is room or if
    the array is of unknown size)
    initialize the elements of the array.




    Examples



    char str = "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
    char str[5]= "Hello"; /* sizeof == 5, str is "Hello" with no NUL (no longer a C-string, just an array of char). This may trigger compiler warning */
    char str[6]= "Hello"; /* sizeof == 6, Explicit NUL only */
    char str[7]= "Hello"; /* sizeof == 7, Explicit + Implicit NUL */
    char str[8]= "Hello"; /* sizeof == 8, Explicit + two Implicit NUL */






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 17 '11 at 9:27

























    answered Jan 17 '11 at 9:03









    SiegeX

    89.4k17112143




    89.4k17112143












    • The length of the "string" (as C functions view strings) is 5. The sizeof operator returns 7.
      – Chris Lutz
      Jan 17 '11 at 9:07






    • 2




      @ChrisLutz: The question asked was "What is the length of str array " (my emphasis) so this answer is correct.
      – CB Bailey
      Jan 17 '11 at 9:11








    • 1




      @Chris Ok, I'll concede that the C vernacular has different meanings for length and size with respect to strings. In my answer I was referring to the latter.
      – SiegeX
      Jan 17 '11 at 9:12


















    • The length of the "string" (as C functions view strings) is 5. The sizeof operator returns 7.
      – Chris Lutz
      Jan 17 '11 at 9:07






    • 2




      @ChrisLutz: The question asked was "What is the length of str array " (my emphasis) so this answer is correct.
      – CB Bailey
      Jan 17 '11 at 9:11








    • 1




      @Chris Ok, I'll concede that the C vernacular has different meanings for length and size with respect to strings. In my answer I was referring to the latter.
      – SiegeX
      Jan 17 '11 at 9:12
















    The length of the "string" (as C functions view strings) is 5. The sizeof operator returns 7.
    – Chris Lutz
    Jan 17 '11 at 9:07




    The length of the "string" (as C functions view strings) is 5. The sizeof operator returns 7.
    – Chris Lutz
    Jan 17 '11 at 9:07




    2




    2




    @ChrisLutz: The question asked was "What is the length of str array " (my emphasis) so this answer is correct.
    – CB Bailey
    Jan 17 '11 at 9:11






    @ChrisLutz: The question asked was "What is the length of str array " (my emphasis) so this answer is correct.
    – CB Bailey
    Jan 17 '11 at 9:11






    1




    1




    @Chris Ok, I'll concede that the C vernacular has different meanings for length and size with respect to strings. In my answer I was referring to the latter.
    – SiegeX
    Jan 17 '11 at 9:12




    @Chris Ok, I'll concede that the C vernacular has different meanings for length and size with respect to strings. In my answer I was referring to the latter.
    – SiegeX
    Jan 17 '11 at 9:12










    up vote
    5
    down vote













    Banging my usual drum solo of JUST TRY IT, here's how you can answer questions like that in the future:



    $ cat junk.c
    #include <stdio.h>

    char* string = "Hello";

    int main(int argv, char** argc)
    {
    printf("-->%s<--n", string);
    }
    $ gcc -S junk.c
    $ cat junk.s


    ... eliding the unnecessary parts ...



    .LC0:
    .string "Hello"
    .string ""


    ...



    .LC1:
    .string "-->%s<--n"


    ...



    Note here how the string I used for printf is just "-->%s<---n" while the global string is in two parts: "Hello" and "". The GNU assembler also terminates strings with an implicit NUL character, so the fact that the first string (.LC0) is in those two parts indicates that there are two NULs. The string is thus 7 bytes long. Generally if you really want to know what your compiler is doing with a certain hunk of code, isolate it in a dummy example like this and see what it's doing using -S (for GNU -- MSVC has a flag too for assembler output but I don't know it off-hand). You'll learn a lot about how your code works (or fails to work as the case may be) and you'll get an answer quickly that is 100% guaranteed to match the tools and environment you're working in.






    share|improve this answer

















    • 2




      ... unless the thing we're testing happens to be undefined behavior, in which case the answer might only be 100% guaranteed to match the tools and environment at the moment it's tested. Furthermore, if the thing we're testing is implementation-defined, then to really get the answer, we'd have to test it on all possible implementations. (And we'd also have to know it's implementation-defined, but if we already knew that, we wouldn't have had to ask.) Furthermore, to test in this way, we'll need to know the rules for GNU assembler as well as the language we're actually trying to work in.
      – Rob Kennedy
      Jan 17 '11 at 15:12















    up vote
    5
    down vote













    Banging my usual drum solo of JUST TRY IT, here's how you can answer questions like that in the future:



    $ cat junk.c
    #include <stdio.h>

    char* string = "Hello";

    int main(int argv, char** argc)
    {
    printf("-->%s<--n", string);
    }
    $ gcc -S junk.c
    $ cat junk.s


    ... eliding the unnecessary parts ...



    .LC0:
    .string "Hello"
    .string ""


    ...



    .LC1:
    .string "-->%s<--n"


    ...



    Note here how the string I used for printf is just "-->%s<---n" while the global string is in two parts: "Hello" and "". The GNU assembler also terminates strings with an implicit NUL character, so the fact that the first string (.LC0) is in those two parts indicates that there are two NULs. The string is thus 7 bytes long. Generally if you really want to know what your compiler is doing with a certain hunk of code, isolate it in a dummy example like this and see what it's doing using -S (for GNU -- MSVC has a flag too for assembler output but I don't know it off-hand). You'll learn a lot about how your code works (or fails to work as the case may be) and you'll get an answer quickly that is 100% guaranteed to match the tools and environment you're working in.






    share|improve this answer

















    • 2




      ... unless the thing we're testing happens to be undefined behavior, in which case the answer might only be 100% guaranteed to match the tools and environment at the moment it's tested. Furthermore, if the thing we're testing is implementation-defined, then to really get the answer, we'd have to test it on all possible implementations. (And we'd also have to know it's implementation-defined, but if we already knew that, we wouldn't have had to ask.) Furthermore, to test in this way, we'll need to know the rules for GNU assembler as well as the language we're actually trying to work in.
      – Rob Kennedy
      Jan 17 '11 at 15:12













    up vote
    5
    down vote










    up vote
    5
    down vote









    Banging my usual drum solo of JUST TRY IT, here's how you can answer questions like that in the future:



    $ cat junk.c
    #include <stdio.h>

    char* string = "Hello";

    int main(int argv, char** argc)
    {
    printf("-->%s<--n", string);
    }
    $ gcc -S junk.c
    $ cat junk.s


    ... eliding the unnecessary parts ...



    .LC0:
    .string "Hello"
    .string ""


    ...



    .LC1:
    .string "-->%s<--n"


    ...



    Note here how the string I used for printf is just "-->%s<---n" while the global string is in two parts: "Hello" and "". The GNU assembler also terminates strings with an implicit NUL character, so the fact that the first string (.LC0) is in those two parts indicates that there are two NULs. The string is thus 7 bytes long. Generally if you really want to know what your compiler is doing with a certain hunk of code, isolate it in a dummy example like this and see what it's doing using -S (for GNU -- MSVC has a flag too for assembler output but I don't know it off-hand). You'll learn a lot about how your code works (or fails to work as the case may be) and you'll get an answer quickly that is 100% guaranteed to match the tools and environment you're working in.






    share|improve this answer












    Banging my usual drum solo of JUST TRY IT, here's how you can answer questions like that in the future:



    $ cat junk.c
    #include <stdio.h>

    char* string = "Hello";

    int main(int argv, char** argc)
    {
    printf("-->%s<--n", string);
    }
    $ gcc -S junk.c
    $ cat junk.s


    ... eliding the unnecessary parts ...



    .LC0:
    .string "Hello"
    .string ""


    ...



    .LC1:
    .string "-->%s<--n"


    ...



    Note here how the string I used for printf is just "-->%s<---n" while the global string is in two parts: "Hello" and "". The GNU assembler also terminates strings with an implicit NUL character, so the fact that the first string (.LC0) is in those two parts indicates that there are two NULs. The string is thus 7 bytes long. Generally if you really want to know what your compiler is doing with a certain hunk of code, isolate it in a dummy example like this and see what it's doing using -S (for GNU -- MSVC has a flag too for assembler output but I don't know it off-hand). You'll learn a lot about how your code works (or fails to work as the case may be) and you'll get an answer quickly that is 100% guaranteed to match the tools and environment you're working in.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 17 '11 at 9:21









    JUST MY correct OPINION

    28.1k146694




    28.1k146694








    • 2




      ... unless the thing we're testing happens to be undefined behavior, in which case the answer might only be 100% guaranteed to match the tools and environment at the moment it's tested. Furthermore, if the thing we're testing is implementation-defined, then to really get the answer, we'd have to test it on all possible implementations. (And we'd also have to know it's implementation-defined, but if we already knew that, we wouldn't have had to ask.) Furthermore, to test in this way, we'll need to know the rules for GNU assembler as well as the language we're actually trying to work in.
      – Rob Kennedy
      Jan 17 '11 at 15:12














    • 2




      ... unless the thing we're testing happens to be undefined behavior, in which case the answer might only be 100% guaranteed to match the tools and environment at the moment it's tested. Furthermore, if the thing we're testing is implementation-defined, then to really get the answer, we'd have to test it on all possible implementations. (And we'd also have to know it's implementation-defined, but if we already knew that, we wouldn't have had to ask.) Furthermore, to test in this way, we'll need to know the rules for GNU assembler as well as the language we're actually trying to work in.
      – Rob Kennedy
      Jan 17 '11 at 15:12








    2




    2




    ... unless the thing we're testing happens to be undefined behavior, in which case the answer might only be 100% guaranteed to match the tools and environment at the moment it's tested. Furthermore, if the thing we're testing is implementation-defined, then to really get the answer, we'd have to test it on all possible implementations. (And we'd also have to know it's implementation-defined, but if we already knew that, we wouldn't have had to ask.) Furthermore, to test in this way, we'll need to know the rules for GNU assembler as well as the language we're actually trying to work in.
    – Rob Kennedy
    Jan 17 '11 at 15:12




    ... unless the thing we're testing happens to be undefined behavior, in which case the answer might only be 100% guaranteed to match the tools and environment at the moment it's tested. Furthermore, if the thing we're testing is implementation-defined, then to really get the answer, we'd have to test it on all possible implementations. (And we'd also have to know it's implementation-defined, but if we already knew that, we wouldn't have had to ask.) Furthermore, to test in this way, we'll need to know the rules for GNU assembler as well as the language we're actually trying to work in.
    – Rob Kennedy
    Jan 17 '11 at 15:12










    up vote
    3
    down vote














    What is the length of str array, and with how much 0s it is ending?




    Let's find out:



    int main() {
    char str = "Hello";
    int length = sizeof str / sizeof str[0];
    // "sizeof array" is the bytes for the whole array (must use a real array, not
    // a pointer), divide by "sizeof array[0]" (sometimes sizeof *array is used)
    // to get the number of items in the array
    printf("array length: %dn", length);
    printf("last 3 bytes: %02x %02x %02xn",
    str[length - 3], str[length - 2], str[length - 1]);
    return 0;
    }





    share|improve this answer

























      up vote
      3
      down vote














      What is the length of str array, and with how much 0s it is ending?




      Let's find out:



      int main() {
      char str = "Hello";
      int length = sizeof str / sizeof str[0];
      // "sizeof array" is the bytes for the whole array (must use a real array, not
      // a pointer), divide by "sizeof array[0]" (sometimes sizeof *array is used)
      // to get the number of items in the array
      printf("array length: %dn", length);
      printf("last 3 bytes: %02x %02x %02xn",
      str[length - 3], str[length - 2], str[length - 1]);
      return 0;
      }





      share|improve this answer























        up vote
        3
        down vote










        up vote
        3
        down vote










        What is the length of str array, and with how much 0s it is ending?




        Let's find out:



        int main() {
        char str = "Hello";
        int length = sizeof str / sizeof str[0];
        // "sizeof array" is the bytes for the whole array (must use a real array, not
        // a pointer), divide by "sizeof array[0]" (sometimes sizeof *array is used)
        // to get the number of items in the array
        printf("array length: %dn", length);
        printf("last 3 bytes: %02x %02x %02xn",
        str[length - 3], str[length - 2], str[length - 1]);
        return 0;
        }





        share|improve this answer













        What is the length of str array, and with how much 0s it is ending?




        Let's find out:



        int main() {
        char str = "Hello";
        int length = sizeof str / sizeof str[0];
        // "sizeof array" is the bytes for the whole array (must use a real array, not
        // a pointer), divide by "sizeof array[0]" (sometimes sizeof *array is used)
        // to get the number of items in the array
        printf("array length: %dn", length);
        printf("last 3 bytes: %02x %02x %02xn",
        str[length - 3], str[length - 2], str[length - 1]);
        return 0;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 17 '11 at 9:48









        Fred Nurk

        11.6k32957




        11.6k32957






















            up vote
            3
            down vote













            Specifically, I want to mention one situation, by which you may confuse.



            What is the difference between "" and ""?



            The answer is that "" represents in array is {0 0} and "" is {0}.



            Because "" is still a string literal and it will also add "" at the end of it. And "" is empty but also add "".



            Understanding of this will help you understand "" deeply.






            share|improve this answer























            • In other words, the empty-string is still null-terminated in C.
              – David C. Rankin
              Oct 28 '15 at 7:58















            up vote
            3
            down vote













            Specifically, I want to mention one situation, by which you may confuse.



            What is the difference between "" and ""?



            The answer is that "" represents in array is {0 0} and "" is {0}.



            Because "" is still a string literal and it will also add "" at the end of it. And "" is empty but also add "".



            Understanding of this will help you understand "" deeply.






            share|improve this answer























            • In other words, the empty-string is still null-terminated in C.
              – David C. Rankin
              Oct 28 '15 at 7:58













            up vote
            3
            down vote










            up vote
            3
            down vote









            Specifically, I want to mention one situation, by which you may confuse.



            What is the difference between "" and ""?



            The answer is that "" represents in array is {0 0} and "" is {0}.



            Because "" is still a string literal and it will also add "" at the end of it. And "" is empty but also add "".



            Understanding of this will help you understand "" deeply.






            share|improve this answer














            Specifically, I want to mention one situation, by which you may confuse.



            What is the difference between "" and ""?



            The answer is that "" represents in array is {0 0} and "" is {0}.



            Because "" is still a string literal and it will also add "" at the end of it. And "" is empty but also add "".



            Understanding of this will help you understand "" deeply.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 4 '17 at 7:39

























            answered Oct 28 '15 at 7:13









            YongHao Hu

            1,384815




            1,384815












            • In other words, the empty-string is still null-terminated in C.
              – David C. Rankin
              Oct 28 '15 at 7:58


















            • In other words, the empty-string is still null-terminated in C.
              – David C. Rankin
              Oct 28 '15 at 7:58
















            In other words, the empty-string is still null-terminated in C.
            – David C. Rankin
            Oct 28 '15 at 7:58




            In other words, the empty-string is still null-terminated in C.
            – David C. Rankin
            Oct 28 '15 at 7:58










            up vote
            0
            down vote













            char str= "Hello";


            That would be 7 bytes.



            In memory it'd be:



            48 65 6C 6C 6F 00 00
            H e l l o


            Edit:




            • What does the symbol mean in a C string?

              It's the "end" of a string. A null character. In memory, it's actually a Zero. Usually functions that handle char arrays look for this character, as this is the end of the message. I'll put an example at the end.


            • What is the length of str array? (Answered before the edit part)

              7


            • and with how much 0s it is ending?

              You array has two "spaces" with zero; str[5]=str[6]=''=0



            Extra example:

            Let's assume you have a function that prints the content of that text array.
            You could define it as:



            char str[40];


            Now, you could change the content of that array (I won't get into details on how to), so that it contains the message: "This is just a printing test"
            In memory, you should have something like:



            54 68 69 73 20 69 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
            69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


            So you print that char array. And then you want a new message. Let's say just "Hello"



            48 65 6c 6c 6f 00 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
            69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


            Notice the 00 on str[5]. That's how the print function will know how much it actually needs to send, despite the actual longitude of the vector and the whole content.






            share|improve this answer























            • You are not answering the original question "what does the symbol mean". Please expand your answer to address the original question.
              – Michal
              Nov 8 at 20:28










            • Other answers already mention that str is an array of size 7, including the accepted answer from seven years ago. Why repeat it yet again (without adding anything new)?
              – melpomene
              Nov 8 at 20:37










            • @Michal, you do realize the original post has 3 questions, right?
              – L. Lopez
              Nov 10 at 18:18










            • @melpomene. I do apologize for that. I expanded the answer and hopefully it clarifies further and adds more, as you seem to want.
              – L. Lopez
              Nov 10 at 18:20















            up vote
            0
            down vote













            char str= "Hello";


            That would be 7 bytes.



            In memory it'd be:



            48 65 6C 6C 6F 00 00
            H e l l o


            Edit:




            • What does the symbol mean in a C string?

              It's the "end" of a string. A null character. In memory, it's actually a Zero. Usually functions that handle char arrays look for this character, as this is the end of the message. I'll put an example at the end.


            • What is the length of str array? (Answered before the edit part)

              7


            • and with how much 0s it is ending?

              You array has two "spaces" with zero; str[5]=str[6]=''=0



            Extra example:

            Let's assume you have a function that prints the content of that text array.
            You could define it as:



            char str[40];


            Now, you could change the content of that array (I won't get into details on how to), so that it contains the message: "This is just a printing test"
            In memory, you should have something like:



            54 68 69 73 20 69 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
            69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


            So you print that char array. And then you want a new message. Let's say just "Hello"



            48 65 6c 6c 6f 00 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
            69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


            Notice the 00 on str[5]. That's how the print function will know how much it actually needs to send, despite the actual longitude of the vector and the whole content.






            share|improve this answer























            • You are not answering the original question "what does the symbol mean". Please expand your answer to address the original question.
              – Michal
              Nov 8 at 20:28










            • Other answers already mention that str is an array of size 7, including the accepted answer from seven years ago. Why repeat it yet again (without adding anything new)?
              – melpomene
              Nov 8 at 20:37










            • @Michal, you do realize the original post has 3 questions, right?
              – L. Lopez
              Nov 10 at 18:18










            • @melpomene. I do apologize for that. I expanded the answer and hopefully it clarifies further and adds more, as you seem to want.
              – L. Lopez
              Nov 10 at 18:20













            up vote
            0
            down vote










            up vote
            0
            down vote









            char str= "Hello";


            That would be 7 bytes.



            In memory it'd be:



            48 65 6C 6C 6F 00 00
            H e l l o


            Edit:




            • What does the symbol mean in a C string?

              It's the "end" of a string. A null character. In memory, it's actually a Zero. Usually functions that handle char arrays look for this character, as this is the end of the message. I'll put an example at the end.


            • What is the length of str array? (Answered before the edit part)

              7


            • and with how much 0s it is ending?

              You array has two "spaces" with zero; str[5]=str[6]=''=0



            Extra example:

            Let's assume you have a function that prints the content of that text array.
            You could define it as:



            char str[40];


            Now, you could change the content of that array (I won't get into details on how to), so that it contains the message: "This is just a printing test"
            In memory, you should have something like:



            54 68 69 73 20 69 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
            69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


            So you print that char array. And then you want a new message. Let's say just "Hello"



            48 65 6c 6c 6f 00 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
            69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


            Notice the 00 on str[5]. That's how the print function will know how much it actually needs to send, despite the actual longitude of the vector and the whole content.






            share|improve this answer














            char str= "Hello";


            That would be 7 bytes.



            In memory it'd be:



            48 65 6C 6C 6F 00 00
            H e l l o


            Edit:




            • What does the symbol mean in a C string?

              It's the "end" of a string. A null character. In memory, it's actually a Zero. Usually functions that handle char arrays look for this character, as this is the end of the message. I'll put an example at the end.


            • What is the length of str array? (Answered before the edit part)

              7


            • and with how much 0s it is ending?

              You array has two "spaces" with zero; str[5]=str[6]=''=0



            Extra example:

            Let's assume you have a function that prints the content of that text array.
            You could define it as:



            char str[40];


            Now, you could change the content of that array (I won't get into details on how to), so that it contains the message: "This is just a printing test"
            In memory, you should have something like:



            54 68 69 73 20 69 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
            69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


            So you print that char array. And then you want a new message. Let's say just "Hello"



            48 65 6c 6c 6f 00 73 20 6a 75 73 74 20 61 20 70 72 69 6e 74
            69 6e 67 20 74 65 73 74 00 00 00 00 00 00 00 00 00 00 00 00


            Notice the 00 on str[5]. That's how the print function will know how much it actually needs to send, despite the actual longitude of the vector and the whole content.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 18:25

























            answered Nov 8 at 20:23









            L. Lopez

            11




            11












            • You are not answering the original question "what does the symbol mean". Please expand your answer to address the original question.
              – Michal
              Nov 8 at 20:28










            • Other answers already mention that str is an array of size 7, including the accepted answer from seven years ago. Why repeat it yet again (without adding anything new)?
              – melpomene
              Nov 8 at 20:37










            • @Michal, you do realize the original post has 3 questions, right?
              – L. Lopez
              Nov 10 at 18:18










            • @melpomene. I do apologize for that. I expanded the answer and hopefully it clarifies further and adds more, as you seem to want.
              – L. Lopez
              Nov 10 at 18:20


















            • You are not answering the original question "what does the symbol mean". Please expand your answer to address the original question.
              – Michal
              Nov 8 at 20:28










            • Other answers already mention that str is an array of size 7, including the accepted answer from seven years ago. Why repeat it yet again (without adding anything new)?
              – melpomene
              Nov 8 at 20:37










            • @Michal, you do realize the original post has 3 questions, right?
              – L. Lopez
              Nov 10 at 18:18










            • @melpomene. I do apologize for that. I expanded the answer and hopefully it clarifies further and adds more, as you seem to want.
              – L. Lopez
              Nov 10 at 18:20
















            You are not answering the original question "what does the symbol mean". Please expand your answer to address the original question.
            – Michal
            Nov 8 at 20:28




            You are not answering the original question "what does the symbol mean". Please expand your answer to address the original question.
            – Michal
            Nov 8 at 20:28












            Other answers already mention that str is an array of size 7, including the accepted answer from seven years ago. Why repeat it yet again (without adding anything new)?
            – melpomene
            Nov 8 at 20:37




            Other answers already mention that str is an array of size 7, including the accepted answer from seven years ago. Why repeat it yet again (without adding anything new)?
            – melpomene
            Nov 8 at 20:37












            @Michal, you do realize the original post has 3 questions, right?
            – L. Lopez
            Nov 10 at 18:18




            @Michal, you do realize the original post has 3 questions, right?
            – L. Lopez
            Nov 10 at 18:18












            @melpomene. I do apologize for that. I expanded the answer and hopefully it clarifies further and adds more, as you seem to want.
            – L. Lopez
            Nov 10 at 18:20




            @melpomene. I do apologize for that. I expanded the answer and hopefully it clarifies further and adds more, as you seem to want.
            – L. Lopez
            Nov 10 at 18:20





            protected by melpomene Nov 8 at 20:35



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?



            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues