The Ternary Operator (examples in C and C++)

Поделиться
HTML-код
  • Опубликовано: 22 ноя 2024

Комментарии • 60

  • @BlitterObject
    @BlitterObject 3 года назад +13

    And another great thing about ternary is that it can be used to initialize const variables:
    int const themax = (num1 > num2) ? num1 : num2;

    • @EasyElectronics2412
      @EasyElectronics2412 3 года назад

      Num1 and num2 must be macros or constants

    • @BlitterObject
      @BlitterObject 3 года назад

      @@EasyElectronics2412 Well, no, they can be any expression that yields a value of the correct type (int, in this case) so can come from anywhere: other non-const ints, function, macro, constant, or otherwise.

    • @homelikebrick42
      @homelikebrick42 3 года назад +1

      @@EasyElectronics2412 making it const just means you cant change it not that it has to be a compile time value

    • @EasyElectronics2412
      @EasyElectronics2412 3 года назад

      @@homelikebrick42 true i got that😅

  • @cmdlp4178
    @cmdlp4178 3 года назад +13

    Deleting/destructing an ostream has sideeffects, namely flushing the rest inside the buffer. If you forget to delete/close the stream, the file contents might not be completely written to the file.

    • @JacobSorber
      @JacobSorber  3 года назад +5

      Ah, good point. I was just thinking about memory clean up. Thanks.

  • @DipietroGuido
    @DipietroGuido 3 года назад +10

    A suggestion for a future video perhaps: C++ intro for C programmers (stuff that can be done better or differently in C++ and that C programmers would find helpful)

    • @JacobSorber
      @JacobSorber  3 года назад +7

      Thanks for the idea. I'll see what I can do.

    • @JonnyRobbie
      @JonnyRobbie 3 года назад

      @@JacobSorber That can be surprisingly hard, because C++ is not a simple extension as it might seem. It requirec somplete paradigm shift and different thinking. There's a really good chamce a C programmer will write a terrible C++ code that still may compile.

    • @NonTwinBrothers
      @NonTwinBrothers Год назад

      I'm only now realizing I'm a year late on this one. But there's a great intro video from Bisqwit called "C++ for C programmers".
      It is mostly a summary, and no further videos in the series have been made yet, unfortunately. He pretty much explains what we'd now call "C with C++ features". I def. recommend

  • @lef1970
    @lef1970 3 года назад +1

    I use the ternary operator to safe wrap non safe functions, ie: return str == NULL ? 0 : strlen(str); it’s a good fit for wrapping functions that can be a termination point

  • @enkidughom2508
    @enkidughom2508 3 года назад +6

    Thia channel is a RUclips treasure. I must have shared it with dozens with my friends! Thank you prof for the effort and generosity.
    I am getting a co-op next term and will definitely join your pateron 😁.

    • @JacobSorber
      @JacobSorber  3 года назад +3

      You're welcome. I'm glad I could help.

  • @derekkonigsberg2047
    @derekkonigsberg2047 3 года назад +1

    I think I first learned about the ternary operator alongside trigraphs in a presentation on fun tricks to confuse your TAs.

  • @portblock
    @portblock 2 года назад

    one of the ways I explain it as a true/false return. it will return first part if true, second part if false. I usually show people this (in php)
    $strSuffix = ($countPeople == 1) ? "Person" : "People"

  • @reverse_shell
    @reverse_shell 3 года назад

    YES, more pointer and reference coverage please.

  • @adriancostin9138
    @adriancostin9138 3 года назад

    thank you for the tip with the ternary operator used to switch between cout and ofstream based on argc. that's a really neat trick.

  • @AlexJacksonSmith
    @AlexJacksonSmith 2 года назад

    Cool, ternary is like the "IF" in excel... C++ looks like a nightmare... :) Thanks A.,

  • @benjaminshinar9509
    @benjaminshinar9509 3 года назад +1

    explainning C code - use ternary operator for max().
    explainning c++ code - ostream, refernces, pointers, memory leaks.
    although you probably could use the occasion to introduce sprintf() and the rest of the family.

  • @unperrier5998
    @unperrier5998 2 года назад

    I'd personally not use a ternary expression to assign the local variable, but rather a function that contains the code (in either form).
    The point being that the name of the function should be enough (if chosen well) to understand what the code does.
    That's how I make code more readable, even though it requires to jump to the definition to said new function (virtually free with the right IDE)

  • @zxuiji
    @zxuiji 2 года назад

    4:26, sorry but that's still fine in a ternary, just needs formatting, I tend to put large expressions on multiple lines with the brackets on separate lines also, even tabbing the contents forward to make clear it's part of the expression, although I would not normally stick a loop in there, rather I didn't even realise could put loops in expressions, how would the compiler know what value to give it in that situation?

  • @amirrezaa974
    @amirrezaa974 3 года назад

    I wasn't able to find your video on memory leaks and why they aren't always important. Could you possibly share a link to it either in the video description or here in the comments?

  • @worldshaper1723
    @worldshaper1723 3 года назад

    Thank you for that. Can you me out?
    I need to gain some understanding of the C lang NeuraLink like devices.

  • @Brock-Landers
    @Brock-Landers 3 года назад

    Also. If using ternary operators with printf or other formatting funcs there is no need for the result variable:
    printf("max of %d and %d is %d.
    ", num1, num2, num1 > num2 ? num1 : num2);

    • @JacobSorber
      @JacobSorber  3 года назад +1

      Very true, though the more we try to do on one line...well things get harder to visually parse. This one is pretty straightforward, but there's a risk of readabaility issues.

  • @apostolisparga
    @apostolisparga 3 года назад +2

    An assembly output comparison of the two programs would be interesting to discuss. Is using the ternary operator more efficient than an if-else statement. If the answer is obvious, sorry for wasting your time...
    Thanks

    • @apostolisparga
      @apostolisparga 3 года назад

      Just quickly ran gcc -S on both versions of the C program and the compiler output on my machine was the same-verbatim. Is that to be expected? Is it a similar situation to comparing and infinite 'for' loop and a while loop?

    • @JacobSorber
      @JacobSorber  3 года назад +2

      Hi Tolis! It is interesting to look at. I didn't both because of time and because it's really going to be compiler (and compiler-option) dependent. Maybe something to look at in a future video. Thanks!

    • @luigiq6429
      @luigiq6429 3 года назад

      @@JacobSorber I heard somewhere that the ternary operator can mess with RVO and can sometimes confuse the compiler. That might be something to consider when dealing with objects that aren't trivially copyable...

    • @apostolisparga
      @apostolisparga 3 года назад

      @@JacobSorber Absolute Legend!

  • @sghsghdk
    @sghsghdk 3 года назад

    Regarding the memory leak. GCC has builtin address sanatizer and leak detector so freeing the memory the to be preferred because it will complain about it and always having a certain number of "stuff we do not deallocate" is inconvenient.

  • @sghsghdk
    @sghsghdk 3 года назад +2

    My programming teacher at school referred to usage of the ternary operator a a SAOL
    S-mart
    A-ss
    O-ne
    L-iner

  • @GooogleGoglee
    @GooogleGoglee 3 года назад

    @10:00 @line 18th
    The assign upstream &myout = *pmyout
    Is not really clear to me. Why using the * and why not using the & since I just declared myout

  • @Od3o_ela_sabil_rabek
    @Od3o_ela_sabil_rabek 3 года назад +2

    not equal (!=) how could you write it like that (≠)in videos?

    • @Od3o_ela_sabil_rabek
      @Od3o_ela_sabil_rabek 3 года назад +1

      @Alfie Aha sorry I'm a notepad++ user 😀

    • @JaccovanSchaik
      @JaccovanSchaik 3 года назад +1

      ruclips.net/video/5td9upDbkaU/видео.html

    • @nofilqasim
      @nofilqasim 3 года назад +2

      He is using a font with ligatures

    • @Od3o_ela_sabil_rabek
      @Od3o_ela_sabil_rabek 3 года назад

      Thank you all guys for the help 💖

    • @ahmadhadwan
      @ahmadhadwan 3 года назад +1

      He talked about it in this video:
      ruclips.net/video/5td9upDbkaU/видео.html

  • @yoramyoram4846
    @yoramyoram4846 3 года назад +8

    tentary is the holy operator

  • @YanVidz
    @YanVidz 3 года назад +4

    when will you talk about rust? It seems like you considered it since a few years ago but what's the status now?

  • @mohammedzaid6634
    @mohammedzaid6634 3 года назад

    hay jacob can you talk about data serialization/encapsulation when sending data over socket. (i want to know how can we send whole struct over socket).

    • @JacobSorber
      @JacobSorber  3 года назад +1

      Probably. I'll add it to the list, and see what I can do.

    • @mohammedzaid6634
      @mohammedzaid6634 3 года назад

      @@JacobSorber thank you jacob

  • @EnkoVlog
    @EnkoVlog 3 года назад +1

    What VSC pluggin displays "==" as crossing "="?

    • @JacobSorber
      @JacobSorber  3 года назад +2

      It's just a font ligature. If you turn ligatures on, and use a font with a ligature for "==", then magic will happen.

    • @EnkoVlog
      @EnkoVlog 3 года назад

      @@JacobSorber thanks!

  • @XenoTravis
    @XenoTravis 3 года назад

    Wait how do you have it auto highlighting like that?

  • @icxcarnie
    @icxcarnie 3 года назад +2

    nested ternaries ftw

  • @privileged6453
    @privileged6453 3 года назад +2

    the forbidden operator!

    • @LinucNerd
      @LinucNerd 3 года назад +5

      goto ternary;

    • @JacobSorber
      @JacobSorber  3 года назад +3

      😂

    • @PflanzenChirurg
      @PflanzenChirurg 3 года назад

      @@JacobSorber i sent you a mail to your edu adress. It could be interesting.

  • @lef1970
    @lef1970 3 года назад +2

    What’s with the funky != operator? I’ve been seeing that in some videos on here and other channels lately … whatever that is it should be dropped.

    • @JacobSorber
      @JacobSorber  3 года назад +2

      It's a font ligature. See my font video for more clarification.

  • @pierreabbat6157
    @pierreabbat6157 3 года назад

    Showing the ternary operator to the gullible can be aukward.