Recent Changes - Search:

Coding C++

Computing

Procedural: Hello, World!

Trail Index | Procedural | Hello World

/**
 * Hello World
 * structure, main, includes, output
 */


// include standard input/output streams for C++
#include <iostream>

/**
 * main function
 */

int main() {
        /*
         * lines inside a function should be indented
         * to improve readability
         */

        std::cout << "Hello, World!" << std::endl; // output to terminal
        return 0; // zero means all is well
}
 /*
  * multi-line
  * comment
  * until closed with
  */

 /* single-line comment, until */
 // single-line comment, until the end of line

Comments, in slightly varied forms.

 #include <iostream>

#include statements (which have an unusual syntax, for reasons to be seen much later) are used to make the compiler aware of definitions which are written outside of the current file.

 int main() {

This is the declaration of an executable routine, or function, which is assigned the name main, does accept zero arguments (), and returns a value of type int. The { actually begins the body of the routine.

By a common convention, the "main" function is the point from which the execution of a program starts; so all executable programs need to have a "main" routine. Still by convention, main is expected to return a value, which must be zero when the program has executed correctly, and non-zero if some error occurred.

 std::cout

is the standard console output stream. It's the way C++ represents that odd Terminal thing, in practice.

 << "Hello, world!"

<< is an "operator" (like +,*,/) that sends something into a stream. In this case, the thing being sent is a literal text, identifyed as such by the double quotes.

 << std::endl

is a "end of line" being sent into the console output stream.

The semicolon is very important: it separates one instruction from the other.

identifies the end of the body of the routine.

< Procedural | Trail Index | Variables >

Edit - History - Print - Recent Changes - Search
Page last modified on December 09, 2022, at 05:00 pm