#include // Why does this source code not compile? // // First, examine the code and try to // identify the problem without compiling it. // // If you cannot find the problem, compile // the code and use the compiler's error // message as a hint. // // If you still cannot find the problem, // scroll down. // Function declaration int main(); // Function definition int main() { std::cout << Hello World! << std::endl; return 0; } // The sequence of characters // // Hello World! // // is intended to be a string. In C++, a string // literal must be enclosed in double quotes: // // "Hello World!" // // Without the double quotes, the compiler does // not interpret 'Hello World!' as text to be // printed. Instead, it tries to interpret the // characters as C++ source code. In particular, // 'Hello' and 'World' are treated as identifiers, // while '!' is the logical NOT operator. // // Note that '!' is not a factorial operator. // In fact, C++ has no factorial operator. // // The correct statement is therefore // // std::cout << "Hello World!" << std::endl; // // Exercise: What happens if you include only the // opening double quote but omit the closing one? // What happens if you include only the closing // double quote but omit the opening one? // // Compare the compiler's error messages. Can you // explain why they are different?