Namespaces
Variants
Actions

Hello, World!

From cppreference.com
Revision as of 01:41, 29 November 2013 by DeadMG (Talk | contribs)


The following is one of the most basic programs that is possible to write in C++. This should give you a first feel of how you can get a computer to do things for you. Be sure to click the Run this code button which will compile and run the code on the server.

#include <iostream>
 
int main()
{
    std::cout << "Hello World!";
}

Output:

Hello World!

You can have fun changing the text into whatever you want.

In order to compile your program, the compiler splits the input into words- similar to how a book is made of words. For a computer program, these words are called tokens. The act of converting your source code into a bunch of tokens is called lexing. Then the compiler arranges tokens into structures that it can make sense of- much like how sentences are composed of words. These structures are referred to as productions.

http://i.imgur.com/BFRnmIr.jpg

In this sample program, we print the contents of one specific token- the string. This is conveniently highlighted as green. The compiler's rules for determining when a token is a string are that it always starts and ends with a double quote. So in order to change the text printed, we simply change the text between the quotes.

This, of course, raises a problem. What if I wanted to print some text with quotes in it?

Exercise: There are at least two separate productions in this program that can be repeated in order to output another string. Can you find at least one of them?