C Syntax
Understanding the basic structure and rules of C programming
📝 C Program Structure
Every C program follows a specific structure with essential components. Let's look at a simple C program:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Output:
Hello, World!
Key Syntax Components
Header Files
Include necessary libraries
#include <stdio.h>
Main Function
Entry point of every C program
int main() {
// code here
return 0;
}
Statements
Instructions ending with semicolon
printf("Hello!");
int x = 5;
Code Blocks
Group statements with braces
{
int a = 10;
printf("%d", a);
}
🔹 C Syntax Rules
C syntax rules define how code must be written to be
valid and executable. Identifiers like
variable and function names must start with a letter or
underscore, followed by letters, digits, or underscores.
Keywords like int, if, and
else have special meaning and cannot be
used as variable names. Case sensitivity means
age and Age are different
variables. Indentation and spacing improve readability
but aren't required by the compiler. Understanding these
syntax rules prevents frustrating errors and helps you
write clean, professional code that follows industry
standards.
// 1. Case sensitive
int age = 25; // 'age' and 'Age' are different
int Age = 30;
// 2. Semicolon required
printf("Hello World"); // Correct
// printf("Hello World") // Error: missing semicolon
// 3. Braces for blocks
if (age > 18) {
printf("Adult");
}
// 4. Comments
// This is a single line comment
/* This is a
multi-line comment */
Output:
Hello World
Adult
🔹 Variable Declaration
Properly declaring variables is critical for writing
C programs that compile and run
correctly. Variables must be declared
before use, establishing their type and name like
int number; float price; char grade;. The
declaration allocates memory based on the data type
chosen. You can initialize variables during declaration
using syntax like int age = 25;, combining
declaration and assignment in one statement. Good
practice includes declaring variables close to where
they're used and choosing meaningful names that describe
their purpose, making code more readable and
maintainable.
#include <stdio.h>
int main() {
// Variable declarations
int number = 42;
float price = 19.99;
char grade = 'A';
// Using variables
printf("Number: %d\n", number);
printf("Price: %.2f\n", price);
printf("Grade: %c\n", grade);
return 0;
}
Output:
Number: 42
Price: 19.99
Grade: A
🔹 Basic Program Template
Using a consistent program template speeds up
development and ensures you include all
necessary components. Start with
#include <stdio.h> to include the
standard input/output library, followed by the
int main() function definition. Inside
main, write your program logic, declare variables, and
perform operations. End with return 0; to
indicate successful completion. This template provides
the minimum structure needed for valid C programs.
Following this template consistently across all your
programs builds good habits and prevents common mistakes
like missing headers or forgetting the main function.
#include <stdio.h> // Standard input/output library
int main() { // Main function starts here
// Your code goes here
printf("Welcome to C programming!\n");
return 0; // Program ends successfully
} // Main function ends here
Output:
Welcome to C programming!