Skip to study material
← Question index

QUESTION 75 · Expressions and conversions

Why cast before integer division when a fractional result is needed?

2 marks

Answer

Cast an operand before division to obtain a fractional result from integer values. Casting after integer division cannot restore the discarded fraction.

  • For total = 7 and count = 2, (double)total / count gives 3.5.
  • (double)(total / count) gives 3.0 because 7 / 2 first gives integer 3.

QUESTION 76 · Expressions and conversions

Explain explicit type casting and distinguish casting before and after division.

5 marks

Answer in brief

Explicit type conversion uses a cast written by the programmer. Its syntax is (type) expression.

Purpose of a cast

A cast requests a conversion of an expression value to a specified type. For example, (double)total produces a double value from total. It does not change the declared type of total. Casts are useful when a particular arithmetic type is needed.

Cast before division

Let int total = 7 and count = 2;. In (double)total / count, total becomes 7.0 for the calculation. The division uses floating-point arithmetic and gives 3.5. This is useful when calculating an average from integer totals. The count must be non-zero.

Cast after division

In (double)(total / count), the integer division is performed first. 7 / 2 gives 3. Casting that result produces 3.0. The discarded fraction cannot be restored by a later cast. This is why the position of a cast matters.

Important points

  1. Original variable: A cast produces a converted value; total is still an int containing 7.
  2. Possible loss: Casting 3.8 to int gives 3 when representable.
  3. Range: A cast does not make every value fit the target type. Check the supported range.
  4. Clear purpose: Use a cast when the conversion is intended and understood. It should not be used merely to hide a compiler warning.

Reference table

↔ Scroll horizontally on a small screen
Casting around integer division
ExpressionFirst operationFinal double value
(double)total / countConvert 7 to 7.0; perform floating division3.5
(double)(total / count)Perform integer division 7 / 2 = 3; then convert3.0

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
    int total = 7, count = 2;
    double before = (double)total / count;
    double after = (double)(total / count);
    printf("cast before division = %.1f\n", before);
    printf("cast after division = %.1f\n", after);
    printf("total remains int with value %d\n", total);
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

cast before division = 3.5
cast after division = 3.0
total remains int with value 7

Key points

  • Casting a value does not redeclare its variable.
  • Place a cast before the operation whose arithmetic type must change.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • July 2021 · Q2(a) · 5 marks

    State the purpose of type casting and type conversion.

    Paper P04 · page 1
View all matching exam references →