PrevUpHomeNext

day-02: std::cout


Let's build the very base of cpp code at first

Let's build the very base of cpp code at first: feel cpp/c++ at the full scale.

Write

kak prog.cpp

(kak editor)

#include <iostream>

int main()
{
	std::cout << "Hello World c++!" << std::endl;
}

Compile

g++ prog.cpp -std=c++23 -o prog

Run

./prog
Hello World c++!

std::cout: the very base of cpp

std::cout

What is std::cout ? Simply speeking, it is like a "print" "function" of c++, but in c++, std::cout is an object, it just prints something to your output console.

<<

operator<<

What is << ? it is a c++ operator. c++ has many operators. If you have learned some mathematics, you should understand what operator is. But in c++, the operator concept is more generic than it in math. << is an ostream operator in the program above, we can call it operator<< as its name for the next context of this article.

Can you imagine the math operator? A math operator has some inputs and some outputs. The operator<< in c++ has some inputs and outputs too:

( In this example, std::cout << "Hello World c++!" )

Yes, you read that right, std::cout is the first input of operator<<, "Hello World c++!" is the second input of operator<< .

And seriously, you read that right! std::cout is also the output of operator<< .

In c++, we call inputs arguments or parameters, and we call outputs return-values.

step-analysis:

1->)

(std::cout << "Hello World c++!");

Input std::cout and "Hello World c++!" to operator<<, its output is std::cout, (we say that it returns std::cout) .

2->)

((std::cout << "Hello World c++!") << std::endl);

The previous calling (the brackets on the inside) to operator<< returns std::cout, and pass it to the next (the brackets on the outside) operator<< as its first input, and the second input is std::endl, then at last, it returns std::cout again !

3->)

You might have guessed or thought it! You can chain the above process repeatedly for many times!

Let's chain it !

See:

((((std::cout << "Hello") << "c++!") << 123) << 6.5);

You can compile it, it compiles and run OK.

4->)

Are you annoyed by a lot of brackets? that's right. So I can tell you, you can omit those parentheses, the code will call operator<< working from left to right correctly.

std::cout << "Hello" << "c++!" << 123 << 6.5;

Amazing, doesn't it! I think c++ operator<< is more amazing than operator in mathematics !

Second parameter of operator<<

Numbers, characters, strings, and many many values can be used as the second parameter of operator<<, example:

std::cout << std::endl;         // std::endl is a manipulator to insert newline character.
std::cout << 123 << std::endl;     // 123 is an integer
std::cout << 32.33 << std::endl;   // 32.33 is a decimal, we call it floating value in c++
std::cout << 'A' << std::endl;     // 'A' is a character.
std::cout << "A" << std::endl;     // "A" is a string.
std::cout << "About" << std::endl; // "About" is a string.
std::cout << '\n';                 // '\n' is a newline character.

// You can chain all of them!
std::cout << 123 << '\n' << 32.33 << "\n" << 'A' << "A" << "About" << '\n';

First parameter of operator<<

Not only std::cout, but many c++ objects can be used as the first parameter of operator<<, and even some kind of values can be used as the first parameter of operator<< too, but that topic is too complicated and out of this article, you just have to focus std::cout now.

main framework

A complete c++ program must have one entry point, which the program starts its exeution from, that's the main entry point. The code will run by listed orders according to main .

Header

#include <iostream>

std::cout is found from a file on your disk: iostream

Last

It seems a bit complicated from this article, but it is very simple, you just only have to know how to use std::cout, if you can not understand it. After you use it again and again, you will understand it automatically.

Written on Aug 03, 2024

Back to index

Index

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ===================================
  #  The c++ programming language.  #
  #                                 #
  #  Join c++ Discord: yZcauUAUyC   #
  #  Deck                           #
  ===================================

Home: cppfx.xyz

K


PrevUpHomeNext