C programming follows a structured syntax with specific rules that must be followed to create functional
programs. Here’s an overview of the basic syntax and structure of C programming:
1. Basic Structure of a C Program
A typical C program consists of the following sections:
CopyEdit
#include <stdio.h> // Preprocessor directive
// Function prototype (optional)
void greet();
int main() // Main function: program execution starts here
printf("Hello, World!\n"); // Print statement
greet(); // Function call
return 0; // Return statement
// Function definition
void greet() {
printf("Welcome to C programming!\n");
2. Basic Components of C Syntax
a) Header Files and Preprocessor Directives
These include necessary libraries.
Syntax: #include <library_name>
Example: #include <math.h> for mathematical operations.
b) The main() Function
Every C program must have a main() function.
It serves as the entry point for execution.
c) Statements and Blocks
Each statement ends with a semicolon (;).
Blocks are enclosed within curly braces {}.
d) Comments
Single-line comment: // This is a comment
Multi-line comment: /* This is a multi-line comment */
3. C Data Types
C provides different data types for handling variables.
Data Type Size Example
int 4 bytes int age = 25;
float 4 bytes float pi = 3.14;
double 8 bytes double precise = 3.141592;
char 1 byte char grade = 'A';
void 0 bytes Used for functions with no return value
4. Control Flow Statements
a) Conditional Statements
if, else if, else for decision-making.
CopyEdit
if (age >= 18) {
printf("Adult\n");
} else {
printf("Minor\n");
}
b) Loops
Used to repeat a block of code.
CopyEdit
// for loop
for (int i = 0; i < 5; i++) {
printf("%d ", i);
// while loop
int count = 0;
while (count < 5) {
printf("%d ", count);
count++;
5. Functions in C
C programs can be modularized using functions.
CopyEdit
int add(int a, int b) {
return a + b;
int main() {
int sum = add(5, 3);
printf("Sum = %d", sum);
return 0;
}
6. Input and Output Functions
The stdio.h library provides I/O functions.
CopyEdit
// Input
int age;
printf("Enter your age: ");
scanf("%d", &age);
// Output
printf("You entered: %d", age);
7. Operators in C
Common operators include:
Arithmetic: +, -, *, /, %
Relational: ==, !=, >, <, >=, <=
Logical: &&, ||, !
Bitwise: &, |, ^, <<, >>
Assignment: =, +=, -=, *=, /=
Increment/Decrement: ++, --
8. Arrays and Strings
Handling multiple values using arrays.
CopyEdit
int numbers[5] = {1, 2, 3, 4, 5};
char name[] = "John";
9. Pointers in C
Pointers store memory addresses.
CopyEdit
int x = 10;
int *ptr = &x;
printf("Value of x: %d", *ptr);
10. Memory Management
malloc(), calloc(), free() are used for dynamic memory allocation.
11. File Handling
C allows file operations like reading and writing.
CopyEdit
FILE *file = fopen("example.txt", "w");
fprintf(file, "Hello, file!");
fclose(file);
Summary of C Programming Structure
1. Preprocessor directives (#include)
2. Global variables and function declarations
3. main() function
4. User-defined functions
5. Return statement in main()