For code in C char st[] = "heloThere"; printf("%s",&st[0]);
, How does printf knows it has to print upto heloThere only, not beyond that, as we are passing only address of first byte to function. Also highlight for C++. And also this worksprintf("%s",st)
. As we are not passing any address as above. How printf works differently ? Is it defined separately ? (Then it will be overloading which is not supported by C)

- 301,070
- 26
- 186
- 335

- 75
- 7
-
2C "strings" are `char` arrays with an extra `'\0'` terminator at the end. It is not printed, but used to mark the end of the string. – 001 Jul 26 '20 at 12:51
-
[Is an array name a pointer?](//stackoverflow.com/a/1641963) – 001 Jul 26 '20 at 12:53
-
Are you aware of [array to pointer decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay)? – Bob__ Jul 26 '20 at 12:53
-
In C, most strings end in `'\0'`, so printf will loop through the byte array to find `'\0'` to get the string length. – Sprite Jul 26 '20 at 12:54
-
Because strings in C are really called *null-**terminated** byte strings*. – Some programmer dude Jul 26 '20 at 12:56
-
@Sprite Not "most", "all". By definition, a string is a null terminated array of char. If you have an object that is an array of char that contains embedded nulls or does not contain a null, it is not a string. – William Pursell Jul 26 '20 at 13:32
-
@William Pursell My initial comment was "all", later edited to "most" because I think a array of characters + length struct is also a string, just non-standard and doesn't apply with `printf`. – Sprite Jul 26 '20 at 13:45
3 Answers
In C and C++ string is a sequence of characters terminated by the zero character '\0'
.
This array declaration
char st[] = "heloThere";
is equivalent to
char st[] = { 'h', 'e', 'l', 'o', 'T', 'h', 'e', 'r', 'e', '\0' };
So this call of printf
printf("%s",&st[0]);
outputs characters pointed to by the expression &st[0]
until the zero character is encountered.
This call
printf("%s",st)
is equivalent to the previous call because array designators used in expressions with rare exceptions are converted to pointers to their first elements.

- 301,070
- 26
- 186
- 335
How does the
printf(“%s”,&st[0])
function knows how much it has to print, as we have passed only address of byte of string?
It knows it because of the string-terminating null character '\0'
, which every string needs to provide at its end.
For example the string literal "hello"
is stored in memory as "hello\0"
and the string consists of 6
characters, not 5
.
Note that strlen
does not count the terminating \0
with, so it returns 5
, not 6
.
Every array of char
has to have at least one element reserved for this terminating character. Else if you access this array by string operating functions, it invokes undefined behavior.
If you declare
char st[] = "heloThere";
the compiler automatically calculates the amount of elements needed, which is in this case 10
, not 9
.

- 14,524
- 7
- 33
- 80
C or C++ compilers add '\0' a the end of a string or char array to mark the end.
char* s = (char*)"Hello!";
this line actually looks at the memory like
so you may notice the added '\0', and that's what printf does it loops and counts characters are between " " and send that portion of memory to the out stream, another thing printf does is, it returns the the final value of the characters counter, but cout in c++ is more complex as it is an object and not a function.
this execution of the code below demonstrate that printf returns the counter value.
cout << printf("%d", printf("%s",s));
Execution : Hello!61
the execution above is we print first 'hello!' which is done by the printf on the right then we print the return of it by the second printf on the left, which we got 6 (the number of characters in "hello!") then we print again using cout the return of the second printf which is one character which is "6" the size of the first char array.

- 557
- 4
- 9