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

The Ternary Operator (examples in C and C++) // The ternary operator confuses a lot of beginning programmers. This video will hopefully sort things out for you.

Welcome to my channel. I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I’m a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:
https://www.jacobsorber.com
https://people.cs.clemson.edu/~jsorber/
http://persist.cs.clemson.edu/

To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon — [https://www.patreon.com/jacobsorber]

Source code is also available to Patreon supporters. — [https://jsorber-youtube-source.herokuapp.com/]
Note: I haven’t posted ALL of the code from ALL of my videos yet. The site contains code from my recent tutorials (I wasn’t quite as organized in the pre-2019 past). I will add examples from older videos as I dig them up.

source by Jacob Sorber

go programming language

Mourad ELGORMA

Fondateur de summarynetworks, passionné des nouvelles technologies et des métiers de Réseautique , Master en réseaux et système de télécommunications. ,j’ai affaire à Pascal, Delphi, Java, MATLAB, php …Connaissance du protocole TCP / IP, des applications Ethernet, des WLAN …Planification, installation et dépannage de problèmes de réseau informatique……Installez, configurez et dépannez les périphériques Cisco IOS. Surveillez les performances du réseau et isolez les défaillances du réseau. VLANs, protocoles de routage (RIPv2, EIGRP, OSPF.)…..Manipuler des systèmes embarqués (matériel et logiciel ex: Beaglebone Black)…Linux (Ubuntu, kali, serveur Mandriva Fedora, …). Microsoft (Windows, Windows Server 2003). ……Paquet tracer, GNS3, VMware Workstation, Virtual Box, Filezilla (client / serveur), EasyPhp, serveur Wamp,Le système de gestion WORDPRESS………Installation des caméras de surveillance ( technologie hikvision DVR………..). ,

21 réflexions sur “The Ternary Operator (examples in C and C++)

  • juillet 22, 2021 à 2:27
    Permalien

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

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    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.

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    Thia channel is a Youtube 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 😁.

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    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

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    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.n", num1, num2, num1 > num2 ? num1 : num2);

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

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

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    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.

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

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

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    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.

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

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

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    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)

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

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

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    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.

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    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.

    Répondre
  • juillet 22, 2021 à 2:27
    Permalien

    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

    Répondre

Laisser un commentaire