-2

As you can see in C language I am trying to calcualte average . I want to know the logic about why after putting (float)(a,b,c) in function float average right below int main syntax it taking a,b,c as float instead of int

#include <stdio.h>
  
float average(int a, int  b, int c);


int main(){
   int a , b,c;
  // I have taken a,b,c as int ; 

   printf("Enter the value of a\n");
   scanf("%d",&a);
   printf("Enter the value of b\n");
   scanf("%d",&b);
   printf("Enter the value of c \n");
   scanf("%d",&c);
   printf(" The value of average is %f",average(a,b,c));
    
   return 0;
}

float average(int  a, int  b, int c){
   float result;
   result = (float)(a+b+c)/3;   HERE I want to know its logic 
   return result;
}  
TruthSeeker
  • 1,412
  • 11
  • 20
  • 1
    `(float)` is a type cast. Is explicitely tells the compiler to treat the value as `float` instead of what is iswithout that cast. – Gerhardh May 23 '22 at 10:52
  • 1
    There is no `(float)(a,b,c)`, the code is `(float)(a+b+c)`. Between the title , the body and the code, you have written three different expressions! Accuracy is key. It would be simpler to write the expression as `(a+b+c) / 3.0f`. The cast to `float` is to force floating point rather then integer division, but that is more simply done by making the right-hand operand `float`. – Clifford May 23 '22 at 11:04

0 Answers0