conditional-operator

C Language Operators [closed]

不打扰是莪最后的温柔 提交于 2019-12-14 03:30:07
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . #include <stdio.h> int main() { int a=-1?2:5 + 8?4:5; printf("%d\n",a); return 0; } The output of above program is 2. But why ? Please explain 回答1: Write human-readable and understandable code. ( Atleast, try to... ) int a=-1?2:5 + 8?4:5; is the same as int a = (-1) ? 2 : ( 5 + ( 8 ? 4 : 5) );

Conditional-Operator in Constant Expression

末鹿安然 提交于 2019-12-14 01:23:50
问题 I tried the following code snippet with MSVC 10, where it works fine. enum { FOO = (sizeof(void*) == 8 ? 10 : 20) }; int main() { return FOO; } What I would like to know is: Does the C++ Standard (preferably C++98) allow me to use the conditional-operator in a constant expression when all operands are constant expressions, or is this a Microsoft quirk/extension? 回答1: This is perfectly valid and sensible standard C++. The ternary conditional operator forms an expression , and the expression is

What does this syntax ( page = $page ? $page : 'default' ) in PHP mean?

十年热恋 提交于 2019-12-14 01:19:33
问题 I'm new to PHP. I came across this syntax in WordPress. What does the last line of that code do? $page = $_SERVER['REQUEST_URI']; $page = str_replace("/","",$page); $page = str_replace(".php","",$page); $page = $page ? $page : 'default' 回答1: It's an example of the conditional operator in PHP. It's the shorthand version of: if (something is true ) { Do this } else { Do that } See Using If/Else Ternary Operators http://php.net/manual/en/language.operators.comparison.php . 回答2: That's the

lvalue required as left operand of assignment in conditional operator [duplicate]

天大地大妈咪最大 提交于 2019-12-14 00:12:53
问题 This question already has answers here : Error: lvalue required in this simple C code? (Ternary with assignment?) (4 answers) Closed 6 years ago . #include <stdio.h> int main() { int a = 1, b; a ? b = 3 : b = 4; printf("%d, %d", a, b); return 0; } [user@localhost programs]$ gcc -Wall vol.c vol.c: In function ‘main’: vol.c:5:16: error: lvalue required as left operand of assignment a ? b = 3 : b = 4; ^ I have given lvalue as b then why gcc is showing error and how to fix it? 回答1: It has to do

Test if two variable are empty at the same time

你离开我真会死。 提交于 2019-12-13 14:25:56
问题 I would like to test both variable in the same if condition. Currently I am using [[ $var ]] to test one but the same for two variable do not work. In order to do that I tried : if [[ &var1 && &var2 ]]; then or if [[ &ipAddress ]] && [[ &bcastAddress ]]; then Is there a limitation ? 回答1: You can use this combined test: [[ -z "${var1}${var2}" ]] OR separate tests in same if condition: [[ -z "$var1" && -z "$var2" ]] 回答2: I often use arithmetic for these sorts of things. For example: case "$(((!

Could someone explain ternary operators in plain English or pseudocode?

夙愿已清 提交于 2019-12-13 00:44:53
问题 I don't understand the syntax used in the following lines, except that it follows a basic structure of what seems to be called a ternary operator. string path = args == null || args.Length == 0 ? @"C:\GENERIC\SYSTEM\PATH" : args[1]; I'm new to this syntax. Would someone help me translate it into real English (or pseudocode), much in the way an if statement can be turned into "if this then that"? EDIT: Thank you everyone for your answers, you've all been extremely helpful. Unfortunately I can

Replace conditional operator with if/else automatically?

痞子三分冷 提交于 2019-12-12 15:53:04
问题 A specific JS parser (not in my control) does not understand nested conditional operator syntax like this: return num === 1 ? condition ? condition : something : something; Hence, I would like to replace all conditional operators (simple and nested) in a file with if/else blocks . How do I go about it? (A regexp for Textmate or similar would be helpful.) 回答1: How do I go about it? (A regexp for Textmate or similar would be helpful. I don't think this is possible with regular expressions - you

Perl ternary conditional operator

北城以北 提交于 2019-12-12 12:32:26
问题 I'm trying to write more efficient code in my scripts and have been implementing ternary conditional operators on occasion. I can't understand why I am getting an additional result when using a ternary conditional operator in a loop: #!/usr/bin/perl use strict; use warnings; my @array = ('Serial = "123"', 'Serial = "456"', 'Serial = "789"'); my ($test1,$test2); foreach my $a (@array){ !$test1 ? $test1 = $a : $test1 .= " AND " . $a; } foreach my $b (@array){ if (!$test2) { $test2 = $b } else {

Python conditional operator 'if else' not equal 'and or' [duplicate]

孤街醉人 提交于 2019-12-12 12:17:42
问题 This question already has answers here : Does Python have a ternary conditional operator? (23 answers) Closed 4 years ago . I think below two function result will be same, but it is not. def fib2(n): return n and n < 2 or fib2(n-1) + fib2(n-2) def fib3(m): return m if m < 2 else fib3(m-1) + fib3(m-2) When argument value is 4, fib2's output is 7, fib3's output is 3. Why this situation happen? I don't know about this. My Python version is 2.7.9 and os is osX 10.11.1 回答1: I tried to be more

Ternary operator in C vs C++ [duplicate]

痞子三分冷 提交于 2019-12-12 07:50:37
问题 This question already has answers here : Errors using ternary operator in c (5 answers) Closed 4 years ago . There are a lot of differences between C and C++ and came to stuck on one of them The same code gives an error in C while just executes fine in C++ Please explain the reason int main(void) { int a=10,b; a>=5?b=100:b=200; } The above code gives an error in C stating lvalue required while the same code compiles fine in C++ 回答1: Have a look at the operator precedence. Without an explicit