QB64 Phoenix Edition
Using the tenary operator in C with 3 numbers - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Utilities (https://qb64phoenix.com/forum/forumdisplay.php?fid=8)
+---- Thread: Using the tenary operator in C with 3 numbers (/showthread.php?tid=2916)

Pages: 1 2


Using the tenary operator in C with 3 numbers - Kernelpanic - 08-08-2024

The tenary operator was discussed here for some time ago, a bit complicated in Basic. It is quite useful for simple comparisons.

The program uses the tenary operator in C with a triple comparison. It also works with four numbers, but that gets a bit confusing. If one only want to compare two numbers, simply enter 0 for the third number. -- Both files in the same directory.

Code: (Select All)

'Aufruf des tenaeren Operators in C - 8. Aug. 2024

Option _Explicit

Declare Library "D:\Lab\QuickBasic64\Extern-nicht-Basic\Basic-ruft-C\btenaerDreifach"
  Function btenaerDreifach& (ByVal Zahl1 As Long, Byval Zahl2 As Long, Byval Zahl3 As Long)
End Declare

Dim As Long zahl1, zahl2, zahl3

Locate 3, 3
Print "Dreifachen tenaeren Operator in C aufrufen"
Locate 4, 3
Print "=========================================="

Locate 6, 3
Input "Zahl 1: ", zahl1

Locate 7, 3
Input "Zahl 2: ", zahl2

Locate 8, 3
Input "Zahl 3: ", zahl3

Locate 10, 3
Print Using "Die groesste Zahl ist: ####"; btenaerDreifach&(zahl1&, zahl2&, zahl3&)

End

Save the C program as btenaerDreifach.h
Code: (Select All)

//Dreifachen tenaeren Operator von Basic aufrufen - 7. Aug. 2024

#include <stdio.h>
#include <stdlib.h>

long btenaerDreifach(long zahl1, long zahl2, long zahl3)
{
long max;

max = (zahl1 > zahl2 ? ((zahl1 > zahl3) ? zahl1 : zahl3) : zahl2);

return(max);
}



RE: Using the tenary operator in C with 3 numbers - Pete - 08-08-2024

For those of you who missed out watching reruns of Hogan's Heroes, the tenary operator is basically saying...

if a then b otherwise c 

Which looks to me like the BASIC equivalent of our if/then/else statements.

But then again, I could be channeling Sergent Schultz, in which case, "I know nothing, nothing!"

Pete Big Grin


RE: Using the tenary operator in C with 3 numbers - Jack - 08-08-2024

Pete
the difference is that you can assign the ternary expression to a variable, you can basically do the same with if then else, but sometimes you can simplify things by using the ternary expression, but I confess that the ternary operator looks weird unless you use it a lot


RE: Using the tenary operator in C with 3 numbers - Pete - 08-08-2024

I look at it as shorthand...

C=(A>B)?A:B; 

meaning:

If A > B Then C = A Else C = B

Pete


RE: Using the tenary operator in C with 3 numbers - Kernelpanic - 08-08-2024

(08-08-2024, 08:53 PM)Pete Wrote: I kook at it as shorthand...

C=(A>B)?A:B; 

meaning:

If A > B Then C = A Else C = B

Pete
I do not think so!

Code: (Select All)

a = 5
b = 4
c = 7

If a > b Then
  c = a
Else
  c = b
End If

Print c

There c would have to be the largest number, right?  Huh


RE: Using the tenary operator in C with 3 numbers - Pete - 08-09-2024

You can't assign c.

Pete


RE: Using the tenary operator in C with 3 numbers - SMcNeill - 08-09-2024

(08-09-2024, 01:05 AM)Pete Wrote: You can't assign c.

Pete

Must be under this new, modern grading system.  A, B, D, E, F  ??


RE: Using the tenary operator in C with 3 numbers - Pete - 08-09-2024

This one was fun...

Code: (Select All)
Age = 30

Beverage = Age >= 21 ? "Beer" : "Juice";

Results: Beer

So you don't assign beverage, it is determined by the assigned value of age.

Pete


RE: Using the tenary operator in C with 3 numbers - hsiangch_ong - 08-10-2024

https://en.wikipedia.org/wiki/Ternary

spell it right.

(not being a jerk. but i thought the word started with "ter" which got me to look it up.)

https://en.wikipedia.org/wiki/Ternary_conditional_operator


RE: Using the tenary operator in C with 3 numbers - Pete - 08-10-2024

(08-10-2024, 04:39 PM)hsiangch_ong Wrote: https://en.wikipedia.org/wiki/Ternary
spell it right.
(not being a jerk. but i thought the word started with "ter" which got me to look it up.)
https://en.wikipedia.org/wiki/Ternary_conditional_operator

I think the Kernel was using the 8th instance of the function as in...

I'm tenary the eighth I am
Tenary the 8th I am I am
I got called from the mainframe next door
It's been booted seven times before
And every call was a tenary, it couldn't call a driver or a cam
I'm the eighth old function tenary
Tenary the eighth I am

Pete Big Grin