No video

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

  Рет қаралды 56,463

Tsoding Daily

Tsoding Daily

Жыл бұрын

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

Пікірлер: 121
@julkiewicz
@julkiewicz Жыл бұрын
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 Жыл бұрын
You can't just say all that and then not share your grade with us
@julkiewicz
@julkiewicz Жыл бұрын
@@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 11 ай бұрын
​@@julkiewicz Share the compiler source code!
@marcs9451
@marcs9451 10 ай бұрын
I much rather hand write my parser than use ANTLR or Bison
@KeplerEmeritus
@KeplerEmeritus 9 ай бұрын
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…
@DVRC
@DVRC Жыл бұрын
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 Жыл бұрын
Can't find the paper, whats the title?
@DVRC
@DVRC Жыл бұрын
@@salman8562 "Ace: a syntax driven C preprocessor", James Gosling, 1989. Likely YT removed the comment where I linked it
@julians.2597
@julians.2597 4 ай бұрын
Pretty sure Zig kinda does that. Having a preprocessor act on source code is just a recipe for desaster
@yaksher
@yaksher Жыл бұрын
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 Жыл бұрын
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.
@SteveRowe
@SteveRowe 7 ай бұрын
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!
@cramble
@cramble Жыл бұрын
TSoding being amazing as always.
@alecs2056
@alecs2056 Жыл бұрын
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 Жыл бұрын
Oh, that's a cool idea! Thanks!
@Mikayex
@Mikayex Жыл бұрын
Also you can make a macro #define COMMA , to allow using commas in the second argument
@alecs2056
@alecs2056 Жыл бұрын
@@Mikayex i think that using variadic argoments in the macro is cleaner
@michalbotor
@michalbotor Жыл бұрын
(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 Жыл бұрын
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.
@pedrorondon2617
@pedrorondon2617 Жыл бұрын
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!
@anon_y_mousse
@anon_y_mousse Жыл бұрын
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);
@Vank4o
@Vank4o Жыл бұрын
Even after 50 years C is just amazing!
@tomaszstanislawski457
@tomaszstanislawski457 2 ай бұрын
Actually, multiple functions can be declared with the one declaration. `void foo(void), bar(void), baz(void);`. Or with a typedefed function type `typedef void foo_f(void); foo_f foo, bar, baz;`. This trick can be used for compressing headers. One can even mix function and variable declarations: `int foo(), var = 3;`. C is a simple language and let mix simple things together in a very strange ways. Function types is one of the obscure features that were present in C since it very beginning.
@phantomwizard5892
@phantomwizard5892 Жыл бұрын
The amount of knowldege ive gain from you these past couple of months is just insane Thanks a lot sir!
@diegorocha2186
@diegorocha2186 Жыл бұрын
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!
@ruffianeo3418
@ruffianeo3418 Жыл бұрын
"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 Жыл бұрын
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
@FireDragon91245
@FireDragon91245 Жыл бұрын
man i watch 1 of your videos and instantly get the uncontrolable urge to code a shit ton of C
@poijmc606
@poijmc606 Жыл бұрын
Tsoding casually writing C language black magic curses
@karmavil4034
@karmavil4034 Жыл бұрын
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
@skope2055
@skope2055 Жыл бұрын
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 Жыл бұрын
#define def_func(func_name, // insert function here to use helper function , helper_function_name) \ new_function_here Something like this?
@rotbjorn
@rotbjorn Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
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 Жыл бұрын
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
@arthurgrillo3462
@arthurgrillo3462 Жыл бұрын
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
@donkeyy8331
@donkeyy8331 Жыл бұрын
this is the type of thing you don't see or hear about in your day to day, thank you for the knowledge.
@OneWingedShark
@OneWingedShark Жыл бұрын
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 Жыл бұрын
I assume the problem is some combination of: mixed levels of abstraction, confusing syntax and historical artifacts ?
@OneWingedShark
@OneWingedShark Жыл бұрын
@@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 11 ай бұрын
@@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 11 ай бұрын
@@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.
@paklenizmaj
@paklenizmaj Жыл бұрын
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.
@drdca8263
@drdca8263 Жыл бұрын
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 Жыл бұрын
I’m glad you say that, I think everyone feels incompetent watching tsoding sessions😂
@tbkih
@tbkih 3 ай бұрын
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
@FelipeMarkson
@FelipeMarkson Жыл бұрын
This feature to disable hot reloading was even more amazing of add hot reloading... God dammit C consistency
@octagonal8905
@octagonal8905 Жыл бұрын
Lol, love the SCP-087 thumbnail
@TheRailroad99
@TheRailroad99 Жыл бұрын
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
@screamyboy4722
@screamyboy4722 Жыл бұрын
This man brain can simulate the universe
@WilderPoo
@WilderPoo Жыл бұрын
Watching you keeps me motivated, thank you! 😁
@zoltankurti
@zoltankurti Жыл бұрын
This is exactly how I deal with loading vulkan functions.
@zoltankurti
@zoltankurti Жыл бұрын
Not exactly. My list of functions is in a different file and I just include that file after defining the macro and before undef.
@luserdroog
@luserdroog 11 ай бұрын
X-macros! Hurray!
@pascualsu
@pascualsu Жыл бұрын
Top notch programming channel for sure
@yjk_ch
@yjk_ch Жыл бұрын
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!
@FunkschyIsWatchingYou
@FunkschyIsWatchingYou Жыл бұрын
Will the government come after me now for knowing stuff I'm not supposed to? monkaS
@mrmaniac9905
@mrmaniac9905 Жыл бұрын
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?
@CoderDBF
@CoderDBF Жыл бұрын
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.
@SingleTheShot
@SingleTheShot Жыл бұрын
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?
@MisererePart
@MisererePart Жыл бұрын
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.
@oj0024
@oj0024 Жыл бұрын
So how about you try to solve the first 3 days of advent of code at compile time with only the preprocessor.
@xravenx24fe
@xravenx24fe Жыл бұрын
You can do that with Zig fairly easily if the problem doesn't have a lot of branching :)
@NeZversSounds
@NeZversSounds Жыл бұрын
Hands down my favorite video! Learned a lot from it.
@FredericoKlein
@FredericoKlein Жыл бұрын
I admire your memory. If I did this to my code in like 1 week i would no longer remember how it worked.
@KafeinBE
@KafeinBE Жыл бұрын
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.
@weakspirit_
@weakspirit_ 10 ай бұрын
macros in C! what we would we do without em
@wrfsh
@wrfsh 7 ай бұрын
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.
@tomaspecl1082
@tomaspecl1082 Жыл бұрын
Cool stuff!
@emperorpalpatine6080
@emperorpalpatine6080 Жыл бұрын
That's an SCP. But I prefer the SC++P
@luserdroog
@luserdroog 11 ай бұрын
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.
@BboyKeny
@BboyKeny Жыл бұрын
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 Жыл бұрын
It's just a fun definition
@OM-yn8pt
@OM-yn8pt Жыл бұрын
Would you consider investigating the comparison of sound files from the ground up, i.e. checking the similarity between two audio signals
@einsjannis
@einsjannis Жыл бұрын
Very cool thank you very much for sharing
@DarioSterzi
@DarioSterzi 11 ай бұрын
Damn this was surprisingly fun
@anilgr1111
@anilgr1111 Жыл бұрын
Love from India ❣
@bbq1423
@bbq1423 Жыл бұрын
You could probably use varargs to put the function parameters in the macro
@mithrandirek1813
@mithrandirek1813 Жыл бұрын
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?
@lolcat69
@lolcat69 Жыл бұрын
I love when TypeScript-oding does C coding :)
@rolandinnamorato1953
@rolandinnamorato1953 10 ай бұрын
Legcoded X Macros when?
@thespiralofpowah
@thespiralofpowah Жыл бұрын
In the start, the bottom of the screen is SUS
@kuijaye
@kuijaye Жыл бұрын
As some say: dark corners of c
@tcurdt
@tcurdt 8 ай бұрын
what are you using to create the window and draw?
@BennoKushnir
@BennoKushnir Жыл бұрын
29:30 it's not "weirdness": int i = (int) double_var; is the usual method to cast a variable
@glados_creator
@glados_creator Жыл бұрын
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 Жыл бұрын
gotta to love C
@darioabbece3948
@darioabbece3948 Жыл бұрын
This is forbidden C
@vee.m
@vee.m 6 ай бұрын
My mind is blown
@desertfish74
@desertfish74 Жыл бұрын
Recreazional zozing zession.
@truectl
@truectl Жыл бұрын
Right.
@ksco3309
@ksco3309 Жыл бұрын
I read and wrote these macros a lot, now I finally know its name!
@OsamaHasan000
@OsamaHasan000 Жыл бұрын
please can you make some beginner level project from scratch????????????????/
@business548
@business548 Жыл бұрын
What font you use for terminal?
@doce3609
@doce3609 Жыл бұрын
wow this is very cool
@IrtaMan228
@IrtaMan228 Жыл бұрын
Those are my favorite parts of C!
@pajeetsingh
@pajeetsingh Жыл бұрын
Is the background transparent?
@alurma
@alurma Жыл бұрын
Cool. Thanks
@Asia_Bangladesh
@Asia_Bangladesh Жыл бұрын
Why you use Emac not VIM? Make a compare video about Vim Emac And VS code
@adr420
@adr420 Жыл бұрын
Hello 👋 Everyone...
@aaronspeedy7780
@aaronspeedy7780 Жыл бұрын
Why couldn't you just have a simple function to restart the application at the current timestep?
@Njinx_
@Njinx_ Жыл бұрын
Not as fun as messing with shared libraries
@TsodingDaily
@TsodingDaily Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
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 Жыл бұрын
why ifndef instead of pragma once ?
@garrysingh4484
@garrysingh4484 Жыл бұрын
👋 Need a Video to demonstrate Buffer overflow ??? How to create a buffer overflow and possible fix's !!!
@energy-tunes
@energy-tunes Жыл бұрын
The only non grifter eceleb that can actually teach you a thing or two
@UnknownSENKu
@UnknownSENKu Жыл бұрын
Thanks A lot. For me Hard to follow CASEY code but I follow your code . Thanks.....
@SlinkyD
@SlinkyD Жыл бұрын
13:21 alternative method: gcc -E foo.c Do yo thang
@ricardo.mazeto
@ricardo.mazeto 11 ай бұрын
You need to stop saying "how'bout that". 🫤
Wait, but Who's Debugging the Debugger?!
59:53
Tsoding Daily
Рет қаралды 34 М.
the cleanest feature in C that you've probably never heard of
8:13
Low Level Learning
Рет қаралды 133 М.
The Joker saves Harley Quinn from drowning!#joker  #shorts
00:34
Untitled Joker
Рет қаралды 55 МЛН
Running With Bigger And Bigger Feastables
00:17
MrBeast
Рет қаралды 53 МЛН
Bony Just Wants To Take A Shower #animation
00:10
GREEN MAX
Рет қаралды 6 МЛН
My Program Sucks!
59:09
Tsoding Daily
Рет қаралды 23 М.
These Illusions Fool Almost Everyone
24:55
Veritasium
Рет қаралды 1,9 МЛН
Zig 0.13.0 - Overview and Updates
8:25
Zig Is Great
Рет қаралды 17 М.
2 Years Of Learning C | Prime Reacts
22:24
ThePrimeTime
Рет қаралды 270 М.
I regret doing this...
1:20:07
Tsoding Daily
Рет қаралды 69 М.
SOME UNIQUE C++ CODE! // Pacman Clone Code Review
26:42
The Cherno
Рет қаралды 270 М.
Configuring my Emacs. Vim users are not invited!
16:37
Tsoding
Рет қаралды 87 М.
Will Ada Replace C/C++?
44:57
Tsoding
Рет қаралды 89 М.
Dynamic Arrays in C
11:46
Dylan Falconer
Рет қаралды 66 М.
Neural Network in C that Recognizes Shapes
3:43:48
Tsoding Daily
Рет қаралды 36 М.
The Joker saves Harley Quinn from drowning!#joker  #shorts
00:34
Untitled Joker
Рет қаралды 55 МЛН