🌐
C For Dummies
c-for-dummies.com › blog
The Elvis Operator | C For Dummies Blog
June 10, 2017 - A new operator was added to the C language in the last revision, one that I don’t cover in my books. (I’m not sure how that happened.) Anyway, it’s the Elvis operator. Unless you’re a fan of the ternary operator, you’ll probably never use it. As a review, the ternary operator is a ...
binary operator in computer programming
Elvis operator - Wikipedia
In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that evaluates its first operand and returns it if its value is logically true (according to a … Wikipedia
Factsheet
Named after Elvis Presley
Factsheet
Named after Elvis Presley
🌐
Wikipedia
en.wikipedia.org › wiki › Elvis_operator
Elvis operator - Wikipedia
3 weeks ago - In several languages, such as Common Lisp, Clojure, Lua, Object Pascal, Perl, Python, Ruby, and JavaScript, there is no need for the Elvis operator, because the language's logical disjunction operator (typically || or or) is short-circuiting and returns its first operand if it would evaluate to a truthy value, and otherwise its second operand, which may be a truthy or falsy value (rather than a Boolean true or false value, such as in C and C++).
Discussions

Why is the Elvis operator not more common? - Programming Language Design and Implementation Stack Exchange
In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that returns its first operand if that operand evaluates to a true value, and otherwise evaluates and returns its second operand. More on langdev.stackexchange.com
🌐 langdev.stackexchange.com
May 17, 2023
Elvis operator
Hi all is there a better way to write this with Elvis operator? params = rc.keyExists('areaId') ? params.append( { 'areaId': rc.areaId } ) : params; I expected, like in C: rc.keyExists('areaId') ?? params.append( { 'areaId': rc.areaId } ) Removing the left expression, if the first condition ... More on dev.lucee.org
🌐 dev.lucee.org
1
July 25, 2023
Elvis operator for Rust - language design - Rust Internals
I usually shy away from new syntax discussions, but this bit seems interesting to me. A similar idea was discussed previously, but I think that post missed a rather crucial detail. Summary Add ?: (elvis) operator to Rus… More on internals.rust-lang.org
🌐 internals.rust-lang.org
34
November 6, 2019
What does ? do here?
Can someone point me to the Language Reference that describes what the ? is doing · Why is it called the Elvis operator? Because if you look at it sideways and squint a lot, you may see Elvis Presley More on forum.arduino.cc
🌐 forum.arduino.cc
0
November 27, 2017
🌐
Reddit
reddit.com › r/cpp › elvis operator ?: and other non-standard syntactic sugars
r/cpp on Reddit: Elvis Operator ?: and other non-standard syntactic sugars
October 12, 2021 -

So today I discovered that the elvis operator (for null coalescing) works in clang, and presumably in gcc as well (since it's apparently a gcc extension).

I'm not sure what other compilers this works in, but given that it's non-standard it's probably not a good idea to get too used to it, but what's wild to me is that it's not just a syntactic sugar - compared to the ternary operator it only evaluates the x from (x ?: y) once.

How widespread is compiler support for this operator? I've never had to use it in c++, but I've seen a lot of code using it in JS (which I've had to use a lot recently), so I imagine it's a useful construct.

Also, any other similar operators that's not well known?

Top answer
1 of 5
16
That operator has little to do with NULL specifically. int and NULL, for example, have nothing to do with each other, yet both are contextually-convertible to bool. As for compiler support, MSVC doesn't support it. You decide if that's relevant to you.
2 of 5
12
It has been said that the reason GCC grew so many extensions is so that C could be useful in projects that refuse to use C++ for political reasons. But there are plenty that are useful in their own right. By far, the most common extensions: the various __builtin_foo()s (and I don't mean the ones that are just for optimizing/implementing standard library functions) the various __attribute__((foo))s. There are a lot of them, far more than the handful of standard C++ [[attribute]]s. Statement expressions are incredibly useful. Among other things, it makes it possible to implement the Rust-style TRY macro: // expr is usually a function call # define TRY(expr) \ ({ \ auto _tmp = expr; \ if (_tmp == -1) \ return -1; \ _tmp; \ }) (since there is no standard Result type, you do have to implement multiple versions, for each possible way of representing errors. This one is useful for the convention that is common in the C standard library, with details in errno) Zero-sized arrays are well-supported on all compilers I've tested. They're not quite as necessary now that we have [[no_unique_address]], but I still find they make the code a lot more elegant. Taking the address of a label (then using it for a computed goto) can be a major performance win in a few niche use cases. __extension__ and #pragma GCC diagnostic ... (remember that the _Pragma() form exists and is usable from macros) are useful for writing warning-clean headers. There are more, but this is what comes to mind. Possibly some of the ones I'm omitting are so instinctive that I don't even realize it's an extension.
🌐
Lucee Dev
dev.lucee.org › support
Elvis operator - support - Lucee Dev
July 25, 2023 - Hi all is there a better way to write this with Elvis operator? params = rc.keyExists('areaId') ? params.append( { 'areaId': rc.areaId } ) : params; I expected, like in C: rc.keyExists('areaId') ?? params.append( { 'areaId': rc.areaId } ) Removing the left expression, if the first condition is false, nothing should happen.
🌐
underoot
underoot.dev › blog › 2023 › 06 › 22 › elvis-operator
Elvis Operator
According to the Wikipedia page, it was GNU C/C++. You maybe know about the ternary operator. For example in the following code fragment on C++ you can choose b if a does have some empty value like 0 or '' or c otherwise: ... But why the Elvis operator is called so?
🌐
Spring
docs.spring.io › spring-framework › reference › core › expressions › language-ref › operator-elvis.html
The Elvis Operator :: Spring Framework
ExpressionParser parser = new SpelExpressionParser(); EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build(); Inventor tesla = new Inventor("Nikola Tesla", "Serbian"); String name = parser.parseExpression("name ?: 'Elvis Presley'").getValue(context, tesla, String.class); System.out.println(name); // Nikola Tesla tesla.setName(""); name = parser.parseExpression("name ?: 'Elvis Presley'").getValue(context, tesla, String.class); System.out.println(name); // Elvis Presley
Find elsewhere
🌐
Better Programming
betterprogramming.pub › did-you-know-theres-an-elvis-operator-1406cb364929
Did You Know There's an Elvis Operator? | by Jonathan Hsu
November 18, 2019 - Falling in the class of binary assignment operators — meaning it takes two operands used for assignment — the Elvis operator is a “logical or” for the purpose of assignment to a variable (or constant).
🌐
Christiannagel
csharp.christiannagel.com › 2016 › 06 › 17 › nullconditionaloperator
C# 6 – Null-Conditional Operator
June 18, 2016 - Explaining the null conditional operator (also known null propagation operator or as Elvis operator). This operator allows reducing the code by simplifying null checks.
🌐
W3Schools
w3schools.com › c › c_conditions_short_hand.php
C Short Hand If ... Else (Ternary Operator)
The ternary operator returns a value based on a condition: if the condition is true, it returns the first value; otherwise, it returns the second value.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › conditional-or-ternary-operator-in-c
Conditional or Ternary Operator (?:) in C - GeeksforGeeks
July 12, 2025 - The conditional operator in C is kind of similar to the if-else statement as it follows the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
🌐
Rust Internals
internals.rust-lang.org › language design
Elvis operator for Rust - language design - Rust Internals
November 6, 2019 - I usually shy away from new syntax discussions, but this bit seems interesting to me. A similar idea was discussed previously, but I think that post missed a rather crucial detail. Summary Add ?: (elvis) operator to Rust, with the following desugaring semantics: $lhs ?: $rhs => match Try::into_result($lhs) { Ok(it) => it, Err(_) => $rhs } ?: is similar to unwrap_or_else, but is more powerful, as $rhs can contain control-flow expressions like return, break, continue or even ?
🌐
Arduino Forum
forum.arduino.cc › projects › programming
What does ? do here? - Programming - Arduino Forum
November 27, 2017 - Can someone point me to the Language Reference that describes what the ? is doing · Why is it called the Elvis operator? Because if you look at it sideways and squint a lot, you may see Elvis Presley
🌐
LinkedIn
linkedin.com › pulse › code-smell-212-elvis-operator-maximiliano-contieri
Code Smell 212 - Elvis Operator
February 22, 2024 - It is a shorthand operator used in some programming languages to simplify null-checking. The Elvis operator takes the form of ?.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › operators › conditional-operator
?: operator - the ternary conditional operator - C# reference | Microsoft Learn
January 24, 2026 - The condition expression must evaluate to true or false. If condition evaluates to true, the consequent expression is evaluated, and its result becomes the result of the operation.
🌐
Swift Forums
forums.swift.org › evolution › discussion
Elvis Operator? - Discussion - Swift Forums
December 7, 2017 - I am wondering what people think of the idea of an actual elvis operator? Right now we have a null coalescing operator which is nice for optionals. This however does not work with non-optional Bools. The reason I think this would be handy is that you could set a value of a Bool using functions ...