I just wrote a functional program in C. It is not doing anything too fancy. But I'm so happy about it because of what it means. For the longest time I didn't know pretty much nothing except of Python. I can use Python for a lot of very interesting stuff. But I want to be able to look at code written in different languages and know what I'm looking at. I want to be able to make changes to the UPBGE game engine for my game [Dani's Race](/games/Dani's_Race) and stuff.

I know, I know, UPBGE is a fork of Blender and therefor is written in C++, not C. What am I wasting my time, learning C, for? Well...

If C++ is a fork of C, with extensions on top of what C can do. I should understand the difference between the two, I suppose. And therefor I should, first of all, have some idea of what it is like to program in C.

Here is a surprising thing about this whole endevour. I started looking into C today. And already while writing my first program `journal`, which I will be talking more in depth about in this article, I already felt like I know the language enough to solve little annoyances and overall code rather quickly in it. It felt almost like writing python code.

C has modules, kind of like python. In python you import modules using `import`. Say you want to have access to the operating system functions. You can write something like this:

```
import os

```

And it will give you the whole `os` modules to do things with in the operating system.

In C you `#include` them instead.

```
#include <stdio.h>

```

This `stdio.h` file is a C code file ( as far as I understand ) that exists somewhere relative to the compiler. And the code of that file is injected into your code. So it is a bit different in practice.

In python to call a function from `os` I would be formatting it like this `os.getcwd()` or something. In C all the functions in the files you include are dumped straight into the same context as everything else. 

For example `stdio.h` has a function called `printf()` useful to print stuff into the terminal. It is not `stdio.printf()` or anything stupid like that. You are just simply using `printf()` directly.

I imagine what a pain in the ass this must be for large projects. You have to keep track of so many names. And not, by mistake, define a function with a name that already exist somewhere, in some library you want to use for some task.

Another strange thing about C is that you must have a `main()` function where the main execution thing of your code is done.

In python you just type away. A "hello world" program in python is very simple:

```
print("hello world")
```

In C `print` does not exist and you have to define a `main()` function. So in C a "hello world" program is a bit more complicated:

```
#include <stdio.h>
int main() {
    printf("hello world\n");
}

```

Funny, I wrote this example rather fast for this article. I'm still rather shocked how quickly I'm getting to the point where I can use C for things. And I did double-check myself. This code compiles and outputs an identical output to the python code above.

Now you can spot a few differences of approach. First the inclusion of the `\n` thingie in the C version. `printf` unlike python's `print` doesn't end every line with a new-line character. You have to specifically tell it to do so. Then there is the `;` after each operation. This is because C is indentation agnostic. In python the white-space stuff, like indentation and new line characters, are used to separate stuff, not just visually, but functionally. In C you have visible marks that separate code. Like the `;` sign after every operation. I needed time to get used to this.

Variables are simple but not simple. In python you have so many types of data. You have `int` for integers and `float` and floating point numbers and `str` for text and `list` and `dict` and `tuple` and other various interesting, useful things. In C you have almost none of it.

You've got your `int` and `float` and stuff. And you've got stuff like `char` which represents a single character of text.

Now you can do something like this:

```
char text[256];

```

which will be similar to doing something like:

```
text = ""
```

in python, But notice a few things. First you have to specify that it is a `char` type. Then you can do many `char` at once, kind of like a `list` in python, to store some characters-long strings of text. But you have to specify how many of those characters long, this object is. I think in the real world it literally allocates 256 bytes of RAM ( in this example ) for my text string. And running out of this space might cause all sorts of issues.

Also comparing strings isn't simple either.

When I was playing around with the language I wanted to make a simple if statement. I input some text and if the text I input is one thing, I do one thing, and if it is something else I do another thing.

I tried doing this:

```
if ( text1 == text2 ){ dosomething(); }
```

But even though it never errored out, it never worked either.

Apparently I must do this instead:

```
#include <string.h>
if ( strcmp(text1, text2) == 0 ){ dosomething(); }
```

The `string.h` file has the `strcmp()` function I can use to compare texts. And if the texts are the same, it will return `0`. So I need to compare the output of this function to `0`. It is a bit annoying.

In any-case, I decided to write something useful. Writing something useful is always a good stepping stone into learning a language.

Just a few days ago I wrote [a simple GTK program in Python to keep track of my weight](https://codeberg.org/blenderdumbass/WeightedData). It basically records data I drop at it into a JSON and then shows it to me in a way that makes sense to the task.

So I thought, okay... how hard would it be to read and write a JSON in C? Ah... I found [this](https://github.com/json-c/json-c). And the size of this whole thing made me feel wrong. And then I found [another something to do with C and JSON](https://zserge.com/jsmn/) and the way they described doing it made me feel un-easy.

See, because C doesn't have a `dict` like python, making a JSON like structure of data inside of a C program becomes a pain in a butt.

I decided, fuck it. What is the simplest shit I can do? Well maybe something like a journal writing program. Every entry is a line in a .txt file. All you need to do to present it, is just to `printf` the whole file. And it is probably doable.

I present to you `journal.c`

```
#include <stdio.h>
#include <string.h>



int help() {

  printf("\n\n");
  printf("          HELP DIALOGUE of JOURNAL\n");
  printf("\n");
  printf("    help - Print this helps message.\n");
  printf("    save - Saves a text into the journal.\n");
  printf("    show - Prints out the entire journal.\n");
  printf("\n\n");
    

}

int save(char string[]) {

  FILE *data;
  data = fopen("journal.txt", "a");
  fprintf(data, "\n%s", string);
  fclose(data);


}
int show() {

  FILE *data;
  data = fopen("journal.txt", "r");
  char string[1024];

  printf("\n\n");
  
  while( fgets(string, 1024, data) ){
    printf("%s", string);
  }
  
  printf("\n\n");

}

int main(int argc, char *argv[]) {

  if (argc == 1 || strcmp(argv[1], "help") == 0) {
    help();
  }
  else if (argc == 3 && strcmp(argv[1], "save") == 0) {
    save(argv[2]);
  }
  else if (argc > 1 && strcmp(argv[1], "show") == 0) {
    show();
  }


}
```

You can save it as `journal.c` and run this command:

```
gcc journal.c -o journal
```

to compile it into an executable.

And then

```
./journal help
```

to see the help of this program.

Yes this program does system arguments. And you know what. Those are simple.

```
int main(int argc, char *argv[]) {
```

This line defines the `main()` function, but look what it also does, it gives it arguments. What? System arguments are built-in in C?

The `argc` variable contains the amount of arguments and the `argv` is a list of lists of characters. A list of text strings. A list of the arguments themselves.

So from that point it becomes kind of simple. If there are no arguments or the second argument is `help` ( because the first one is the name of the program `./journal` ) we run the `help()` function. Or in code:

```
if (argc == 1 || strcmp(argv[1], "help") == 0) {
    help();
  }

```

And if you look into the `help()` function. It is just a bunch of `printf()`.

Then if the argument is `show` we open the file and dump its content into the terminal. Now that is not quite as simple as you think.

The fun part was to realize that `fopen()` is like either built-in or a part of the other stuff I already imported. So opening and saving files is actually not that big of a problem in C.

But the code to dump a file into a terminal is strange:

```
  FILE *data;
  data = fopen("journal.txt", "r");
  char string[1024];

  printf("\n\n");
  
  while( fgets(string, 1024, data) ){
    printf("%s", string);
  }
  
  printf("\n\n");
```

There is a `FILE` datatype apparently. Python has something similar. Not unlike python's `read()` C has `fgets()`. But because the string you are saving to has a certain pre-specified buffer size ( in the case of this code 1024 bytes ), you are reading the file one chunk at a time. And as I have noticed, one line at a time too.

So while in python it would look like this:

```
print("\n\n")

data = open("journal.txt", "r")
print(data.read())

print("\n\n")
```

in C we need to do a `while` loop and read the data until there isn't any. Which is a bit weird.

Also you can't `printf()` a string directly. You need to append it like that `printf("%s", string)`. What?

On top of that it seems like C doesn't have much error handling at all. I can open a nonexistent file. And it will just have zero bytes in it. Instead in python it will error out.

So yeah. It was fun. I know enough C to write pointless bullshit in it now. And to, hopefully, understand more complex code written in it.

My next challenge. C++. Let's do it. Let's go!

**Happy Hacking!!!**