Weird Parts of C you probably never heard of...

  Рет қаралды 54,142

Tsoding Daily

Tsoding Daily

10 ай бұрын

Previous Episodes: • Music Visualizer
References:
- Music by / @nu11_ft
- X macro: en.wikipedia.org/wiki/X_macro

Пікірлер: 120
@julkiewicz
@julkiewicz 9 ай бұрын
In my compiler class at the uni we were supposed to implement a simple cut-down Java-like curly braces compiler. We were allowed to extend it with extra features to pass the class with a higher grade. Somehow I got it into my head that I'll extend the language with C-style pointer syntax, including function pointers etc. I ended up spending 90% of my time trying to get the front-end to work. It's just about barely implementable with the classic parser-generator tools like antlr etc. You literally can be half way through parsing a line in C and still be unsure if: A) it's going to be a function declaration, B) pointer type declaration, C) function pointer declaration, D) variable declaration or something else. It's insane how a single asterisk can just completely change the meaning of a line. By the time I got the front-end to finally work I had time to only implement some basic backend that produced unoptimized bytecode and ran it in a simple bytecode machine. The whole thing was like this horse drawing meme
@dokchampa9324
@dokchampa9324 9 ай бұрын
You can't just say all that and then not share your grade with us
@julkiewicz
@julkiewicz 9 ай бұрын
@@dokchampa9324 Well I got the max grade possible but I submitted it after the deadline, so it was an automatic -1 deduction. Sad face :) I guess a way of my uni trying to teach me that "done is better than perfect (?)"
@mastershooter64
@mastershooter64 8 ай бұрын
​@@julkiewicz Share the compiler source code!
@marcs9451
@marcs9451 7 ай бұрын
I much rather hand write my parser than use ANTLR or Bison
@KeplerEmeritus
@KeplerEmeritus 6 ай бұрын
Compiler class…? Yeah I should have gone to school. Compilers are the types of things I dream of building. Instead, I make shitty web api which isn’t really programming at all…
@cramble
@cramble 9 ай бұрын
TSoding being amazing as always.
@poijmc606
@poijmc606 9 ай бұрын
Tsoding casually writing C language black magic curses
@Vank4o
@Vank4o 9 ай бұрын
Even after 50 years C is just amazing!
@DVRC
@DVRC 9 ай бұрын
Speaking of compile time generated code, I've stumbled upon the source code of a weird preprocessor for C called Ace, which was written by James Gosling for the X/NeWS merge. There is a paper where he explains how it works (basically it operates on the syntax tree instead of doing näive macro substitutions), and how he could generate routines for different framebuffer depths/models using the same code.
@salman8562
@salman8562 9 ай бұрын
Can't find the paper, whats the title?
@DVRC
@DVRC 9 ай бұрын
@@salman8562 "Ace: a syntax driven C preprocessor", James Gosling, 1989. Likely YT removed the comment where I linked it
@julians.2597
@julians.2597 2 ай бұрын
Pretty sure Zig kinda does that. Having a preprocessor act on source code is just a recipe for desaster
@yaksher
@yaksher 9 ай бұрын
I think in the header file, what you could do is add another layer. Like, have, #define LIST_OF_PLUGS \ BASE_PLUG(plug_hello, void) \ BASE_PLUG(plug_init, Plug *plug, const char *file_path) \ BASE_PLUG(plug_pre_reload, Plug *plug) \ BASE_PLUG(plug_post_reload, Plug *plug) \ BASE_PLUG(plug_update, Plug *plug) \ which includes the type signature information. Then, you would do // this generates the type defs #define BASE_PLUG(name, args...) typedef void (*name##_t)(args); LIST_OF_PLUGS #undef BASE_PLUG // and then, once you're done using the form which has the type signature, you can just do this to discard it and use the plug name #define BASE_PLUG(name, ...) PLUG(name) And better yet, if you ever need the type signature information again, you just redefine BASE_PLUG and use that, instead of PLUG.
@ChristopherDrum
@ChristopherDrum 9 ай бұрын
I came into comments to note this technique, so I'll just append to yours. The core idea is that variadic arguments are valid in X macros. We can take this even a little further by including function return types in the initial #define. So each element of LIST_OF_PLUGS is defined as BASE_PLUG(, , , , ...) You can then do #define BASE_PLUG(name, return_type, ...)\ typedef return_type (* name##_t)(__VA_ARGS__); LIST_OF_PLUGS #undef BASE_PLUG Then you don't even have to update BASE_PLUG; *everything* is contained within the LIST_OF_PLUGS. This has been working great for me.
@michalbotor
@michalbotor 9 ай бұрын
(4:26) for the end user it needs to be a double click and it works. ;) you tell them to compile from source and link they will have a heart attack.
@portersky
@portersky 9 ай бұрын
Didn't know the macro technique had a name, I used it when I was defining enums in my C++ program to generate a list of names as strings from the enum values.
@phantomwizard5892
@phantomwizard5892 9 ай бұрын
The amount of knowldege ive gain from you these past couple of months is just insane Thanks a lot sir!
@diegorocha2186
@diegorocha2186 9 ай бұрын
Pretty good!!! As usual! I've been following you for the last 3 years or more, and I keep learning from you in every session, thanks a lot Mr Zozin!
@alecs2056
@alecs2056 9 ай бұрын
Why not make PLUG with 2 parameters and store in the second one the arguments of the function then you can generate automatically also the type definitions from the list
@TsodingDaily
@TsodingDaily 9 ай бұрын
Oh, that's a cool idea! Thanks!
@Mikayex
@Mikayex 9 ай бұрын
Also you can make a macro #define COMMA , to allow using commas in the second argument
@alecs2056
@alecs2056 9 ай бұрын
@@Mikayex i think that using variadic argoments in the macro is cleaner
@SteveRowe
@SteveRowe 4 ай бұрын
I've been programming in C since 1983. I've never seen the typedef (fn)(void) as a way to do function declaration. Not sure I'd ever do that, as I try to avoid obscure things. But I loved learning about it, so thank you!
@pedrorondon2617
@pedrorondon2617 9 ай бұрын
The day I understood how Apache http server modules work I was amazed that programming in C could do that, I didn't understood how It was implemented but I knew that could be somthing with shared libraries but didn't know how it could be implemented in code, thanks for the video!
@WilderPoo
@WilderPoo 9 ай бұрын
Watching you keeps me motivated, thank you! 😁
@anon_y_mousse
@anon_y_mousse 9 ай бұрын
It's kind of funny that so many people don't know about all of these old tricks and it's cool that you're bringing them to the forefront again. Something you might consider is using an X-macro to define the namespace that you use for variables and functions. #define PLUG(n) plug_##n##_t and perhaps more arguments to handle defining functions signatures versus defining the type of a variable and assigning a value to it. It looks weird, but works to have things like: PLUG(init,void,(int ac,char**av)) { ... } or PLUG(freq,int,20);
@CoderDBF
@CoderDBF 9 ай бұрын
Great stuff, I saved this video to a playlist so that I can watch it again later. I just want to say, I wouldn't be opposed to seeing you do some short form content as well. You have some interesting stuff to share, but your videos take up a large part of a day to watch. I was thinking there might perhaps be a way to do TLDR; versions of your longer streams? Tsoding for those in a hurry, sort of speak. Anyway, keep up the good work.
@ruffianeo3418
@ruffianeo3418 9 ай бұрын
"If a code base gets bigger and bigger, eventually it will have a worse alternative of Common Lisp in its code." ~~~ Famous Klingon proverb Changing function implementations on the fly is another 0.5% of Common Lisp :)
@francklenormand2680
@francklenormand2680 9 ай бұрын
The plugin should export only 1 symbol: the table of functions implemented. There are other fun stuff to do with macro like logging utility or boomen logic at compile time like checking size of data and such
@NeZversSounds
@NeZversSounds 9 ай бұрын
Hands down my favorite video! Learned a lot from it.
@pascualsu
@pascualsu 9 ай бұрын
Top notch programming channel for sure
@octagonal8905
@octagonal8905 9 ай бұрын
Lol, love the SCP-087 thumbnail
@arthurgrillo3462
@arthurgrillo3462 9 ай бұрын
I learned about x-macros a week ago, now tsoding post a video about that. This is amazing but now I'm scared abou tsoding reading my mind
@screamyboy4722
@screamyboy4722 9 ай бұрын
This man brain can simulate the universe
@TheRailroad99
@TheRailroad99 9 ай бұрын
Wow, the "function variable" was very interesting! However to any dev: please only use such techniques when REALLY necessary and comment them well. Remember that in theory, you could build your own language with the preprocessor, its very powerful. But noone will understand your code anymore
@karmavil4034
@karmavil4034 9 ай бұрын
Wow I'm usually wordless at the end of your videos (and exhausted because they're veeeery long) but this one has been the most exiting video I've seen in a long long time on youtube. I've started today actually to "formally" learn C from manuals and really take a deep understanding because it is becoming an issue for me and I think not changing this will limit and devaluate any progress to become better with .cc This is very impressive and the same time the reason of my problems.. Not taking advantage of preprocessor statements is not just a matter of performance or readable code, it is necessary in some scenarios and you just game me a priceless lesson about that. And the example is so f amazing man congrats! you're a genius
@paklenizmaj
@paklenizmaj 9 ай бұрын
37:48 you can just compile both versions like as musialiser and musialiser-dev (or musialiser-hot) so user can just start what he want.
@MisererePart
@MisererePart 9 ай бұрын
I think you can take a look at inotify to implement auto compile after change, which would be cool. You can also add a pipe to your main to trigger reload from outside of the process, so the script which handle auto-compile would be able to reload completely the app without pressing r.
@mrmaniac9905
@mrmaniac9905 9 ай бұрын
I've been trying to catch one of your streams for half a year now. I always remember to check at the wrong times. Any chance you can post a schedule, even if it's only a day in advance of when you're going to stream?
@donkeyy8331
@donkeyy8331 9 ай бұрын
this is the type of thing you don't see or hear about in your day to day, thank you for the knowledge.
@FireDragon91245
@FireDragon91245 9 ай бұрын
man i watch 1 of your videos and instantly get the uncontrolable urge to code a shit ton of C
@einsjannis
@einsjannis 9 ай бұрын
Very cool thank you very much for sharing
@OM-yn8pt
@OM-yn8pt 9 ай бұрын
Would you consider investigating the comparison of sound files from the ground up, i.e. checking the similarity between two audio signals
@skope2055
@skope2055 9 ай бұрын
Useful tech, it saved me a lot of time. I'm glad you demonstrated it here. Edit: You could also put function signature with definition in a macro where you defined your plug functions to avoid some duplication, if it makes sense.
@Hardcore_Remixer
@Hardcore_Remixer 9 ай бұрын
#define def_func(func_name, // insert function here to use helper function , helper_function_name) \ new_function_here Something like this?
@rotbjorn
@rotbjorn 9 ай бұрын
For each of the PLUG in ``` #define LIST_OF_PLUGS \ PLUG(plug_int_changer) \ ``` There can be more "metadata" added with each PLUG macro: ``` #define LIST_OF_PLUGS \ PLUG(plug_int_changer, "1.0.0", int*) ``` Where for example "1.0.0" is the version and int* is the type signature of the parameters You can then consume them with ``` #define PLUG(name, _unused_version, type) \ typedef void (*name##_t)(type) LIST_OF_PLUGS #undef PLUG ```
@Hardcore_Remixer
@Hardcore_Remixer 9 ай бұрын
@@rotbjorn Wow. I didn't think about that. It reminds me of how C++ renames functions (so functions with the same name, but different paramerer types or number can be declared).
@JekasObps
@JekasObps 9 ай бұрын
pros are using -Wl,-rpath= linker option to specify search path for the .so in the executable itself and leave env vars untouchable
@JekasObps
@JekasObps 9 ай бұрын
great thing to typedef a function signature, neat stuff I wonder if I did noticed that behavior past life or maybe in a dream, so ridiculous... possibly that got rejected in a code review as missleading construct
@KafeinBE
@KafeinBE 9 ай бұрын
Don't get me wrong, it's clever and I have seen X macros used to good effect in a few C projects. But it's really one of these huge reasons to use C++ rather than C. Anything that lets you remove a macro and use a type-safe construct instead is an immense improvement for maintainability.
@emperorpalpatine6080
@emperorpalpatine6080 9 ай бұрын
That's an SCP. But I prefer the SC++P
@DarioSterzi
@DarioSterzi 8 ай бұрын
Damn this was surprisingly fun
@yjk_ch
@yjk_ch 9 ай бұрын
8:03 I saw something similar to X-macro in SerenityOS project, though sometimes it accepts X as macro argument instead of using whatever current definition of X is. It seemed like very clever idea, but I didn't know it had name!
@FredericoKlein
@FredericoKlein 9 ай бұрын
I admire your memory. If I did this to my code in like 1 week i would no longer remember how it worked.
@drdca8263
@drdca8263 9 ай бұрын
Wow! I hadn’t seen X macros, and that seems very nice! Edit: I am also quite impressed by how quickly you made the change to make whether hot-reloading is set up or whether everything linked as one file, be determined by compiler options. I know you must get this often, but wow you are much better at programming than I am. I mean, I feel generally competent at it (I can generally get the things I want to work, working), but, I’m always both using higher-than-C level languages (usually python) and taking longer to get anything written. I suppose that’s partially to be expected because I’ve put more of my skill points into math than I’ve put into programming (and intend to continue to do so), but I also think that there’s a major difference beyond that. Edit 2: I hope I’m not being weird with excessive praise.. I’m just impressed, is all.
@jaygatward7450
@jaygatward7450 9 ай бұрын
I’m glad you say that, I think everyone feels incompetent watching tsoding sessions😂
@OneWingedShark
@OneWingedShark 9 ай бұрын
The simplicity of C is a lie: it just tricks you into thinking it is simple. That is why making a standards-compliant C compiler is actually quite difficult, despite it's false-branding as "high level assembler", and why a verified C compiler is *much* harder than that.
@goawqebt6931
@goawqebt6931 9 ай бұрын
I assume the problem is some combination of: mixed levels of abstraction, confusing syntax and historical artifacts ?
@OneWingedShark
@OneWingedShark 9 ай бұрын
@@goawqebt6931 - Yes to all of that, David Chisnall's paper "C is not a low level language" describes where a lot of the mismatch is; look up the term "sequence point" before reading the paper and you'll see EXACTLY what he's referring to. / The other thing that makes it only seem simple is how features interact; consider "if (user = admin)" -- this error is possible because (a) assignment returns a value, (b) C doesn't have a boolean (and thus treats "not zero" as false), and (c) the combination of 'a' & 'b' means there's no way for this to be prohibited and still be a compliant C compiler because then you're rejecting valid C.
@MichaelPohoreski
@MichaelPohoreski 9 ай бұрын
@@OneWingedSharkOne could extend C so that := is used inside an if clause to tell the compiler that “yes, I REALY do meant assignment here” but sadly C has remained stagnant instead relying on compiler warning spam. :-/
@OneWingedShark
@OneWingedShark 9 ай бұрын
@@MichaelPohoreski - Honestly, you'd be better off just using Ada. The FSF GNAT Ada compiler is part of GCC, and thus has that whole suite of tools available, and the actual standard is available for free from the standards committee (the ARG) on their site.
@FelipeMarkson
@FelipeMarkson 9 ай бұрын
This feature to disable hot reloading was even more amazing of add hot reloading... God dammit C consistency
@luserdroog
@luserdroog 8 ай бұрын
Reiterating my SO post: make the convention explicit by passing in the inner macro by name. Then you give your inner handlers readable names , and don't have to undef anything.
@tbkih
@tbkih 29 күн бұрын
You could have used X-macros a little bit more around 18:30 if you defined PLUG to take two arguments (and ignored the last one in two of its instantiations). In LIST_OF_PLUGS the second argument would be the argument list of the signature. You could also solve all this by using Common Lisp, but you're brave to hotreload in C
@SingleTheShot
@SingleTheShot 9 ай бұрын
Hello Mr Tsoding, this might be a good idea a little while later if you have a more developed interface, but what if you could switch between different plugins?
@ksco3309
@ksco3309 9 ай бұрын
I read and wrote these macros a lot, now I finally know its name!
@alib5503
@alib5503 9 ай бұрын
As some say: dark corners of c
@business548
@business548 9 ай бұрын
What font you use for terminal?
@tomaspecl1082
@tomaspecl1082 9 ай бұрын
Cool stuff!
@mithrandirek1813
@mithrandirek1813 9 ай бұрын
Xmacros seem like a long stretch for the C preprocessor, good luck with debugging, I just use autogen to solve this issue in any text format (you can mangle the list elements in GNU Guile). I remember you had custom solution in C for generating code for linalg, why not use that?
@darioabbece3948
@darioabbece3948 9 ай бұрын
This is forbidden C
@bbq1423
@bbq1423 9 ай бұрын
You could probably use varargs to put the function parameters in the macro
@lolcat69
@lolcat69 9 ай бұрын
I love when TypeScript-oding does C coding :)
@anilgr1111
@anilgr1111 9 ай бұрын
Love from India ❣
@luserdroog
@luserdroog 8 ай бұрын
X-macros! Hurray!
@tcurdt
@tcurdt 5 ай бұрын
what are you using to create the window and draw?
@zoltankurti
@zoltankurti 9 ай бұрын
This is exactly how I deal with loading vulkan functions.
@zoltankurti
@zoltankurti 9 ай бұрын
Not exactly. My list of functions is in a different file and I just include that file after defining the macro and before undef.
@wrfsh
@wrfsh 4 ай бұрын
I've seen x macros a number of times in production, in super large C code bases (mil+ loc). They make finding a definition of a thing a real PITA. Every time i came across them, I would much rather prefer people to do a tiny bit of code duplication instead but keep things explicit and obvious.
@weakspirit_
@weakspirit_ 7 ай бұрын
macros in C! what we would we do without em
@rolandinnamorato1953
@rolandinnamorato1953 7 ай бұрын
Legcoded X Macros when?
@Asia_Bangladesh
@Asia_Bangladesh 9 ай бұрын
Why you use Emac not VIM? Make a compare video about Vim Emac And VS code
@doce3609
@doce3609 9 ай бұрын
wow this is very cool
@OsamaHasan000
@OsamaHasan000 9 ай бұрын
please can you make some beginner level project from scratch????????????????/
@alurma
@alurma 9 ай бұрын
Cool. Thanks
@IrtaMan228
@IrtaMan228 9 ай бұрын
Those are my favorite parts of C!
@vee.m
@vee.m 3 ай бұрын
My mind is blown
@truectl
@truectl 9 ай бұрын
Right.
@oj0024
@oj0024 9 ай бұрын
So how about you try to solve the first 3 days of advent of code at compile time with only the preprocessor.
@xravenx24fe
@xravenx24fe 9 ай бұрын
You can do that with Zig fairly easily if the problem doesn't have a lot of branching :)
@thespiralofpowah
@thespiralofpowah 9 ай бұрын
In the start, the bottom of the screen is SUS
@FunkschyIsWatchingYou
@FunkschyIsWatchingYou 9 ай бұрын
Will the government come after me now for knowing stuff I'm not supposed to? monkaS
@pajeetsingh
@pajeetsingh 9 ай бұрын
Is the background transparent?
@BennoKushnir
@BennoKushnir 9 ай бұрын
29:30 it's not "weirdness": int i = (int) double_var; is the usual method to cast a variable
@desertfish74
@desertfish74 9 ай бұрын
Recreazional zozing zession.
@BboyKeny
@BboyKeny 9 ай бұрын
29:00 That seems to me like it's practically function hoisting. This means the Uncle Bob's Clean Code is possible in C 😲
@goawqebt6931
@goawqebt6931 9 ай бұрын
It's just a fun definition
@adr420
@adr420 9 ай бұрын
Hello 👋 Everyone...
@glados_creator
@glados_creator 9 ай бұрын
you think you can use it with nobuild so like with interpreted scripts language except you fully compile everytime ; gentoo style
@glados_creator
@glados_creator 9 ай бұрын
gotta to love C
@aaronspeedy7780
@aaronspeedy7780 9 ай бұрын
Why couldn't you just have a simple function to restart the application at the current timestep?
@Njinx_
@Njinx_ 9 ай бұрын
Not as fun as messing with shared libraries
@TsodingDaily
@TsodingDaily 9 ай бұрын
Because this limits the potential state of the whole application to a single timestamp, which is probably not the limitation you want to have if you plan to create anything interesting.
@aaronspeedy7780
@aaronspeedy7780 9 ай бұрын
@@TsodingDaily If you want more state, couldn't you just add more arguments to the function that restarts the program? From what I see, the only limitation is that you need the entire state of your program to be viewable by a single function, which I guess could be hard for really large programs.
@aaronspeedy7780
@aaronspeedy7780 9 ай бұрын
Wait, never mind. Isn't it actually literally the same except that you're restarting the program one function at time instead of just restarting the program all at once, or am I missing something?
@user-ge2vc3rl1n
@user-ge2vc3rl1n 9 ай бұрын
why ifndef instead of pragma once ?
@garrysingh4484
@garrysingh4484 9 ай бұрын
👋 Need a Video to demonstrate Buffer overflow ??? How to create a buffer overflow and possible fix's !!!
@energy-tunes
@energy-tunes 9 ай бұрын
The only non grifter eceleb that can actually teach you a thing or two
@UnknownSENKu
@UnknownSENKu 9 ай бұрын
Thanks A lot. For me Hard to follow CASEY code but I follow your code . Thanks.....
@SlinkyD
@SlinkyD 9 ай бұрын
13:21 alternative method: gcc -E foo.c Do yo thang
@ricardo.mazeto
@ricardo.mazeto 8 ай бұрын
You need to stop saying "how'bout that". 🫤
Wait, but Who's Debugging the Debugger?!
59:53
Tsoding Daily
Рет қаралды 33 М.
Newton's Method in C
1:33:17
Tsoding Daily
Рет қаралды 12 М.
Каха с волосами
01:00
К-Media
Рет қаралды 6 МЛН
didn't want to let me in #tiktok
00:20
Анастасия Тарасова
Рет қаралды 12 МЛН
Normal vs Smokers !! 😱😱😱
00:12
Tibo InShape
Рет қаралды 92 МЛН
Do you have a friend like this? 🤣#shorts
00:12
dednahype
Рет қаралды 10 МЛН
Mastering Data Product Management: Expert Strategies with Panos Lazardis
15:15
Drag and Drop in C (Yes, it is possible! Let me show you how...)
1:18:02
Read a paper: Ropes-- an alternative to Strings
6:51
Vivek Haldar
Рет қаралды 7 М.
the cleanest feature in C that you've probably never heard of
8:13
Low Level Learning
Рет қаралды 122 М.
Is C++ better than C?
1:46:10
Tsoding Daily
Рет қаралды 30 М.
Can C actually do Perfect Bézier Curves?
2:17:22
Tsoding Daily
Рет қаралды 14 М.
I regret doing this...
1:20:07
Tsoding Daily
Рет қаралды 60 М.
Async Engine in C
3:12:16
Tsoding Daily
Рет қаралды 33 М.
I made a Russian Programming Language
2:06:44
Tsoding Daily
Рет қаралды 85 М.
Вы поможете украсть ваш iPhone
0:56
Romancev768
Рет қаралды 647 М.
С Какой Высоты Разобьётся NOKIA3310 ?!😳
0:43
Carregando telefone com carregador cortado
1:01
Andcarli
Рет қаралды 1,2 МЛН