Thread: Question regarding cout

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    Question regarding cout

    why does these two statement behaving in the same manner?
    Code:
     	(cout<<"hello");         //1st
    Code:
    	cout<<"hello";           //2nd
    and this one throwing an error:

    Code:
    	(cout<<"hello";)         //3rd

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because
    (expression;)
    in C++ is a syntax error.
    It's nothing special about cout.

    Try
    (a=1;)

    1 works because cout doesn't return void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    in 1st shouldn't it throw some error since the operator << sends its right-hand-operands i.e. "hello") into the left hand operand i.e. ostream object cout & it should be asking for the left-parenthesis (the left-parenthesis being placed before cout)?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The parentheses isn't part of operator <<'s operands. Parentheses was used for grouping.

    One necessary use:
    Code:
    #include <iostream>
    #include <cstdlib>
    bool coinflip()
    {
       return std::rand() % 2 != 0;
    }
    int main()
    {
       std::cout << (coinflip() ? "hello" : "bye") << std::endl;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like I said, cout returns a value, so you can put it inside parenthesised expressions.

    Here's another example.
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
       if ( cout << "hello world" << endl ) {
       }
    }
    Similarly for cin
    Code:
    if ( cin >> myvar ) {
      // do stuff with valid data
    } else {
      // oops
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cout question
    By arian in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2010, 06:59 AM
  2. Cout Question
    By macman in forum C++ Programming
    Replies: 14
    Last Post: 04-08-2005, 04:00 AM
  3. cout question?
    By correlcj in forum C++ Programming
    Replies: 6
    Last Post: 11-03-2002, 01:05 PM
  4. cout question
    By The Gweech in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2002, 04:11 PM
  5. cout question...
    By bishop_74 in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2001, 11:22 AM

Tags for this Thread