Check The Programming Section
C++ is a compiler language. That means to get a program to run, a program must translate it from human-readable form to something a machine can understand. This translation is done by a small system program called compiler. What we write and read like "Hello, World!" program it is called a source code or program text and what a compiler executes is called executable, object code or machine code. Typically C++ source code is given an extension .cpp (for example, helloWorld.cpp) or .h (for example stdio.h) and object code is given an extension .obj (in Windows) or .o (in Unix). A source code file has two types source lines as
executable statements and
comments
Executable statements are translated to a language, which a machine can understand. And the comments are written by us, to better understand the source code and explain what exactly each statement will do during the execution of the source code. Let's write Hello, World! program with comments.
#include<stdio.h> //header file
int main(){
printf("Hello, World!\n"); //this line prints Hello, World!
return 0;
}
The two backslash // indicates the compiler about the starting of comment. Two // represents single line comments in C++. These comments are used for us to understand the source code. So for compiler source code means "codes without comments". So a compiler takes the source code as input and produces an object code as output, which a machine can understand.
Figure: Flow of Compiling a Source Code
During compilation, the compiler expects the source file must be grammatically correct, and what are the words we have written must have a specified meaning defined in the compiler. If the source code has anything wrong, it is detected by the compiler before executing the source code. Next, we have listed a series of error-prone code and discuss the meaning of each missing source line.
//#include is missing
int main(){
printf("Hello, World!\n");
return 0;
}
We did not include something to tell the compiler, what printf() is and what it can do? So the compiler complains and raise an error and to correct that, let's add the header file
#include<studio.h>
int main(){
printf("Hello, World!\n");
return 0;
}
Unfortunatelly, we misspelled stdio.h and again compiler complains. The compiler also objects to to this
#include<stdio.h>
int main(){
printf("Hello, World!\n);
return 0;
}
We did not terminate the string Hello, World! with a double quote ". The compiler also object this
#include<stdio.h>
integer main(){
printf("Hello, World!\n");
return 0;
}
Instead of int abbreviation we have written integer as return type of main(). Compiler also object this
#include<stdio.h>
int main(){
printf('Hello, World!\n');
return 0;
}
The string Hello, World! is represented inside a single quote ' ', instead of double quote " " to delimit the string. Compiler also object for this
#include<stdio.h>
int main(){
printf("Hello, World!\n") //missing of semicolon
return 0;
}
We forget to terminate the printf( ) statement with semicolon. In C++, many statements are terminated with a semicolon (;), which means end of statement. A C++ compiler needs this, to know when a statement ends and when a statement starts. Just for the time, consider that, "each statement requires a semicolon (;) if it is not ended with a right-curly brace (})."
Note: Remember compiler does not have common sense like human, what is looking good to us, its may not correct for compiler. Compiler is very picky about the mistakes and compiler expect what we given to the compiler must be correct.