What is syntax?
Helping the computer understand your intentions
C is a language like any other - in order to make yourself understood, there are rules and conventions to follow. These rules are termed syntax. In a human-human conversation, ambiguities are reduced by body language, tone, context and experience but ambiguities often remain and mistakes are common. Computer languages need to be precise about the meaning of any statement. Ambiguities and confusion cannot be allowed and this is where syntax errors appear.
#include
main() {
printf("%s","Hello World!\n");
}
Probably the simplest of all C programs. Line by line:
. . include the standard input/output library header file
. . all C programs need a main function
. . print a formatted string to the default output
. . close the main function block
Include the standard I/O library
Every program needs some basic methods - receiving input from the keyboard or files, producing output to the screen or to files, arithmetic, conversions and other functions. stdio.h provides the definitions of the basic methods of input and output for the screen, files and memory. The code to implement each one is built in with the compiler.
All C programs need a main function.
The function starts with a definition giving the name of the function. You can use your own function names but the OS will start execution of the program by calling main() so the file will not compile without one. As with other languages, functions can take arguments within the parentheses - in this example, main doesn't take any arguments. It is common for this first line of the function to also open the function block that contains the code to execute when the function is executed.
print a formatted string to the default output
printf can handle all your general purpose screen output. (Alternatives like cout