Are there Classes in the C Programming Language?


If you are new to programming or come from an object oriented language like Java or C# you may ask yourself if there are classes in C. It is a widely used language and still popular today despite its age. This article explains in detail if there are classes in C and why it may be important.

There are no classes in the C Programming Language. There is no keyword class and no concept of a class as defined in the object oriented programming world.

But why is this and what does it mean for your programming experience? What are the alternatives? Discover the answers in the following paragraphs.

Why does the C language have no Classes?

C is an imperative and procedural language. Instructions are grouped into sequences (procedures) which are executed step by step. A procedure in C is a subroutine, also referred to as “function”.

Image from StockSnap at Pixabay

The origin of the C programming langue comes from ALGOL, an imperative programming language developed in 1958. The language B arose from it in order to create a simplified subset of ALGOL. During the development of the UNIX operating system, the developer Dennis Ritchie improved B to what eventually would become C.

In ALGOL there was no concept of classes or object orientation so it was not considered in the conception of B and later C. Although the concept of object orientation was present since the 1950’s, the first object oriented programming language was not released before 1972 (Smalltalk).

Is C++ C with classes?

Originally C++ was called “C with Classes”. Its goal was to extend the C language with the common features of an object oriented language.

In 1979 the danish developer Bjarne Stroustrup began to work on a language he then called “C with Classes”. It was renamed in 1983 to C++ which is also very popular and heavily used to this day. The first official release of C++ was in 1985.

Is there an equivalent for Classes in C?

There is no real equivalent for a class in C. What comes closest is a so called struct.
A struct is defined like this:

struct person {
  char firstName[20];
  char lastName[30];
  int  age;
};

The main differences between a class and a struct is, that the members are public by default. In a class everything is private by default. Also you cannot define methods (functions) in a C struct. A member in a C struct can never be private because there is no “private” in the C programming language (although in some circumstances the keyword static can fulfill the same intention).

You may know that C++ also has a struct, but there are differences between them:

C StructC++ StructC++ Class
All Members are always PublicPublic by Default, but can be PrivatePrivate by Default
No Methods allowedMethods allowed (i.e. Initializer)Methods allowed
Comparing C Struct and C++ Struct

In fact, the only difference between a class and a struct in C++ is the default visibility of its members (public for struct and private for classes).

You can emulate the behaviour of classes and objects in C with structs, the keyword static, dynamic memory allocation and a disciplined naming convention.

Are Classes Important for C Anyway?

Object Orientation is just one of many concepts in the programming world. Some languages are designed to suit better for one style than another. C as procedural language may not be designed for object orientation but it is a powerful language that can be used on almost every system and platform.

If you really want classes and the power of C then you should go for C++. It’s heavier than C but you have a lot of features and extensions like templates or smart pointers that you won’t find in C. If you can go with another programming paradigm and a lighter approach then stay with C.

If you want to emulate classes and object orientation in C then there is another article for you. You can even emulate Functional Programming with C which is also explained in a different article.

Example: Structs (in C) vs. Classes (in C++)

To conclude this article we will compare two solutions of a simple problem: Let’s assume that we want to store the name and age of a person and print it to the screen. We have to provide a data structure, initialize it and then print it to the console. We will do it with a C struct and C++ class and see the differences.

Please note that the examples are oversimplified and shall only prove the point. If you want to know what the output is just copy and paste the code to your favourite IDE or editor, compile it with a C/C++ compiler of your choice and run the program. For a quick run you can also use an online compiler like https://www.onlinegdb.com/Opens in a new tab. (link opens in new tab).

Example in C

In C you group the related data into a struct and perform all operations on it in the code as you go. You modify the attributes directly. This is fast and to the point, but the data could be manipulated in not intended ways because of the missing encapsulation.

#include <stdio.h>

struct person {
  char firstName[20];
  char lastName[30];
  int  age;
};

int main() {
    struct person myPerson = {"John", "Doe", 28};

    printf("Hello %s %s! You are %d years old.\n", 
           myPerson.firstName, myPerson.lastName, myPerson.age);

    myPerson.age++;
    
    printf("Hello %s %s! You are now %d years old.\n", 
           myPerson.firstName, myPerson.lastName, myPerson.age);

    return 0;
}

Example in C++

In C++ (and with a object oriented approach) you encapsulate the data into a class. You modify the attributes indirectly by the usage of methods. This ensures that private data will not be manipulated in a way that was not originally intended. The downside is, of course, the greater overhead in code and operations which can slow down the process and be more prone to bugs.

#include <iostream>
#include <string>

class Person {
public:
    Person(std::string firstName, std::string lastName, int age) {
        this->firstName = firstName;
        this->lastName  = lastName;
        this->age       = age;
    }
    
    std::string getFullName() { return this->firstName + " " + this->lastName; }
    int getAge() { return this->age; }
    
    void happyBirthday() { this->age++; }

private:
    std::string firstName;
    std::string lastName;
    int age;
};


int main()
{
    Person myPerson("John", "Doe", 28);
    std::cout << "Hello " << myPerson.getFullName() << "! ";
    std::cout << "You are " << myPerson.getAge() << " Years old." << std::endl;

    myPerson.happyBirthday();

    std::cout << "Now you are " << myPerson.getAge() << " Years old." << std::endl;

    return 0;
}

So although there are no classes in C you should give this language a try even if you come from an object oriented language. I even wrote an article about how to emulate classes and other object oriented features in C!

Marco Lieblang

Professional Programmer since 2003, passionate Programmer since the mid 90's. Developing in many languages from C/C++ to Java, C#, Python and some more. And I also may know a bit about Assembly Languages and Retro Systems.

Recent Posts