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

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 พ.ย. 2024
  • Previous Episodes: • Music Visualizer
    References:
    Music by / @nu11_ft
    X macro: en.wikipedia.o...

ความคิดเห็น • 121

  • @julkiewitz
    @julkiewitz ปีที่แล้ว +210

    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 ปีที่แล้ว +24

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

    • @julkiewitz
      @julkiewitz ปีที่แล้ว +20

      @@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 ปีที่แล้ว +4

      ​@@julkiewitz Share the compiler source code!

    • @marcs9451
      @marcs9451 ปีที่แล้ว +5

      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 ปีที่แล้ว +70

    TSoding being amazing as always.

  • @yaksher
    @yaksher ปีที่แล้ว +55

    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 ปีที่แล้ว +12

      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 ปีที่แล้ว +58

    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 ปีที่แล้ว +3

      Can't find the paper, whats the title?

    • @DVRC
      @DVRC ปีที่แล้ว +15

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

    • @julians.2597
      @julians.2597 7 หลายเดือนก่อน +2

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

  • @SteveRowe
    @SteveRowe 10 หลายเดือนก่อน +3

    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!

  • @poijmc606
    @poijmc606 ปีที่แล้ว +7

    Tsoding casually writing C language black magic curses

  • @pedrorondon2617
    @pedrorondon2617 ปีที่แล้ว +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!

  • @alecs2056
    @alecs2056 ปีที่แล้ว +29

    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  ปีที่แล้ว +16

      Oh, that's a cool idea! Thanks!

    • @Mikayex
      @Mikayex ปีที่แล้ว +2

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

    • @alecs2056
      @alecs2056 ปีที่แล้ว +5

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

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

  • @Vank4o
    @Vank4o ปีที่แล้ว +6

    Even after 50 years C is just amazing!

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

  • @diegorocha2186
    @diegorocha2186 ปีที่แล้ว +4

    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!

  • @phantomwizard5892
    @phantomwizard5892 ปีที่แล้ว +24

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

  • @JekasObps
    @JekasObps ปีที่แล้ว +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 ปีที่แล้ว

      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

  • @donkeyy8331
    @donkeyy8331 ปีที่แล้ว +2

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

  • @pascualsu
    @pascualsu ปีที่แล้ว +1

    Top notch programming channel for sure

  • @anon_y_mousse
    @anon_y_mousse ปีที่แล้ว +5

    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);

  • @karmavil4034
    @karmavil4034 ปีที่แล้ว +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

  • @ruffianeo3418
    @ruffianeo3418 ปีที่แล้ว +5

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

  • @arthurgrillo3462
    @arthurgrillo3462 ปีที่แล้ว +4

    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

  • @francklenormand2680
    @francklenormand2680 ปีที่แล้ว +4

    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

  • @paklenizmaj
    @paklenizmaj ปีที่แล้ว +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.

  • @octagonal8905
    @octagonal8905 ปีที่แล้ว +2

    Lol, love the SCP-087 thumbnail

  • @skope2055
    @skope2055 ปีที่แล้ว +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 ปีที่แล้ว +1

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

    • @rotbjorn
      @rotbjorn ปีที่แล้ว +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 ปีที่แล้ว

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

  • @WilderPoo
    @WilderPoo ปีที่แล้ว

    Watching you keeps me motivated, thank you! 😁

  • @FireDragon91245
    @FireDragon91245 ปีที่แล้ว +2

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

  • @tomaszstanislawski457
    @tomaszstanislawski457 4 หลายเดือนก่อน +1

    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.

  • @FelipeMarkson
    @FelipeMarkson ปีที่แล้ว +1

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

  • @NeZversSounds
    @NeZversSounds ปีที่แล้ว

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

  • @OneWingedShark
    @OneWingedShark ปีที่แล้ว +10

    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 ปีที่แล้ว +4

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

    • @OneWingedShark
      @OneWingedShark ปีที่แล้ว +3

      @@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 ปีที่แล้ว

      @@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 ปีที่แล้ว

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

  • @luserdroog
    @luserdroog ปีที่แล้ว +1

    X-macros! Hurray!

  • @screamyboy4722
    @screamyboy4722 ปีที่แล้ว +1

    This man brain can simulate the universe

  • @FunkschyIsWatchingYou
    @FunkschyIsWatchingYou ปีที่แล้ว +2

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

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

  • @drdca8263
    @drdca8263 ปีที่แล้ว +15

    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 ปีที่แล้ว +5

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

  • @TheRailroad99
    @TheRailroad99 ปีที่แล้ว +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

  • @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!

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

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

  • @tbkih
    @tbkih 6 หลายเดือนก่อน

    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

  • @MisererePart
    @MisererePart ปีที่แล้ว +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.

  • @starc0w
    @starc0w 23 วันที่ผ่านมา

    Great stuff!

  • @wrfsh
    @wrfsh 10 หลายเดือนก่อน

    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.

  • @luserdroog
    @luserdroog ปีที่แล้ว

    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.

  • @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 ปีที่แล้ว +1

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

  • @darioabbece3948
    @darioabbece3948 ปีที่แล้ว +1

    This is forbidden C

  • @emperorpalpatine6080
    @emperorpalpatine6080 ปีที่แล้ว +1

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

  • @weakspirit_
    @weakspirit_ ปีที่แล้ว

    macros in C! what we would we do without em

  • @einsjannis
    @einsjannis ปีที่แล้ว

    Very cool thank you very much for sharing

  • @lolcat69
    @lolcat69 ปีที่แล้ว

    I love when TypeScript-oding does C coding :)

  • @SingleTheShot
    @SingleTheShot ปีที่แล้ว +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?

  • @KafeinBE
    @KafeinBE ปีที่แล้ว +5

    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.

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

  • @alexanderzikal7244
    @alexanderzikal7244 2 หลายเดือนก่อน

    You are a Genius ^ ^!!!

  • @mrmaniac9905
    @mrmaniac9905 ปีที่แล้ว +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?

  • @tomaspecl1082
    @tomaspecl1082 ปีที่แล้ว +1

    Cool stuff!

  • @ksco3309
    @ksco3309 ปีที่แล้ว

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

  • @DarioSterzi
    @DarioSterzi ปีที่แล้ว

    Damn this was surprisingly fun

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

  • @bbq1423
    @bbq1423 ปีที่แล้ว

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

  • @IrtaMan228
    @IrtaMan228 ปีที่แล้ว

    Those are my favorite parts of C!

  • @tcurdt
    @tcurdt 11 หลายเดือนก่อน

    what are you using to create the window and draw?

  • @anilgr1111
    @anilgr1111 ปีที่แล้ว +1

    Love from India ❣

  • @glados_creator
    @glados_creator ปีที่แล้ว +1

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

  • @desertfish74
    @desertfish74 ปีที่แล้ว

    Recreazional zozing zession.

  • @business548
    @business548 ปีที่แล้ว

    What font you use for terminal?

  • @rolandinnamorato1953
    @rolandinnamorato1953 ปีที่แล้ว

    Legcoded X Macros when?

  • @OsamaHasan000
    @OsamaHasan000 ปีที่แล้ว

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

  • @BennoKushnir
    @BennoKushnir ปีที่แล้ว

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

  • @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?

  • @vee.m
    @vee.m 8 หลายเดือนก่อน

    My mind is blown

  • @thespiralofpowah
    @thespiralofpowah ปีที่แล้ว

    In the start, the bottom of the screen is SUS

  • @pajeetsingh
    @pajeetsingh ปีที่แล้ว

    Is the background transparent?

  • @truectl
    @truectl ปีที่แล้ว

    Right.

  • @doce3609
    @doce3609 ปีที่แล้ว

    wow this is very cool

  • @alurma
    @alurma ปีที่แล้ว

    Cool. Thanks

  • @aaronspeedy7780
    @aaronspeedy7780 ปีที่แล้ว

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

    • @Njinx_
      @Njinx_ ปีที่แล้ว +2

      Not as fun as messing with shared libraries

    • @TsodingDaily
      @TsodingDaily  ปีที่แล้ว +7

      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 ปีที่แล้ว +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 ปีที่แล้ว

    Hello 👋 Everyone...

  • @Asia_Bangladesh
    @Asia_Bangladesh ปีที่แล้ว +1

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

  • @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 !!!

  • @UnknownSENKu
    @UnknownSENKu ปีที่แล้ว +1

    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 ปีที่แล้ว

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