Introduction to Encapsulation in C
There have been instances when due to business requirements we need to write complex code. This code will not be user-friendly and looks hard to understand. To make the program look easier to read we can wrap up the complex snippet of code in a capsule and hide it. This will also serve as an added layer of protection against any malicious injections in the code. We can then use this piece of code by calling its instance in the main program rather than writing the whole bunch of complex lines. This feature is called encapsulation. It is one of the core features of Object-oriented languages. However, it is not only limited to OOP languages only. In C, encapsulation has been despite the absence of private and public keywords. Encapsulation is being used by various other programming languages like C#, C++, PHP, JAVA as well.
Working of Encapsulation in C
To understand the working of encapsulation let’s consider the real-life example. Consider a big company that has its own in-house production unit of books and delivers them to customers by tying up with third-party vendors. To make this business model work there will be different departments involved like the procurement department, production department, sales department, finance department. The procurement department will take care of all procurement linked activities like procurement of ink, papers, paper covers, printers, etc. The sales department will work on marketing, record sales. Finance department responsibilities to conduct the financial audits, calculate profits/losses and publish reports on the basis of which key strategic decisions will be taken. In case sales went down and this impacted the finances of the business. Then the Finance department will not be allowed to directly access sales data. The finance department will have to request a member of the sales team to get the relevant data. This is Encapsulation. All activities of finance are wrapped under one entity called the “finance department”.
This concept of encapsulation is used in C language for data hiding and protection. It can be implemented when the main calling program has an object, the object should be able to find the functions applicable and in the same way, they find the data.
Advantages of Encapsulation in C
- Encapsulation hides the implementation of a program and hence easy to read and modify in the future according to business requirements.
- It allows us to deploy the updated code version wherever required, without requiring the whole program to be restructured.
- It secures the program by providing data hiding functionality.
- Encapsulation promotes a modular way of programming making code resilient.
Why Do We Need Encapsulation in C?
We need encapsulation in C to manipulate the access modifiers in C. The access modifiers are explicitly present in C++ for e.g. Public, private, but they are not explicitly present in C. Although we can make use of this property in C by implementing encapsulation. Encapsulation also provides secure code which can be better understood by an example provided in the below section. This promotes adaptability with changing requirements as whichever code requires a change can be modified in a separate file without changes anything in the main program. This will give simple and error-free code up to some extent.
Example of Encapsulation in C
Data in C language is public by default. Although “Struct” variables can be declared private by defining them separately from the main class. This can be achieved by having a separate header and source C files. A header file is followed by the “.h” extension while C files are followed by the “.C” extension. In the below example: There are three files
p_variable.h: It is a header file that is to be included in other “.c” files. This file acts as a link between the data scattered over multiple files.
main_prog.c: It is the main implementation file. When this is executed then function call is made to functions in which are defined in other C files along with structures.
Access_pfile.c: It is a file containing structure. Because “struct” requires allocation and de-allocation of memory, some functions from standard C library like “malloc()” and “alloc()” are used.
struct Con; // It is a variable to store contact
struct Con * create_contact(); // functional call to create function
void delete_contact( struct Con * some_contact ); // functional call to delete function
#include "p_variable.h" //we hav included header file in this file so as to access the structure members. This is //an indirect way of accessing structures and thus implementing encapsulation.
struct Con //structure definition containing two members. Both the members are integer type
struct Con * create_contact() // structure declaration. In this code section we allocate memory for the data we //need to input in the above defined members. Malloc function allocates the memory.
struct Con * some_contact;
some_contact = malloc(sizeof(struct Con));
some_contact->mob_number = 1234567891;
some_contact->flat_number = 541;
return( some_contact );
void delete_contact( struct Con * some_contact )
free(some_contact); // this is tandard C function to de- allocate the memory. It frees up the memory so that //the same can be used by other programs.
struct Con * Meghna;
Meghna = create_contact();
printf( "Mobile number: %d\n", Meghna->mob_number); // This should cause compile time error as we are //trying to access the private struct member
delete_contact( Meghna );
Encapsulation is the leading step towards object-oriented programming. This article gives information about Encapsulation in C. Using accessor and mutator methods, access modifiers we can make use of encapsulation in C#, C++, PHP as well. The benefit of properties is that the users can manipulate the object from an internal data point of view using a single named item having data and functionality defined in it.
This is a guide to the Encapsulation in C. Here we discuss the introduction and need of encapsulation in C along with Advantage and examples. You can also go through our other suggested articles to learn more –