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

Sdílet
Vložit
  • čas přidán 26. 07. 2023
  • Previous Episodes: • Music Visualizer
    References:
    - Music by / @nu11_ft
    - X macro: en.wikipedia.org/wiki/X_macro
  • Věda a technologie

Komentáře • 121

  • @julkiewicz
    @julkiewicz Před 11 měsíci +191

    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 Před 11 měsíci +23

      You can't just say all that and then not share your grade with us

    • @julkiewicz
      @julkiewicz Před 11 měsíci +19

      @@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 Před 10 měsíci +4

      ​@@julkiewicz Share the compiler source code!

    • @marcs9451
      @marcs9451 Před 9 měsíci +4

      I much rather hand write my parser than use ANTLR or Bison

    • @KeplerEmeritus
      @KeplerEmeritus Před 8 měsíci +3

      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 Před 11 měsíci +67

    TSoding being amazing as always.

  • @yaksher
    @yaksher Před 11 měsíci +49

    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 Před 10 měsíci +10

      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.

  • @DVRC
    @DVRC Před 11 měsíci +56

    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 Před 11 měsíci +3

      Can't find the paper, whats the title?

    • @DVRC
      @DVRC Před 11 měsíci +14

      @@salman8562 "Ace: a syntax driven C preprocessor", James Gosling, 1989.
      Likely YT removed the comment where I linked it

    • @julians.2597
      @julians.2597 Před 3 měsíci +1

      Pretty sure Zig kinda does that. Having a preprocessor act on source code is just a recipe for desaster

  • @alecs2056
    @alecs2056 Před 11 měsíci +25

    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  Před 11 měsíci +13

      Oh, that's a cool idea! Thanks!

    • @Mikayex
      @Mikayex Před 11 měsíci +2

      Also you can make a macro #define COMMA , to allow using commas in the second argument

    • @alecs2056
      @alecs2056 Před 11 měsíci +5

      @@Mikayex i think that using variadic argoments in the macro is cleaner

  • @poijmc606
    @poijmc606 Před 11 měsíci +4

    Tsoding casually writing C language black magic curses

  • @michalbotor
    @michalbotor Před 11 měsíci +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.

  • @diegorocha2186
    @diegorocha2186 Před 11 měsíci +3

    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!

  • @Vank4o
    @Vank4o Před 11 měsíci +4

    Even after 50 years C is just amazing!

  • @WilderPoo
    @WilderPoo Před 11 měsíci

    Watching you keeps me motivated, thank you! 😁

  • @phantomwizard5892
    @phantomwizard5892 Před 11 měsíci +22

    The amount of knowldege ive gain from you these past couple of months is just insane
    Thanks a lot sir!

  • @pedrorondon2617
    @pedrorondon2617 Před 11 měsíci +4

    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!

  • @NeZversSounds
    @NeZversSounds Před 11 měsíci

    Hands down my favorite video! Learned a lot from it.

  • @ruffianeo3418
    @ruffianeo3418 Před 11 měsíci +3

    "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 :)

  • @portersky
    @portersky Před 11 měsíci +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.

  • @SteveRowe
    @SteveRowe Před 6 měsíci +1

    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!

  • @pascualsu
    @pascualsu Před 11 měsíci +1

    Top notch programming channel for sure

  • @anon_y_mousse
    @anon_y_mousse Před 11 měsíci +4

    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 Před 11 měsíci

    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.

  • @francklenormand2680
    @francklenormand2680 Před 11 měsíci +3

    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

  • @arthurgrillo3462
    @arthurgrillo3462 Před 11 měsíci +3

    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

  • @octagonal8905
    @octagonal8905 Před 11 měsíci +2

    Lol, love the SCP-087 thumbnail

  • @mrmaniac9905
    @mrmaniac9905 Před 11 měsíci +2

    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?

  • @OM-yn8pt
    @OM-yn8pt Před 11 měsíci

    Would you consider investigating the comparison of sound files from the ground up, i.e. checking the similarity between two audio signals

  • @donkeyy8331
    @donkeyy8331 Před 11 měsíci

    this is the type of thing you don't see or hear about in your day to day, thank you for the knowledge.

  • @tomaszstanislawski457
    @tomaszstanislawski457 Před 21 dnem

    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.

  • @skope2055
    @skope2055 Před 11 měsíci +6

    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 Před 11 měsíci +1

      #define def_func(func_name,
      // insert function here to use helper function
      , helper_function_name) \
      new_function_here
      Something like this?

    • @rotbjorn
      @rotbjorn Před 11 měsíci +1

      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 Před 11 měsíci

      @@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).

  • @FireDragon91245
    @FireDragon91245 Před 11 měsíci +1

    man i watch 1 of your videos and instantly get the uncontrolable urge to code a shit ton of C

  • @einsjannis
    @einsjannis Před 11 měsíci

    Very cool thank you very much for sharing

  • @SingleTheShot
    @SingleTheShot Před 11 měsíci +3

    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 Před 11 měsíci +1

    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.

  • @karmavil4034
    @karmavil4034 Před 11 měsíci +2

    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

  • @tomaspecl1082
    @tomaspecl1082 Před 11 měsíci +1

    Cool stuff!

  • @paklenizmaj
    @paklenizmaj Před 11 měsíci +7

    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.

  • @JekasObps
    @JekasObps Před 11 měsíci +2

    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 Před 11 měsíci

      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

  • @screamyboy4722
    @screamyboy4722 Před 11 měsíci +1

    This man brain can simulate the universe

  • @FelipeMarkson
    @FelipeMarkson Před 11 měsíci +1

    This feature to disable hot reloading was even more amazing of add hot reloading... God dammit C consistency

  • @tcurdt
    @tcurdt Před 7 měsíci

    what are you using to create the window and draw?

  • @tbkih
    @tbkih Před 2 měsíci

    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

  • @yjk_ch
    @yjk_ch Před 11 měsíci

    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!

  • @OneWingedShark
    @OneWingedShark Před 11 měsíci +6

    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 Před 11 měsíci +2

      I assume the problem is some combination of: mixed levels of abstraction, confusing syntax and historical artifacts ?

    • @OneWingedShark
      @OneWingedShark Před 11 měsíci +2

      @@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 Před 10 měsíci

      @@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 Před 10 měsíci

      @@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.

  • @drdca8263
    @drdca8263 Před 11 měsíci +14

    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 Před 11 měsíci +5

      I’m glad you say that, I think everyone feels incompetent watching tsoding sessions😂

  • @FredericoKlein
    @FredericoKlein Před 10 měsíci

    I admire your memory. If I did this to my code in like 1 week i would no longer remember how it worked.

  • @wrfsh
    @wrfsh Před 6 měsíci

    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.

  • @zoltankurti
    @zoltankurti Před 11 měsíci

    This is exactly how I deal with loading vulkan functions.

    • @zoltankurti
      @zoltankurti Před 11 měsíci

      Not exactly. My list of functions is in a different file and I just include that file after defining the macro and before undef.

  • @business548
    @business548 Před 11 měsíci

    What font you use for terminal?

  • @TheRailroad99
    @TheRailroad99 Před 10 měsíci +1

    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

  • @bbq1423
    @bbq1423 Před 11 měsíci

    You could probably use varargs to put the function parameters in the macro

  • @mithrandirek1813
    @mithrandirek1813 Před 11 měsíci

    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?

  • @Asia_Bangladesh
    @Asia_Bangladesh Před 11 měsíci +1

    Why you use Emac not VIM? Make a compare video about Vim Emac And VS code

  • @weakspirit_
    @weakspirit_ Před 9 měsíci

    macros in C! what we would we do without em

  • @oj0024
    @oj0024 Před 11 měsíci +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 Před 11 měsíci +1

      You can do that with Zig fairly easily if the problem doesn't have a lot of branching :)

  • @DarioSterzi
    @DarioSterzi Před 9 měsíci

    Damn this was surprisingly fun

  • @FunkschyIsWatchingYou
    @FunkschyIsWatchingYou Před 11 měsíci +1

    Will the government come after me now for knowing stuff I'm not supposed to? monkaS

  • @emperorpalpatine6080
    @emperorpalpatine6080 Před 11 měsíci +1

    That's an SCP.
    But I prefer the SC++P

  • @lolcat69
    @lolcat69 Před 11 měsíci

    I love when TypeScript-oding does C coding :)

  • @truectl
    @truectl Před 11 měsíci

    Right.

  • @doce3609
    @doce3609 Před 11 měsíci

    wow this is very cool

  • @ksco3309
    @ksco3309 Před 11 měsíci

    I read and wrote these macros a lot, now I finally know its name!

  • @IrtaMan228
    @IrtaMan228 Před 11 měsíci

    Those are my favorite parts of C!

  • @OsamaHasan000
    @OsamaHasan000 Před 11 měsíci

    please can you make some beginner level project from scratch????????????????/

  • @luserdroog
    @luserdroog Před 10 měsíci

    X-macros! Hurray!

  • @vee.m
    @vee.m Před 4 měsíci

    My mind is blown

  • @glados_creator
    @glados_creator Před 11 měsíci +1

    you think you can use it with nobuild so like with interpreted scripts language except you fully compile everytime ; gentoo style

  • @kuijaye
    @kuijaye Před 11 měsíci

    As some say: dark corners of c

  • @pajeetsingh
    @pajeetsingh Před 11 měsíci

    Is the background transparent?

  • @BennoKushnir
    @BennoKushnir Před 11 měsíci

    29:30 it's not "weirdness": int i = (int) double_var; is the usual method to cast a variable

  • @alurma
    @alurma Před 11 měsíci

    Cool. Thanks

  • @darioabbece3948
    @darioabbece3948 Před 11 měsíci

    This is forbidden C

  • @rolandinnamorato1953
    @rolandinnamorato1953 Před 9 měsíci

    Legcoded X Macros when?

  • @luserdroog
    @luserdroog Před 10 měsíci

    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.

  • @anilgr1111
    @anilgr1111 Před 11 měsíci +1

    Love from India ❣

  • @BboyKeny
    @BboyKeny Před 11 měsíci

    29:00 That seems to me like it's practically function hoisting. This means the Uncle Bob's Clean Code is possible in C 😲

  • @desertfish74
    @desertfish74 Před 11 měsíci

    Recreazional zozing zession.

  • @KafeinBE
    @KafeinBE Před 11 měsíci +4

    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.

  • @thespiralofpowah
    @thespiralofpowah Před 11 měsíci

    In the start, the bottom of the screen is SUS

  • @aaronspeedy7780
    @aaronspeedy7780 Před 11 měsíci

    Why couldn't you just have a simple function to restart the application at the current timestep?

    • @Njinx_
      @Njinx_ Před 11 měsíci +2

      Not as fun as messing with shared libraries

    • @TsodingDaily
      @TsodingDaily  Před 11 měsíci +6

      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 Před 11 měsíci

      @@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 Před 11 měsíci +1

      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?

  • @adr420
    @adr420 Před 11 měsíci

    Hello 👋 Everyone...

  • @user-ge2vc3rl1n
    @user-ge2vc3rl1n Před 11 měsíci

    why ifndef instead of pragma once ?

  • @garrysingh4484
    @garrysingh4484 Před 11 měsíci

    👋 Need a Video to demonstrate Buffer overflow ???
    How to create a buffer overflow and possible fix's !!!

  • @energy-tunes
    @energy-tunes Před 11 měsíci

    The only non grifter eceleb that can actually teach you a thing or two

  • @UnknownSENKu
    @UnknownSENKu Před 11 měsíci +1

    Thanks A lot. For me Hard to follow CASEY code but I follow your code . Thanks.....

  • @SlinkyD
    @SlinkyD Před 11 měsíci

    13:21 alternative method: gcc -E foo.c
    Do yo thang

  • @ricardo.mazeto
    @ricardo.mazeto Před 10 měsíci

    You need to stop saying "how'bout that". 🫤