#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 block of statements that forms the body // of the 'main' function begins with the opening // brace '{' and must end with a matching closing // brace '}'. In this program, that closing brace // is missing. // // When the compiler reaches the end of the source // code without finding the matching '}', it reports // a syntax error. We will discuss blocks and braces // in more detail in the next topic. // // Important takeaway: When writing programs, you // must learn to be very precise. C++ has a formally // defined syntax, and source code that violates that // syntax will generally result in a compile-time // error. More troublesome are mistakes that still // form valid C++ programs but cause those programs // to behave differently from what you intended. // // In English, if you write a paragraph and forget // the final period, a reader can usually infer what // you intended. Human languages routinely tolerate // small errors because readers use context and their // knowledge of the language to resolve them. // // Programming languages also have grammars, but // their syntactic rules are much more rigid. A // compiler cannot simply replace arbitrary invalid // source code with whatever it thinks the programmer // probably intended. This precision is especially // important as programs grow from a few lines to // thousands or even millions of lines of source code. // // Historical note: Early web browsers were often // very forgiving of malformed HTML. For example, // authors frequently omitted closing tags such as // //

// // and browsers attempted to determine the intended // structure of the document. Different browsers did // not always make the same decisions, so malformed // HTML could be displayed differently by different // browsers. // // XML, and consequently XHTML, took a much stricter // approach: an XML document must be well formed, // with properly nested elements and required closing // tags. Modern HTML took a different approach. // HTML5 formally specifies how browsers should // recover from many kinds of malformed HTML, making // that error recovery much more consistent across // browsers.