Advanced C #20: error handling

2023 ж. 21 Жел.
2 599 Рет қаралды

How to do error handling in C and get specific error in a human readable form.
Social links:
Website: cacharle.xyz
Github: github.com/cacharle
Linkedin: / charles-cabergs-328aa8214

Пікірлер
  • At last, finally someone makes great advanced C videos, fantastic! And big thanks for not having disturbing background music and irrelevant graphics editing. Your pedagogical skills are also great, because you have the editor and the console side by side and the pace is fine and not having irrelevant info included on the screen. What about making a video or more about sending outputs to a default printer? Of course you got a new subscriber.

    @grimvian@grimvian2 ай бұрын
    • Thank you for the kind words :) Having as little distractions as possible is my trade mark and it will keep being that way. Unfortunately I have never written code to interact with a printer and I don't have a printer at home so I can't help you with that (I wouldn't be surprised if you can find a clean cli tool or a library to interact with printers tho)

      @cacharle@cacharle2 ай бұрын
    • @@cacharle I have now discovered a library cops.h and understand, that is widely used in Linux. I can use Raylib.h and print ttf characters and lines on a virtual graphical page and then via cups make a hardcopy to the printer. And this little code can also print although I'm new to Linux and do not yet understand the command. #include #include #include int main() { char text[] = "Hello, Printer! "; char command[256]; sprintf(command, "echo \"%s\" | lp -d $(lpstat -d | cut -d' ' -f4)", text); if (system(command) == -1) { perror("Failed to print"); return 1; } printf("Text printed successfully! "); return 0; } I admit it's not my code. :o)

      @grimvian@grimvian2 ай бұрын
  • great video but I thought it would be c# XD

    @darrenfernandes4189@darrenfernandes41895 ай бұрын
  • Super useful! Thank you so much! Really great stuff 🙂

    @starc0w@starc0w4 ай бұрын
    • I'm glad you're enjoying this series :D

      @cacharle@cacharle4 ай бұрын
  • A good next video on Advanced C might be how to take user input from a user safely in a loop in a "clean" way. It's harder then it might sound.

    @SeanCodingThings@SeanCodingThings7 күн бұрын
    • Indeed, I could make a video on `getline`, a very useful function. Or one where I detail the *get* functions and warn about buffer overflows with `gets`. Thank you for your suggestion.

      @cacharle@cacharle3 күн бұрын
    • @@cacharle 👍

      @SeanCodingThings@SeanCodingThingsКүн бұрын
  • Great video man keep up the good work 😊

    @davidreilly6578@davidreilly657818 күн бұрын
    • Thank you :D

      @cacharle@cacharle17 күн бұрын
  • Great video as always

    @youdontknowme2508@youdontknowme25085 ай бұрын
    • Thanks! :D

      @cacharle@cacharle5 ай бұрын
  • fine video but man the gruvbox colorscheme is so timeless. looking forward to your work!

    @spacewad8745@spacewad87455 ай бұрын
    • Thanks, I like the comfy vibe of the gruvbox theme, makes me feel at home 😅

      @cacharle@cacharle5 ай бұрын
  • Nice!

    @SeanCodingThings@SeanCodingThings10 күн бұрын
  • Great series, thank you! I am a software engineer for about two decades, but always used managed languages like C#, Java etc. Now i have to learn c and delve deeper into low level programming. I know the basics but i am so used to having dynamic arrays, lists, maps, even a proper string data type and all that stuff. Sometimes i have a hard time with this. When i look on YT about C tutorials, they all start at zero for total beginners. What i need is a complete tutorial for programmers switching to C. What are the best practices when you need dynamic arrays or lists or maps. Will one write them themselves? If yes, could you show it to us? What are the best practices about memory management? What allocators are suited best for what needs, how do they work, and how do you implement them? Could you make such series? That would be really awesome!

    @rednibcoding3412@rednibcoding34122 ай бұрын
    • Hi, I just made a video about writing your own dynamic array in C (and I also played around making it a "generic" data structure on stream). I am planning on doing more videos like this in the future. I think the main advice I have for someone that comes from a higher level programming language is try to just use the basic C features *as much as possible*. You will be very surprised by how many times it is possible to replace a dynamic array by a fixed size array, a map with an enum and a "lookup table", a string by a char array. Do not try to shoehorn these high level constructs in C. If you really need them, you can either rewrite them yourself or there is probably libraries that do that for you aswell (altho I've never felt the need to use one)

      @cacharle@cacharle2 ай бұрын
    • I'm a hobby programmer and I am at the finale of an little application for my wife's business, a relations database in C, Raylib graphics for user interface and Linux Mint. I'm using CodeBlocks and the debugger is very good to tell you where your logic fails. Because I really want learn C, I decided to build my my own string handling for editing. It's a enormous satisfaction, when your own library does the handling correct and gives lots cries, when it's not.

      @grimvian@grimvian2 ай бұрын
    • @@grimvian I also started writing C using CodeBlocks :D it is pretty good to learn but it started to get in my way when I wanted to do more advanced stuff from what I remember (ofc don't change editors if you're comfortable with it) The C standard library already has a lot of string handling functions which are very useful. Depending on your usecase, it might be necessary to implement a more dynamic string data structure like C++ or Python (which is basically just a dynamic array of characters). If I was you, I would really try to keep it simple at first and only use the standard library and regular character arrays.

      @cacharle@cacharle2 ай бұрын
    • @@cacharleI' very bad at typing and reading, so I'll have to stick with CodeBlocks and I'm a hobby coder most of the time. I do not think for a second I'll do better string handling than the standard library, it's just because my own library keeps me on my toes. I also used it for the editing in my database for my wife's business, although it was quite a task have the data on the disk and string handling and the graphics in place, but it seems to work fine now or else I'll know very quickly, because of my wife :o). Even insert and overwrite and a timed cursor, that correspond to ins/overwrite are in place. So it's great that someone like you is making advanced C videos and I think many C learners have tried many of the beginner videos and want to go on. I saw that a advanced C course costed about 3000 euros in Denmark, where I live...

      @grimvian@grimvian2 ай бұрын
  • Would love a video on varargs!

    @goncaloazevedo9822@goncaloazevedo98225 ай бұрын
    • Already made one: kzhead.info/sun/iJyso7mopKSNrYE/bejne.html And for macro variable argument aswell: kzhead.info/sun/gseHfJRviIOFiYE/bejne.html

      @cacharle@cacharle5 ай бұрын
    • @@cacharle Amazing brother 🙌

      @goncaloazevedo9822@goncaloazevedo98225 ай бұрын
  • very good.... maybe also show the "goto" pattern for free'ing resourcs in case of error.

    @oschonrock@oschonrock5 ай бұрын
    • Ye, I was planning on doing that but I've never used it myself, I just know it exists and how it works.. so I feel less qualified to talk about it

      @cacharle@cacharle5 ай бұрын
    • Well thx whole resource free'inf in the context of errors is quite tricky to get tight IMO. Multiple techniques exist just show the one you use?

      @oschonrock@oschonrock5 ай бұрын
    • I use the dumb and easy way: r1 = f1(); if (r1 == NULL) return NULL; r2 = f2(); if (r2 == NULL) { free(r1); return NULL; } r3 = f3(); if (r3 == NULL) { free(r1); free(r2); return NULL; } I can see why someone would want to remove the duplicate free's, but tbh, I very rarely encounter a case where I have more than 2/3 resources in one function so this simple technique is fine for my needs.

      @cacharle@cacharle5 ай бұрын
    • Yeah that's fair. I think it's still worth a video to explain that.

      @oschonrock@oschonrock5 ай бұрын
    • Indeed, I'll think about it.. Any other resource management technique I should know about other than the dumb one and the goto one? I think I saw some people make a `defer` macro but I'm not sure if it was in C++ or C.. EDIT: it was in C++ www.gingerbill.org/article/2015/08/19/defer-in-cpp/ :'(

      @cacharle@cacharle5 ай бұрын
  • Super vidéo mais ça aurait été bien de mentionner goto 👍

    @PLAYGAME-wj9bw@PLAYGAME-wj9bw5 ай бұрын
    • Je sais, quelqu'un me l'a dit dans un autre commentaire mais je n'utilise pas cette methode personnelement

      @cacharle@cacharle5 ай бұрын
  • En français, S.V.P. 😂

    @leonardopisano9575@leonardopisano95753 ай бұрын
    • jamais! 😄

      @cacharle@cacharle3 ай бұрын
  • Hum… réécrire des fonctions, un accent français… 42?

    @basilenordmann7356@basilenordmann735613 күн бұрын
    • Oui, j'ai fait l'ecole 19 a Bruxelles 😅

      @cacharle@cacharle13 күн бұрын
    • @@cacharle haha démasqué! 😜 C’était bien à Bruxelles ? C’est recommandable ?

      @basilenordmann7356@basilenordmann735613 күн бұрын
    • @@basilenordmann7356 Oui c'etait tres bien. Apres je suis jamais aller visiter d'autre ecole dans le Network 42 donc je peux pas vraiment comparer

      @cacharle@cacharle13 күн бұрын
KZhead