A comprehensive guide to understanding C program components
Single-line: // This is a comment
Multi-line: /* This is a multi-line comment */
// Program to add two numbers/* This program demonstrates how to add numbers in C */#include <stdio.h> // Function to add two integers int add(int a, int b) { return a + b; // Return the sum }/* * Main function - Entry point of the program * Purpose: Demonstrate addition of two numbers * Author: Your Name * Date: Today's Date */int main() { int num1 = 10; // First number int num2 = 20; // Second number int result; // Variable to store result result = add(num1, num2); // Call add function // Display the result printf("Sum of %d and %d is %d\n", num1, num2, result); return 0; // Indicate successful execution }
// This is a simple C program #include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
#include <stdio.h> - Includes standard input/output libraryint main() - Main function declaration returning an integerprintf() - Function to print text to consolereturn 0; - Indicates successful program termination// Header file inclusion #include <stdio.h> // Standard I/O #include <stdlib.h> // Standard library #include <math.h> // Math functions // Macro definitions #define PI 3.14159 #define MAX_SIZE 100 #define SQUARE(x) ((x) * (x)) // Conditional compilation #ifdef DEBUG #define PRINT_DEBUG(msg) printf("DEBUG: %s\n", msg) #else #define PRINT_DEBUG(msg) #endif int main() { double radius = 5.0; double area = PI * SQUARE(radius); printf("Area of circle: %.2f\n", area); PRINT_DEBUG("Calculation completed"); return 0; }
#include - Include header files#define - Define macros and constants#ifdef/#ifndef - Conditional compilation#pragma - Compiler-specific instructions#include <stdio.h> // Global variables int global_counter = 0; // Initialized global variable char global_message[50]; // Global array const double GRAVITY = 9.81; // Global constant // Global structure definition struct Student { int id; char name[30]; float gpa; }; // Function prototypes (declarations) void increment_counter(void); void display_student(struct Student s); double calculate_weight(double mass); int main() { struct Student student1 = {101, "John Doe", 3.75}; printf("Initial counter: %d\n", global_counter); increment_counter(); printf("After increment: %d\n", global_counter); display_student(student1); double mass = 70.5; printf("Weight: %.2f N\n", calculate_weight(mass)); return 0; } // Function definitions void increment_counter(void) { global_counter++; } void display_student(struct Student s) { printf("Student ID: %d, Name: %s, GPA: %.2f\n", s.id, s.name, s.gpa); } double calculate_weight(double mass) { return mass * GRAVITY; }
/*
int main(int argc, char *argv[]) {
printf("Program name: %s\n", argv[0]);
printf("Number of arguments: %d\n", argc);
for(int i = 1; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
*/
// Alternative: main with environment variables
/*
int main(int argc, char *argv[], char *envp[]) {
printf("Environment variables:\n");
for(int i = 0; envp[i] != NULL; i++) {
printf("%s\n", envp[i]);
}
return 0;
}
*/
// Alternative: void main (not recommended)
/*
void main() {
printf("Void main - not standard compliant\n");
}
*/
0 - SuccessNon-zero - ErrorEXIT_SUCCESS - Success macroEXIT_FAILURE - Failure macro
/*
* Student Management System
* Demonstrates complete C program structure
* Author: P rogramming Tutorial
*/
// Preprocessor directives
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Macro definitions
#define MAX_STUDENTS 50
#define NAME_LENGTH 50
// Global structure definition
struct Student {
int id;
char name[NAME_LENGTH];
float gpa;
int age;
};
// Global variables
struct Student students[MAX_STUDENTS];
int student_count = 0;
// Function prototypes
void add_student(int id, char *name, float gpa, int age);
void display_students(void);
void find_student(int id);
float calculate_average_gpa(void);
void display_menu(void);
// Main function - Program entry point
int main() {
int choice, id, age;
char name[NAME_LENGTH];
float gpa;
printf("=== Student Management System ===\n\n");
// Sample data
add_student(101, "Alice Johnson", 3.8, 20);
add_student(102, "Bob Smith", 3.5, 21);
add_student(103, "Carol Davis", 3.9, 19);
while(1) {
display_menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter ID: ");
scanf("%d", &id);
printf("Enter name: ");
scanf("%s", name);
printf("Enter GPA: ");
scanf("%f", &gpa);
printf("Enter age: ");
scanf("%d", &age);
add_student(id, name, gpa, age);
break;
case 2:
display_students();
break;
case 3:
printf("Enter student ID to find: ");
scanf("%d", &id);
find_student(id);
break;
case 4:
printf("Average GPA: %.2f\n", calculate_average_gpa());
break;
case 5:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
printf("\n");
}
return 0;
}
// Function to add a new student
void add_student(int id, char *name, float gpa, int age) {
if(student_count < MAX_STUDENTS) {
students[student_count].id = id;
strcpy(students[student_count].name, name);
students[student_count].gpa = gpa;
students[student_count].age = age;
student_count++;
printf("Student added successfully!\n");
} else {
printf("Maximum student limit reached!\n");
}
}
// Function to display all students
void display_students(void) {
if(student_count == 0) {
printf("No students found!\n");
return;
}
printf("\n--- Student List ---\n");
printf("ID\tName\t\tGPA\tAge\n");
printf("----------------------------------------\n");
for(int i = 0; i < student_count; i++) {
printf("%d\t%-15s\t%.2f\t%d\n",
students[i].id, students[i].name,
students[i].gpa, students[i].age);
}
}
// Function to find a student by ID
void find_student(int id) {
for(int i = 0; i < student_count; i++) {
if(students[i].id == id) {
printf("Student Found:\n");
printf("ID: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("GPA: %.2f\n", students[i].gpa);
printf("Age: %d\n", students[i].age);
return;
}
}
printf("Student with ID %d not found!\n", id);
}
// Function to calculate average GPA
float calculate_average_gpa(void) {
if(student_count == 0) return 0.0;
float total = 0.0;
for(int i = 0; i < student_count; i++) {
total += students[i].gpa;
}
return total / student_count;
}
// Function to display menu
void display_menu(void) {
printf("\n=== MENU ===\n");
printf("1. Add Student\n");
printf("2. Display All Students\n");
printf("3. Find Student\n");
printf("4. Calculate Average GPA\n");
printf("5. Exit\n");
}