Easy Tutorial
❮ C Examples Even Odd C Macro Offsetof ❯

C Exercise Example 15

C Language Classic 100 Examples

Title: Use nested conditional operators to complete this task: Students with scores >= 90 are represented by 'A', those with scores between 60-89 are represented by 'B', and those with scores below 60 are represented by 'C'.

Program Analysis: (a>b)?a:b is a basic example of the conditional operator.

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. All rights reserved.
//

#include<stdio.h>
int main()
{
    int score;
    char grade;
    printf("Please enter the score: ");
    scanf("%d",&score);
    grade=(score>=90)?'A':((score>=60)?'B':'C');
    printf("%c\n",grade);
    return 0;
}

The output of the above example is:

Please enter the score: 87
B

C Language Classic 100 Examples

❮ C Examples Even Odd C Macro Offsetof ❯