HP C A.06.05 Reference Manual
Expressions and Operators
Conditional Expression Operator (?:)
Chapter 5 105
Example
/* Program name is "conditional_exp_op_example".
This program uses the conditional expression to
see if the user wants to continue adding
numbers. */
#include <stdio.h>
int main(void)
{
int a, b, c, d, again, total;
char answer;
printf("\n");
again = 1;
while (again)
{
printf("Enter four numbers separated by spaces that\n");
printf("you want added together: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
fflush(stdin);
total = a + b + c + d;
printf("\nThe total is: %d\n", total);
printf("Do you want to continue ? ");
scanf("%c", &answer);
again = (answer == 'y' || answer == 'Y') ? 1 : 0;
} /* end while */
}
If you execute this program, you get the following output:
Enter four numbers separated by spaces that
you want added together: 20 30 40 50
The total is: 140
Do you want to continue ? y
Enter four numbers separated by spaces that
you want added together: 1 2 3 4
The total is: 10
Do you want to continue ? n