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.
↩ Reply
import. Say you want to have access to the operating system functions. You can write something like this:
↩ Reply
import os
os modules to do things with in the operating system.
↩ Reply
#include them instead.
↩ Reply
#include <stdio.h>
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.
↩ Reply
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.
↩ Reply
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.
↩ Reply
main() function where the main execution thing of your code is done.
↩ Reply
print("hello world")
print does not exist and you have to define a main() function. So in C a "hello world" program is a bit more complicated:
c:0 ↩ Reply
#include <stdio.h> int main() { printf("hello world\n"); }
\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.
↩ Reply
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.
↩ Reply
int and float and stuff. And you've got stuff like char which represents a single character of text.
↩ Reply
char text[256];
text = ""
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.
↩ Reply
if ( text1 == text2 ){ dosomething(); }
#include <string.h> if ( strcmp(text1, text2) == 0 ){ dosomething(); }c:1
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.
↩ Reply
dict like python, making a JSON like structure of data inside of a C program becomes a pain in a butt.
↩ Reply
printf the whole file. And it is probably doable.
↩ Reply
journal.c
↩ Reply
#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(); } }
journal.c and run this command:
↩ Reply
gcc journal.c -o journal
./journal help
int main(int argc, char *argv[]) {
main() function, but look what it also does, it gives it arguments. What? System arguments are built-in in C?
↩ Reply
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.
↩ Reply
help ( because the first one is the name of the program ./journal ) we run the help() function. Or in code:
↩ Reply
if (argc == 1 || strcmp(argv[1], "help") == 0) { help(); }
help() function. It is just a bunch of printf().
↩ Reply
show we open the file and dump its content into the terminal. Now that is not quite as simple as you think.
↩ Reply
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.
↩ Reply
FILE *data; data = fopen("journal.txt", "r"); char string[1024]; printf("\n\n"); while( fgets(string, 1024, data) ){ printf("%s", string); } printf("\n\n");
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.
↩ Reply
print("\n\n") data = open("journal.txt", "r") print(data.read()) print("\n\n")
while loop and read the data until there isn't any. Which is a bit weird.
↩ Reply
printf() a string directly. You need to append it like that printf("%s", string). What?
↩ Reply
Troler
c:0 August 04, 2026
Troler
c:1 August 04, 2026if ( !strcmp(text1, text2) ){ dosomething(); }
Troler
c:4 August 04, 2026main() is an int function, it requires to return an whole number. Here, the number represents the return code.
Troler
c:4 August 04, 2026main() is an int function, it requires to return an whole number. Here, the number represents the return code.
Troler c:4 August 04, 2026main() is an int function, it requires to return an whole number. Here, the number represents the return code.git for this game. Instead, most changes will be recorded via the notgit.py program I wrote for fun the other day.