Solution for Exercise 8-21
ex0821
#include <stdio.h> int main() { int a,b,larger; printf("Enter value A: "); scanf("%d",&a); printf("Enter different value B: "); scanf("%d",&b); larger = (a > b) ? a : b; printf("Value %d is larger.\n",larger); return(0); }
Output
Enter value a: 67
Enter different value B: 4
Value 67 is larger.
Notes
* Some programmers prefer ?:
because it's cryptic, yet it fits compactly on a line.
* Fortunately, only a few situations exist where the ternary structure is really the best way to make a decision. An example can be found in the book in Chapter 17.
* If the ?:
thing frustrates you, then you can generally construct a handy if-else structure to replace it. Even so, be aware that the ?:
thing is one of those aspects of the C language that's heavily borrowed by other programming languages. It may come back to haunt you!
Copyright © 1997-2024 by QPBC.
All rights reserved