Before diving into advanced programming concepts, it’s important to understand how basic syntax works in different Computer Programming languages. Syntax refers to the rules and structure that determine how code must be written so the computer can understand and execute it correctly.
In this chapter, we will start with simple “Hello, World!” examples in C, Java, and Python to help you understand the foundations of program structure, functions, statements, comments, whitespaces, and syntax errors.
Table of Contents:
Writing Your First Program
Let’s begin with a simple task—displaying the message “Hello, World!” on the screen. This is usually the first program beginners write, regardless of the programming language.
Below are examples of the same program written in C, Java, and Python.
Hello World Program in C
#include <stdio.h>
int main() {
/* printf() function to write Hello, World! */
printf("Hello, World!");
}
Output:
Hello, World!
Try changing the text inside printf() and observe the output. Whatever you place within double quotes gets printed on the screen.
Understanding Program Structure in C
Program Entry Point
Every C program starts with the function main(). The opening { marks the beginning of the program body, and the closing } marks its end. Everything between these braces is part of the program.
Functions
Functions are blocks of code created for specific tasks.
In the above program:
main()is the entry pointprintf()prints text to the screen
C has many built-in functions, and you can also create your own.
Comments
Comments help explain code. They are ignored by the compiler.
In C:
/* This is a comment */
You can write comments in any language you prefer since they do not affect program execution.
Whitespaces in Programming
Whitespace characters include:
- New Line →
\n - Tab →
\t - Space
These make your code readable but are usually ignored by the compiler.
Example with extra spaces (still works fine):
#include <stdio.h>
int main() {
/* printf() function */
printf( "Hello, World!" );
}
Semicolons in C
Every statement in C must end with a semicolon (;).
Example printing two lines:
printf("Hello, World!\n");
printf("Hello, World!");
Without \n, both messages appear on the same line.
Explaining the C Program Execution Process
- Write the program (e.g., in
test.c) - Compile it:
gcc test.c -o demo - If syntax errors exist, fix them
- Execute it:
./demo
Output:
Hello, World!
Syntax Errors in Programming
A syntax error occurs when you break the rules of the programming language.
Example (missing semicolon):
#include <stdio.h>
int main() {
printf("Hello, World!")
}
Compiler output:
error: expected ';' before '}' token
A single missing symbol is enough to break the entire program.
Hello World Program in Java
public class HelloWorld {
public static void main(String[] args) {
/* println() function to write Hello, World! */
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
Java programs must be compiled before execution, similar to C.
Hello World Program in Python
# print function to write Hello, World!
print("Hello, World!")
Output:
Hello, World!
Python is an interpreted language, which means:
- No compilation is required
- New lines, not semicolons, mark statement endings
Key Differences Between C, Java, and Python
| Feature | C | Java | Python |
|---|---|---|---|
| Needs compilation | Yes | Yes | No |
| Semicolon required | Yes | Yes | No |
| Entry point | main() | main() method | No fixed structure |
| Print function | printf() | System.out.println() | print() |
basic programming syntax, hello world program, syntax error examples, C program structure, Java hello world, Python print statement, programming basics for beginners
Frequently Asked Questions (FAQ)
Why do most tutorials start with “Hello, World!”?
It is a simple way to demonstrate the minimum structure required to run a program in any language.
Why does C require semicolons?
Semicolons tell the compiler where a statement ends, helping it parse the code correctly.
Why doesn’t Python need semicolons?
Python uses new lines and indentation to determine code structure.
What causes syntax errors?
Missing characters, incorrect symbols, or violating language rules cause syntax errors.
Why does C use main() as the entry point?
It tells the compiler where the program begins execution.
Do all programming languages require compilation?
No. Languages like C and Java require compilation, while Python and PHP use interpreters.