From 4a800a5a85530d5f761be321aa30759ba6d9bed9 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 9 Apr 2023 13:52:04 +0300 Subject: Update content for gemtext --- ...-20-object-oriented-programming-with-ansi-c.gmi | 10 +- gemfeed/atom.xml | 170 +++++++++++++-------- 2 files changed, 110 insertions(+), 70 deletions(-) (limited to 'gemfeed') diff --git a/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.gmi b/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.gmi index a5a779e5..1ad18aa8 100644 --- a/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.gmi +++ b/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.gmi @@ -17,7 +17,7 @@ You can do a little of object-oriented programming in the C Programming Language Let's have a look at the following sample program. All you have to do is to add a function pointer such as "calculate" to the definition of struct "something_s". Later, during the struct initialization, assign a function address to that function pointer: -``` +```c #include typedef struct { @@ -53,21 +53,21 @@ int main(void) { As you can see, you can call the function (pointed by the function pointer) with the same syntax as in C++ or Java: -``` +```c printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b)); printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b)); ``` However, that's just syntactic sugar for: -``` +```c printf("%s(%f, %f) => %f\n", mult.name, a, b, (*mult.calculate)(a,b)); printf("%s(%f, %f) => %f\n", div.name, a, b, (*div.calculate)(a,b)); ``` Output: -``` +```sh pbuetow ~/git/blog/source [38268]% gcc oop-c-example.c -o oop-c-example pbuetow ~/git/blog/source [38269]% ./oop-c-example Multiplication(3.000000, 2.000000) => 6.000000 @@ -80,7 +80,7 @@ Not complicated at all, but nice to know and helps to make the code easier to re However, that's not really how it works in object-oriented languages such as Java and C++. The method call in this example is not a method call as "mult" and "div" in this example are not "message receivers". I mean that the functions can not access the state of the "mult" and "div" struct objects. In C, you would need to do something like this instead if you wanted to access the state of "mult" from within the calculate function, you would have to pass it as an argument: -``` +```c mult.calculate(mult,a,b)); ``` diff --git a/gemfeed/atom.xml b/gemfeed/atom.xml index 20fcf3d5..1a87ed65 100644 --- a/gemfeed/atom.xml +++ b/gemfeed/atom.xml @@ -1,6 +1,6 @@ - 2023-04-09T13:33:53+03:00 + 2023-04-09T13:51:48+03:00 foo.zone feed To be in the .zone! @@ -5501,8 +5501,10 @@ Total time: 1213.00s You can do a little of object-oriented programming in the C Programming Language. However, that is, in my humble opinion, limited. It's easier to use a different programming language than C for OOP. But still it's an interesting exercise to try using C for this.
-

Object oriented programming with ANSI C

-

Published at 2016-11-20T22:10:57+00:00; Updated at 2022-01-29

+

Object oriented programming with ANSI C


+
+Published at 2016-11-20T22:10:57+00:00; Updated at 2022-01-29
+
   ___   ___  ____        ____ 
  / _ \ / _ \|  _ \      / ___|
@@ -5510,75 +5512,113 @@ Total time: 1213.00s
 | |_| | |_| |  __/_____| |___ 
  \___/ \___/|_|         \____|
                               
-

-

You can do a little of object-oriented programming in the C Programming Language. However, that is, in my humble opinion, limited. It's easier to use a different programming language than C for OOP. But still it's an interesting exercise to try using C for this.

-

Function pointers

-

Let's have a look at the following sample program. All you have to do is to add a function pointer such as "calculate" to the definition of struct "something_s". Later, during the struct initialization, assign a function address to that function pointer:

-
-#include <stdio.h>
+
+
+You can do a little of object-oriented programming in the C Programming Language. However, that is, in my humble opinion, limited. It's easier to use a different programming language than C for OOP. But still it's an interesting exercise to try using C for this.
+
+

Function pointers


+
+Let's have a look at the following sample program. All you have to do is to add a function pointer such as "calculate" to the definition of struct "something_s". Later, during the struct initialization, assign a function address to that function pointer:
+
+ +
#include <stdio.h>
 
-typedef struct {
-    double (*calculate)(const double, const double);
-    char *name;
-} something_s;
+typedef struct {
+    double (*calculate)(const double, const double);
+    char *name;
+} something_s;
 
-double multiplication(const double a, const double b) {
-    return a * b;
-}
+double multiplication(const double a, const double b) {
+    return a * b;
+}
 
-double division(const double a, const double b) {
-    return a / b;
-}
+double division(const double a, const double b) {
+    return a / b;
+}
 
