C is a procedural programming language, but it supports modularity through functions and can implement object-oriented programming (OOP) principles using structures and pointers. This tutorial covers how to use functions and simulate OOP concepts in C.
Functions in C help in modularizing the code, making it reusable and easier to debug.
Here’s how to define and use a function:
#include <stdio.h>
// Function declaration
void greet() {
printf("Hello from a function!\n");
}
int main() {
greet(); // Function call
return 0;
}
Functions can take parameters and return values:
#include <stdio.h>
// Function with parameters and return value
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("Sum: %d\n", result);
return 0;
}
Though C is not an OOP language, structures help in organizing data like objects:
#include <stdio.h>
// Defining a struct (similar to a class in OOP)
struct Person {
char name[50];
int age;
};
// Function to print person details
void displayPerson(struct Person p) {
printf("Name: %s, Age: %d\n", p.name, p.age);
}
int main() {
struct Person person1 = {"Alice", 25};
displayPerson(person1);
return 0;
}
Encapsulation can be achieved by using functions to modify struct data:
#include <stdio.h>
#include <string.h>
// Defining a struct
struct Car {
char brand[50];
int year;
};
// Function to update car year
void updateCarYear(struct Car *car, int newYear) {
car->year = newYear;
}
int main() {
struct Car myCar;
strcpy(myCar.brand, "Toyota");
myCar.year = 2020;
printf("Before update: %s - %d\n", myCar.brand, myCar.year);
updateCarYear(&myCar, 2023);
printf("After update: %s - %d\n", myCar.brand, myCar.year);
return 0;
}
Function pointers allow function behavior to change at runtime, similar to polymorphism in OOP:
#include <stdio.h>
// Function pointer example
void sayHello() {
printf("Hello!\n");
}
void sayGoodbye() {
printf("Goodbye!\n");
}
int main() {
void (*messageFunc)();
messageFunc = sayHello;
messageFunc(); // Calls sayHello
messageFunc = sayGoodbye;
messageFunc(); // Calls sayGoodbye
return 0;
}
Inheritance can be mimicked by including one struct in another:
#include <stdio.h>
// Base struct
struct Animal {
char species[50];
};
// Derived struct
struct Dog {
struct Animal base;
char breed[50];
};
int main() {
struct Dog myDog;
strcpy(myDog.base.species, "Canine");
strcpy(myDog.breed, "Labrador");
printf("Species: %s, Breed: %s\n", myDog.base.species, myDog.breed);
return 0;
}
Practice by creating a program that models a real-world object using structs and functions. Explore more complex function pointers and memory management for better OOP-style programming in C.