Why can I print C-Strings without a Loop?

  • Context:
  • Thread starter Thread starter MinusTheBear
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary

Discussion Overview

The discussion revolves around the behavior of C-Strings in C++ and how they are printed using the output stream operator. Participants explore the underlying mechanics of C-Strings, the role of the compiler, and the implications of operator overloading.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions why a C-String can be printed without an explicit loop, suggesting that the compiler might handle this implicitly.
  • Another participant confirms that the compiler does handle this implicitly, allowing the C-String to be printed directly.
  • A further explanation indicates that the overloaded << operator for output streams is responsible for displaying the characters of the C-String until the null character is encountered.
  • There is a contention regarding whether the compiler "knows" to print a C-String as a sequence of characters, with one participant asserting that this behavior is defined in the library.
  • Another participant clarifies that the behavior is due to operator overloading, specifically for const char* types, and describes the process of how the compiler selects the appropriate overload.

Areas of Agreement / Disagreement

Participants express differing views on the role of the compiler in determining how C-Strings are printed, leading to a lack of consensus on whether the compiler's behavior is implicit or defined by the library.

Contextual Notes

Some assumptions about operator overloading and the behavior of the << operator may not be fully explored, and the discussion does not resolve the nuances of these technical points.

MinusTheBear
Messages
22
Reaction score
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";
   count << 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
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.
 
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.
 
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::count.append("Hi");
 

Similar threads

  • · Replies 18 ·
Replies
18
Views
4K
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
55
Views
7K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K