Advent Calendar #13 - C is object-oriented?

Alexander PanovAlexander Panov
1 min read

Although this does not fit the current series of javascript, typescript and web development paths, I still want to talk about, it because I recently learned about it.

For a long time, I thought, that functional programming was called functional programming, because it does not allow object-oriented programming.

I missed, that object-oriented languages are written in functional programming languages. Therefore there has to be a possibility.

And well it's quite easy to implement the most basic object-oriented features within very basic steps.

Structs

Structs are used to group certain correlating variables. A struct is our class, without encapsulation and functions on the objects.

Let's look at a simple user class. Written in Typescript like this.

class User {
  public username: string;
  public admin: boolean;


  public sayName() {
     return "Hello " + this.username;
  }
}

The same can be built in C.

struct User {
  char username[32];
  int admin;
}

void say_name(User *user) {
  printf("Hello %s", user->username);
}

Conclusion

The whole point of this mini-post is to show you, that C just because it is a functional programming language, does not mean, that there are just primitives and you are doing maths all the time.

I did this to show you my latest learnings about it as I was quite amazed.

0
Subscribe to my newsletter

Read articles from Alexander Panov directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Alexander Panov
Alexander Panov

Software developer and CEO at RoyalZSoftware. I build web applications for startups with Ruby on Rails, Angular and React.