Every programming language has a set of rules that dictate how code must be written and organized. This is known as Syntax. If you violate the syntax rules, the compiler will throw an error and refuse to build your program.
In the previous chapter, we wrote a simple "Hello, World!" program. Now, we are going to break it apart line by line so you can understand exactly what every single word, symbol, and bracket does.
Almost every C program you write will start with this fundamental boilerplate structure:
#include <stdio.h>int main() { // Your code goes here return 0; }
Let's dissect what this means, line by line.
#include <stdio.h>This is a preprocessor directive.
#include command tells the C compiler to fetch an external file before it starts compiling.<stdio.h> stands for Standard Input Output header file. This file contains pre-written code that allows your program to take input from a keyboard and output data to the screen (like the printf function).printf means, and your program would fail to run.C ignores blank spaces and empty lines. We use blank lines simply to make our code more readable for humans.
int main()This is the most important part of any C program. It is the main function.
main(). This is the entry point of your program. When you execute your application, the computer looks for main() and starts executing the code from there.int keyword stands for integer. It indicates that this function will eventually "return" or output an integer value to the operating system when it finishes.{ (Opening Curly Bracket)The opening curly brace marks the beginning of the main() function's code block. All the instructions you want the main function to perform must be placed between the opening { and the closing }.
// Your code goes hereThis is a comment. Any text preceded by two forward slashes // is completely ignored by the compiler. Comments are used to leave notes for yourself or other developers.
return 0;This statement terminates the main() function.
0 tells the operating system: "The program executed successfully without any errors."1), signaling to the OS that something went wrong.} (Closing Curly Bracket)This marks the end of the main() function.
To prevent syntax errors, you must keep the following strict rules in mind:
In C, every executable instruction (called a statement) must end with a semicolon (;).
Think of the semicolon in C like a period at the end of a sentence in English. It tells the compiler where an instruction ends.
printf("Hello!"); // Correct: ends with a semicolon
return 0; // Correct: ends with a semicolon
Forgetting a semicolon is the most common mistake beginners make, and it will result in a compilation error!
C is a case-sensitive language. This means that uppercase and lowercase letters are treated as completely different entities.
For example:
printf is correct.Printf or PRINTF will cause an error because the compiler doesn't recognize them.Age is different from a variable named age.C compilers generally ignore extra spaces, tabs, and newlines. You could technically write your entire program on a single line, and it would still work!
However, doing so is considered terrible practice. We use indentation (spacing code inward using the Tab key) to make the code highly readable and organized.
// BAD FORMATTING (Will run, but impossible to read):
#include<stdio.h>
int main(){printf("Hi");return 0;}
// GOOD FORMATTING (Clean and structured):
#include <stdio.h>
int main() {
printf("Hi");
return 0;
}
#include necessary libraries, like <stdio.h> for input and output.main() function.{}.;.Which of the following characters is required to end a standard statement in C?