Why can I print C-Strings without a Loop?

  • Thread starter MinusTheBear
  • Start date
  • Tags
    Loop
In summary, a C-String is a sequence of characters stored in consecutive memory locations and terminated by a null character. It can be defined as a programmer-defined array of characters. When printing out a C-String, the compiler implicitly knows to display the characters instead of the address. The << operator is overloaded to handle this behavior.
  • #1
MinusTheBear
22
0
Hey all,

I have a question regarding C-Strings.

Say I have the following function
C:
#include <iostream>
using namespace std;

int main() {
   const int SIZE = 5;
   char cstr1[SIZE] = "Hi";
   cout << cstr1;
   return 0;
}

This will actually print out the string literal.

I'm confused because the book defines a C-String as: a sequence of characters stored in consecutive memory locations and terminated by a null character. And that a C-String can appear in a program as a programmer-defined arrays of character (like my above code).

So I understand that "Hi" would have two subscripts 0 and 1, and at the end of the i it would have a null character \0. But, it'd also have two empty locations since I defined my array to carry 5 characters. However, since this is an array, shouldn't it require a loop? Does the compiler do this implicitly?

Secondly, if a C-String stores the address of the first character of the string literal, why doesn't printing out cstr1 display an address?

My book demonstrates these concepts but doesn't really explain why it's doing this.

Thanks.
 
Technology news on Phys.org
  • #2
MinusTheBear said:
Does the compiler do this implicitly?
Yes.
MinusTheBear said:
if a C-String stores the address of the first character of the string literal, why doesn't printing out cstr1 display an address?
Because the compiler "knows" that a C-string is supposed to be printed as a sequence of characters, not as an address.
 
  • #3
To expand on what @jtbell said, there are numerous overloaded versions of the << operator. If the object to be displayed (inserted into the output stream) is an int, char, long, bool, float, double, or other scalar type (i.e., not a pointer), the << operator causes that value to be displayed. If the object to be displayed is a string or the address of a character array, the << operator causes all characters in the string or char array to be displayed, stopping when it reaches the null character.
 
  • #4
jtbell said:
Because the compiler "knows" that a C-string is supposed to be printed as a sequence of characters, not as an address.
No it doesn't. That behavior is defined in <iostream>.

In C++ operators can be overloaded. One of the overloads for the << operation for this stream is for const char *. Because it's polymorphic, the compiler searches through the virtual functions of the same name until it finds one with the correct parameter signature, then it uses that. It's exactly the same as saying std::cout.append("Hi");
 

FAQ: Why can I print C-Strings without a Loop?

Why can I print C-Strings without a Loop?

This is because C-Strings are considered one-dimensional arrays of characters. Therefore, you can use the printf() function to print the entire string without needing to iterate through it using a loop.

Can I use a loop to print C-Strings?

Yes, you can use a loop to print C-Strings character by character. However, using the printf() function is a more efficient and concise way to print the entire string at once.

How does the printf() function print C-Strings without a loop?

The printf() function uses a pointer to the beginning of the string and iterates through each character until it reaches the null terminator, which marks the end of the string. It then prints each character until the null terminator is reached.

Can I use the scanf() function to input C-Strings without a loop?

Yes, you can use the scanf() function to input C-Strings without a loop. Similar to the printf() function, it uses a pointer to the beginning of the string and reads each character until it reaches the null terminator.

Are there any drawbacks to printing C-Strings without a loop?

One potential drawback is that you cannot perform any additional operations or manipulations on the individual characters of the string. If you need to do so, using a loop may be a better option. Additionally, it may be more difficult to format the output if you need to print the string in a specific way.

Similar threads

Replies
118
Views
8K
Replies
5
Views
1K
Replies
1
Views
1K
Replies
8
Views
2K
Replies
10
Views
2K
Replies
22
Views
3K
Back
Top