Advanced C #20: Error Handling

  Рет қаралды 3,251

Charles Cabergs

Charles Cabergs

5 ай бұрын

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

Пікірлер: 44
@darrenfernandes4189
@darrenfernandes4189 5 ай бұрын
great video but I thought it would be c# XD
@grimvian
@grimvian 3 ай бұрын
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.
@cacharle
@cacharle 3 ай бұрын
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)
@grimvian
@grimvian 3 ай бұрын
@@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)
@SeanCodingThings
@SeanCodingThings Ай бұрын
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.
@cacharle
@cacharle 27 күн бұрын
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.
@SeanCodingThings
@SeanCodingThings 25 күн бұрын
@@cacharle 👍
@starc0w
@starc0w 5 ай бұрын
Super useful! Thank you so much! Really great stuff 🙂
@cacharle
@cacharle 5 ай бұрын
I'm glad you're enjoying this series :D
@MegaPacoquinha
@MegaPacoquinha 21 күн бұрын
Do you recommend any advanced books on c++ or c, on fat pointers, dynamic dispatching and other complicated stuff?
@cacharle
@cacharle 21 күн бұрын
I very rarely read books about programming. I read The Practice of Programming by Rob Pike but it's not really advanced or focused on C if I remember correctly. I have rewritten malloc as a side project and I would like to make a video about it some day (malloc uses fat pointers) and I have touched on dynamic dispatch in my video about "lookup tables" where I show my sed project: kzbin.info/www/bejne/l3a8npRogreGfrM
@youdontknowme2508
@youdontknowme2508 5 ай бұрын
Great video as always
@cacharle
@cacharle 5 ай бұрын
Thanks! :D
@spacewad8745
@spacewad8745 5 ай бұрын
fine video but man the gruvbox colorscheme is so timeless. looking forward to your work!
@cacharle
@cacharle 5 ай бұрын
Thanks, I like the comfy vibe of the gruvbox theme, makes me feel at home 😅
@davidreilly6578
@davidreilly6578 Ай бұрын
Great video man keep up the good work 😊
@cacharle
@cacharle Ай бұрын
Thank you :D
@goncaloazevedo9822
@goncaloazevedo9822 5 ай бұрын
Would love a video on varargs!
@cacharle
@cacharle 5 ай бұрын
Already made one: kzbin.info/www/bejne/i2nWpIimoteKsKs And for macro variable argument aswell: kzbin.info/www/bejne/hZSxfWNthraCjKs
@goncaloazevedo9822
@goncaloazevedo9822 5 ай бұрын
@@cacharle Amazing brother 🙌
@SeanCodingThings
@SeanCodingThings Ай бұрын
Nice!
@oschonrock
@oschonrock 5 ай бұрын
very good.... maybe also show the "goto" pattern for free'ing resourcs in case of error.
@cacharle
@cacharle 5 ай бұрын
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
@oschonrock
@oschonrock 5 ай бұрын
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?
@cacharle
@cacharle 5 ай бұрын
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.
@oschonrock
@oschonrock 5 ай бұрын
Yeah that's fair. I think it's still worth a video to explain that.
@cacharle
@cacharle 5 ай бұрын
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/ :'(
@rednibcoding3412
@rednibcoding3412 3 ай бұрын
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!
@cacharle
@cacharle 3 ай бұрын
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)
@grimvian
@grimvian 3 ай бұрын
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.
@cacharle
@cacharle 3 ай бұрын
@@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.
@grimvian
@grimvian 3 ай бұрын
@@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...
@PLAYGAME-wj9bw
@PLAYGAME-wj9bw 5 ай бұрын
Super vidéo mais ça aurait été bien de mentionner goto 👍
@cacharle
@cacharle 5 ай бұрын
Je sais, quelqu'un me l'a dit dans un autre commentaire mais je n'utilise pas cette methode personnelement
@leonardopisano9575
@leonardopisano9575 4 ай бұрын
En français, S.V.P. 😂
@cacharle
@cacharle 3 ай бұрын
jamais! 😄
@basilenordmann7356
@basilenordmann7356 Ай бұрын
Hum… réécrire des fonctions, un accent français… 42?
@cacharle
@cacharle Ай бұрын
Oui, j'ai fait l'ecole 19 a Bruxelles 😅
@basilenordmann7356
@basilenordmann7356 Ай бұрын
@@cacharle haha démasqué! 😜 C’était bien à Bruxelles ? C’est recommandable ?
@cacharle
@cacharle Ай бұрын
@@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
Advanced C #21: Read a file line by line with getline
8:45
Charles Cabergs
Рет қаралды 872
Vim Tips I Wish I Knew Earlier
23:00
Sebastian Daschner
Рет қаралды 38 М.
🍕Пиццерия FNAF в реальной жизни #shorts
00:41
ПООСТЕРЕГИСЬ🙊🙊🙊
00:39
Chapitosiki
Рет қаралды 68 МЛН
2000000❤️⚽️#shorts #thankyou
00:20
あしざるFC
Рет қаралды 15 МЛН
1❤️#thankyou #shorts
00:21
あみか部
Рет қаралды 83 МЛН
The Importance of Error Handling in C
8:18
Nir Lichtman
Рет қаралды 29 М.
Is C++ better than C?
1:46:10
Tsoding Daily
Рет қаралды 38 М.
Best Programming Languages Tier List
33:02
Serif Sundown
Рет қаралды 10 М.
Handling Errors in C/Unix (perror, strerror, errno)
6:36
Jacob Sorber
Рет қаралды 37 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 280 М.
Advanced C: The UB and optimizations that trick good programmers.
1:12:34
Eskil Steenberg
Рет қаралды 157 М.
why do header files even exist?
10:53
Low Level Learning
Рет қаралды 361 М.
ARRAYLIST VS LINKEDLIST
21:20
Core Dumped
Рет қаралды 50 М.
tree-sitter explained
15:00
TJ DeVries
Рет қаралды 71 М.
wireless switch without wires part 6
0:49
DailyTech
Рет қаралды 3,9 МЛН
WWDC 2024 Recap: Is Apple Intelligence Legit?
18:23
Marques Brownlee
Рет қаралды 5 МЛН
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 1,6 МЛН