-int main(void) {
-    something_s mult = (something_s) {
-        .calculate = multiplication,
-        .name = "Multiplication"
-    };
+int main(void) {
+    something_s mult = (something_s) {
+        .calculate = multiplication,
+        .name = "Multiplication"
+    };
 
-    something_s div = (something_s) {
-        .calculate = division,
-        .name = "Division"
-    };
+    something_s div = (something_s) {
+        .calculate = division,
+        .name = "Division"
+    };
 
-    const double a = 3, b = 2;
+    const double a = 3, b = 2;
 
-    printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b));
-    printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b));
-}
-

-

As you can see, you can call the function (pointed by the function pointer) with the same syntax as in C++ or Java:

-
-printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b));
-printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b));
-

-

However, that's just syntactic sugar for:

-
-printf("%s(%f, %f) => %f\n", mult.name, a, b, (*mult.calculate)(a,b));
-printf("%s(%f, %f) => %f\n", div.name, a, b, (*div.calculate)(a,b));
-

-

Output:

-
-pbuetow ~/git/blog/source [38268]% gcc oop-c-example.c -o oop-c-example
-pbuetow ~/git/blog/source [38269]% ./oop-c-example
-Multiplication(3.000000, 2.000000) => 6.000000
-Division(3.000000, 2.000000) => 1.500000
-

-

Not complicated at all, but nice to know and helps to make the code easier to read!

-

That's not OOP, though

-

However, that's not really how it works in object-oriented languages such as Java and C++. The method call in this example is not a method call as "mult" and "div" in this example are not "message receivers". I mean that the functions can not access the state of the "mult" and "div" struct objects. In C, you would need to do something like this instead if you wanted to access the state of "mult" from within the calculate function, you would have to pass it as an argument:

-
-mult.calculate(mult,a,b));
-

-

Real object oriented programming with C

-

If you want to take it further, hit "Object-Oriented Programming with ANSI-C" into your favourite internet search engine or follow the link below. It goes as far as writing a C preprocessor in AWK, which takes some object-oriented pseudo-C and transforms it to plain C so that the C compiler can compile it to machine code. This is similar to how the C++ language had its origins.

-https://www.cs.rit.edu/~ats/books/ooc.pdf
-

OOP design patterns in the Linux Kernel

-

Big C software projects, like Linux, also follow some OOP techniques:

-https://lwn.net/Articles/444910/
-

C is a very old programming language with it's quirks. This might be one of the reasons why Linux will also let Rust code in.

-

E-Mail your comments to hi@paul.cyou :-)

-Back to the main site
+ printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b)); + printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b)); +} + +
+As you can see, you can call the function (pointed by the function pointer) with the same syntax as in C++ or Java:
+
+ +
printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b));
+printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b));
+
+
+However, that's just syntactic sugar for:
+
+ +
printf("%s(%f, %f) => %f\n", mult.name, a, b, (*mult.calculate)(a,b));
+printf("%s(%f, %f) => %f\n", div.name, a, b, (*div.calculate)(a,b));
+
+
+Output:
+
+ +
pbuetow ~/git/blog/source [38268]% gcc oop-c-example.c -o oop-c-example
+pbuetow ~/git/blog/source [38269]% ./oop-c-example
+Multiplication(3.000000, 2.000000) => 6.000000
+Division(3.000000, 2.000000) => 1.500000
+
+
+Not complicated at all, but nice to know and helps to make the code easier to read!
+
+

That's not OOP, though


+
+However, that's not really how it works in object-oriented languages such as Java and C++. The method call in this example is not a method call as "mult" and "div" in this example are not "message receivers". I mean that the functions can not access the state of the "mult" and "div" struct objects. In C, you would need to do something like this instead if you wanted to access the state of "mult" from within the calculate function, you would have to pass it as an argument:
+
+ +
mult.calculate(mult,a,b));
+
+
+

Real object oriented programming with C


+
+If you want to take it further, hit "Object-Oriented Programming with ANSI-C" into your favourite internet search engine or follow the link below. It goes as far as writing a C preprocessor in AWK, which takes some object-oriented pseudo-C and transforms it to plain C so that the C compiler can compile it to machine code. This is similar to how the C++ language had its origins.
+
+https://www.cs.rit.edu/~ats/books/ooc.pdf
+
+

OOP design patterns in the Linux Kernel


+
+Big C software projects, like Linux, also follow some OOP techniques:
+
+https://lwn.net/Articles/444910/
+
+C is a very old programming language with it's quirks. This might be one of the reasons why Linux will also let Rust code in.
+
+E-Mail your comments to hi@paul.cyou :-)
+
+Back to the main site
-- cgit v1.2.3