Basic Structure and Syntax of C Programming Language
🎯 Lab Objective
The objective of this lab is to:
- Understand the basic structure of a C program.
- Learn the role of header files, main function, and statements.
- Write, compile, and execute a simple C program.
- Get familiar with syntax rules such as semicolons, case-sensitivity, and indentation.
📖 Background
C is one of the most widely used programming languages, developed by Dennis Ritchie in the early 1970s at Bell Labs. It is the foundation of many modern languages (like C++, Java, and Python).
A C program typically contains:
- Preprocessor directives (like
#include) - Main function (
int main()) - Variable declarations
- Statements for input/output, processing, and logic
- Return statement to end the program
🧩 General Structure of a C Program
#include <stdio.h> // Preprocessor directive
// Main function: program execution starts here
int main() {
// Variable declaration
int number;
// Input from user
printf("Enter a number: ");
scanf("%d", &number);
// Output
printf("You entered: %d\n", number);
return 0; // Exit status
}
##🔎 Explanation of Each Part
- Preprocessor Directives
Lines starting with # are instructions to the compiler before actual compilation.
Example:
#include <stdio.h>
Includes the Standard Input Output library so that we can use printf() and scanf()
- Main Function
Every C program must have main() Execution begins here:
int main() { ... }
- Variable Declarations
All variables must be declared before use. Example:
int number;
- Input and Output
- Input:
scanf("%d", &number); - Output:
printf("You entered: %d\n", number);- Return Statement
Ends the program and returns control to the operating system:
return 0;
Syntax Rules in C
- Every statement must end with a semicolon (;).
- C is case-sensitive (main ≠ Main).
- Curly braces {} define the beginning and end of code blocks.
- Indentation and spacing are not mandatory, but they improve readability.
Enjoy Reading This Article?
Here are some more articles you might like to read next: