3

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 program

#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.

Community
  • 1
  • 1
phwd
  • 19,975
  • 5
  • 50
  • 78

4 Answers4

11

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.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    Your `for` loop example isn't a use of the comma operator, it's just part of the syntax for variable declaration. – Chris Lutz Jan 13 '11 at 01:32
  • 3
    @Chris Lutz- The first part of the loop is a variable declaration, but the last part (a++, b++) is indeed a use of the comma operator. I only noticed this after you pointed it out. :-) – templatetypedef Jan 13 '11 at 01:32
  • @templatetypedef - Right! I should work on my spot-error-and-comment reflex. – Chris Lutz Jan 13 '11 at 01:34
  • @Chris Lutz- No problem. :-) You should go with your gut! Most of the time you'll be right, and the times that you're wrong people are usually forgiving. – templatetypedef Jan 13 '11 at 01:39
  • Ok I get it now , I still do not understand why the comma operator cannot be coupled with the return statement though. – phwd Jan 13 '11 at 01:45
  • The comma operator can only be used to chain *expressions* together, not *statements*. return is a statement, not an expression, whereas function calls or printing data with cout are expressions and can be chained together with comma. – templatetypedef Jan 13 '11 at 01:47
  • 1
    Oh, I am mixing up expressions with statements, now I understand :) – phwd Jan 13 '11 at 01:50
3

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Yeah you are right I sat down and looked and tried other combinations of statements with/without semicolons ending a statement preceding return... Could you explain the return ? I thought it was a statement , http://msdn.microsoft.com/en-us/library/k68ktdwf(v=vs.80).aspx or can it not be used with the comma operator ? – phwd Jan 13 '11 at 01:44
  • @PhilippeHarewood: I'm sorry, but I do not understand your question. `return X;` is indeed a statement; I don't see what that has to do with it though, as your comma was in the preceding statement. Your `return` statement remains unaffected. – Lightness Races in Orbit Jan 13 '11 at 01:46
1

it should be terminated with one, yes

tekknolagi
  • 10,663
  • 24
  • 75
  • 119
0

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().
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252