开发者

Does cout need to be terminated with a semicolon?

开发者 https://www.devze.com 2023-02-04 16:17 出处:网络
I am reading Bjarne Stroustrup\'s Programming : Principles and Practice Using C++ In the drill section for Chapter 2 it talks about various ways to look at typing errors when compiling the hello_worl

I am reading Bjarne Stroustrup's Programming : Principles and Practice Using C++

In the drill section for Chapter 2 it talks about various ways to look at typing errors when compiling the hello_world p开发者_StackOverflow社区rogram

#include "std_lib_facilities.h"

int main()  //C++ programs start by executing the function main
{
    cout << "Hello, World!\n",  // output "Hello, World!"
    keep_window_open();         // wait for a character to be entered
    return 0;
}

In particular this section asks:

Think of at least five more errors you might have made typing in your program (e.g. forget keep_window_open(), leave the Caps Lock key on while typing a word, or type a comma instead of a semicolon) and try each to see what happens when you try to compile and run those versions.

For the cout line, you can see that there is a comma instead of a semicolon.

This compiles and runs (for me). Is it making an assumption ( like in the javascript question: Why use semicolon? ) that the statement has been terminated ?

Because when I try for keep_terminal_open(); the compiler informs me of the semicolon exclusion.


The comma operator in C++ can be used as follows:

a, b;

It means "do a, disregard the result, then do b." You can chain it together like this:

a, b, c, (etc.), n;

In general, this isn't considered good style. The comma operator is rarely used in practice because it's confusing. The few times it's legitimately useful usually come up with for loops:

for (int a = 0, b = 0; a < 100; a++, b++) {
    /* ... */
}

Here, we use the comma operator in the last part of the for loop to mean "increment both a and b."

To answer your question, yes, you should have a semicolon after the cout. Using the comma operator technically works as well, but it's inelegant and likely to confuse people.


Any statement needs to be terminated by a semi-colon:

std::cout << "Hi world";

However, among other things, an expression can take the form of A,B,C, where A and B and C are evaluated, and then C becomes the result.

If you put the following expression:

std::cout << "Hi world", 3

into a statement:

std::cout << "Hi world", 3;

then it looks like you did not need the semicolon at the end of the statement. In fact what happened is that you misunderstood what a "statement" really is.

Hope this helped.


it should be terminated with one, yes


Just to explain a few other aspects of the situation...

The comma operator has the lowest precedence of any C++ operators, so - for example - the code...

#include <iostream>

int main()
{
    std::cout << 5, 2;    // outputs 5, complete line/statement evaluates to 2
    std::cout << '\n';
    std::cout << (5, 2);  // outputs 2 (5 is discarded), line evaluates to std::cout
    std::cout << '\n';
}

...will output "5" at the line commented A, and "2" from B.

Because of this precedence, note that if keep_window_open() returned void, then std::cout wouldn't know how to stream it, and you would get a compiler error from...

std::cout << keep_window_open(); // can't compile if function return type is void

...but still wouldn't be safe in the usage you're exploring...

std::cout << "Hello, World!\n",  // can compile because seen as two comma-separated
keep_window_open();              // subexpressions, so std::cout doesn't try to stream
                                 // a return value from keep_window_open().
0

精彩评论

暂无评论...
验证码 换一张
取 消