C Language: Returning Home for the Spring Festival, I Found Out I’m the Only One Without a Partner!

Gathering

During the Spring Festival, I returned home and encountered many friends: Java, Python, JavaScript, Ruby…

Everyone has been doing well in the big city, returning to their hometowns to gather for meals, chatting and laughing, all in high spirits.

Especially Python and JavaScript, who have become the stars, one boasting about being essential for artificial intelligence, while the other flaunts being the most popular language in the world, citing some language popularity rankings as proof, along with numerous projects on GitHub.

The seasoned Java keeps bringing up the TIOBE rankings: “I’ve been ranked first for over 10 years, it’s lonely at the top!”

Speaking of TIOBE, Python is even prouder: “I was selected as the TIOBE Programming Language of the Year this year!”

Although C language has been ranked second in TIOBE for many years, it feels a bit downcast. Humans have written many programs using it, but they are all at the lower level, in system-level programming, such as operating systems, databases, compilers… Compared to the application layer, it doesn’t seem as glamorous.

Now many people who have trained in Python and Java claim to know programming, but they don’t understand pointers, memory, or the basic principles of the lower levels. Can they really be considered programmers?

C language starts to feel indignant, burying its head in food, seemingly wanting to vent its frustration on the delicious dishes.

Amidst the clinking of glasses, Java puts an arm around C’s shoulder and kindly asks, “Brother, do you have a partner?”

This stirred up a hornet’s nest, and everyone’s eyes turned to C language.

C stammered for a long time: “No… I don’t have one.”

“Hahaha… We all have partners, and you’re still single at your age?!” Python laughed.

“Yeah, what future does a programming language without a partner have?” JavaScript added, who originally had no concept of class and implemented OOP through prototypes, only recently introducing the class keyword at the syntax level.

“Even though I don’t have a partner, I have pointers, which are very powerful.”

“Pointers? Are you talking about those error-prone pointers? Who even uses pointers anymore?” JavaScript said.

“If you can’t use pointers, you’re not a real programmer!” C language’s face turned red.

The atmosphere at the dining table became a bit awkward, and Java, who stirred up trouble, quickly said, “Come on, let’s keep drinking.”

After finally enduring until the meal ended, C language returned home, which was cold and empty. Its “father” Dennis Ritchie, one of the greatest programmers of all time, unfortunately passed away in October 2011.

On the table lay a book titled “The C Programming Language,” which was Dennis Ritchie’s only posthumous work. Picking up this book, C couldn’t help but feel a wave of sadness.

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

Visiting

C language suddenly remembered Ken Thompson next door, who was Dennis Ritchie’s “good buddy.” Together, they created the great Unix operating system and received the highest award in the computer field: the Turing Award.

Why not ask Ken? Why can’t I have a partner? Why can’t I do object-oriented programming!

C went to Ken Thompson’s door, rang the bell, and when the door opened, C saw Ken Thompson happily playing with Go, feeling even more miserable. Go is the favored child, and I am nothing, so I turned to leave.

But Ken called out from behind: “Little C, come in and play with your brother Go for a while.”

Seeing C’s dejected face, Ken was also surprised: “What’s wrong during the New Year?”

C complained: “Why didn’t you let me have a partner back then?”

“Partner? What partner? Oh, you mean object-oriented programming! Actually, your father designed you mainly for system-level programming, which requires closeness to hardware and efficiency. Why bother with that complex stuff? It’s pretty but useless. Besides, like Go, you have structs, right?” Ken winked at Go.

“Yes, yes, structs are very useful!” Go immediately agreed.

“But structs can’t implement OOP! Python and JavaScript all mock me!”

“Then tell me, what is OOP?” Ken asked.

“Um, it’s encapsulation, inheritance, and polymorphism, right?” C replied.

“Good, let me show you how to implement encapsulation, inheritance, and polymorphism in C language!”

Encapsulation

Ken Thompson, true to his reputation, quickly wrote a piece of code.

He said: “First, let’s talk about encapsulation. Encapsulation is about hiding information. Take a look at this piece of code.”

shape.h

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

shape.c

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

main.c

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

Here, a structure called Shape is defined, and the outside world can only operate on this Shape through related functions, such as creating (Shape_create), moving (Shape_move), and getting position (Shape_getX), etc., without directly accessing the internal data structure of Shape.

Although there is no class keyword here, the data structure and related operations are written separately, which may not look perfect, but it indeed achieves encapsulation.

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

C saw that Ken Thompson actually named that pointer self, just like in Python, and couldn’t help but laugh: “I understand now, but how do we do inheritance?”

Inheritance

Ken Thompson remained silent and continued to write code.

It seems that the style of great programmers is similar: stop talking nonsense and just show me the code.

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

This time, a rectangle (Rectangle) structure is defined, which contains a nested Shape. Does this implement inheritance? C is a bit confused.

Go chimed in: “I understand, in memory, they look like this.”

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

Through this combination method, it can also be considered as implementing inheritance.

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

Polymorphism

Having easily implemented encapsulation and inheritance, C language felt very excited, but how to implement polymorphism?

At this moment, the doorbell rang again, and Linus entered with a bottle of wine, looking for C to drink. Upon seeing the code on the table, he immediately understood what was going on.

He said: “Stop with all the fancy stuff, polymorphism is just function pointers! Let me give you an example.”

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

“This structure contains two function pointers, one for calculating the area of the shape and the other for drawing the shape. We call this structure a virtual function table.”

“What use is that?”

“In your Shape, just add a pointer to that function table.” Linus replied.

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

C and Go both looked puzzled.

“Dummies, think about it. When you create a subclass object, like Rectangle, and point that virtual function pointer vptr to another set of functions, what will happen?”

The two still didn’t understand, so Linus had to continue drawing:

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

Now C understands a bit. Whether it’s a Rectangle object or a Square object, when calling the Shape_area method, it needs to find the area method in the virtual function table through the vptr pointer. For Rectangle, it finds the Rectangle_area method, and for Square, it finds the Square_area method.

struct Rectangle *r = Rectangle_create(5,5,10,10);

Shape_area((struct Shape *) r);

“Actually, the polymorphism implementation principle in your brother C++ is similar! It looks up the real function to execute at runtime.” Ken summarized.

“Right, this method of using function pointers is very common. Similar things are defined in my Linux operating system too,” Linus added.

“As long as the IO devices provide the actual definitions of these functions, the function pointers in the File structure can point to the corresponding implementations, achieving the same interface to operate different IO devices.”

C language became happy: “Haha, I told you my pointers are powerful, all of this is achieved through pointers.”

“Yes, don’t listen to Java, Python, and JavaScript when they talk nonsense. You also have a partner and can do object-oriented programming!”

C language said: “Let’s go drink!”

Note: The examples in this article are mainly sourced from https://www.state-machine.com/doc/AN_OOP_in_C.pdf, with modifications made by me.

You might also like

About Old Liu and the Code Farmer’s Reversal

I Am a Thread

I Am a Java Class

The Bible of Object-Oriented Programming

The Great Postman of TCP/IP

CPU Forrest Gump

I Am a Network Card

I Am a Router

A Story About HTTPs

The Pinnacle of Programming Languages

Java: The Birth of an Empire

JavaScript: A Loser’s Comeback

JavaScript: How a World Without Classes Plays with Object-Oriented Programming

C Language: Returning Home for the Spring Festival, I Found Out I'm the Only One Without a Partner!

Leave a Comment