Ishan Ahmed
Ishan Ahmed

Reputation: 105

How to pass different amount of arguments in a function in c

I want to pass different amount of arguments by satisfying condition to the function. But the problem is the arguments have to be the same amount of parameter of a function.In every condtion I have different variables value which I want to pass as arguments. How can avoid this and successfully execute my code.

#include<stdio.h>
float file_w(float root1,float root2, float real,float imag)
{

    FILE *fileWrite;
   fileWrite= fopen("fileNames.txt", "a+");
    if(root2==NULL && real==NULL && imag==NULL){
      fprintf(fileWrite,"%.2f",root1);
      fclose(fileWrite);
   }
}
float file_r()
{
    system("cls");
    printf("\n\n\n\t\t\t\tCalculation History\n\n\t\t\t\t");
    FILE *file;
    char c;
    file=fopen("fileName.txt","r");
    if(file==NULL)
    {
        printf("file not found");
    }
    else
    {
        while(!feof(file))
        {
            c=fgetc(file);
            printf("%c",c);

        }
        fclose(file);
        printf("\n");
        system("Pause");
        main();
    }
}
int main(){
   double a, b, c, discriminant, root1, root2, realPart, imagPart;
    int opt;
    printf("Enter coefficients a, b and c: \n");
    scanf("%lf", &a);
    scanf("%lf",&b);
    scanf("%lf",&c);

    discriminant = b * b - 4 * a * c;

    if (discriminant > 0)
    {
        root1 = (-b + sqrt(discriminant)) / (2 * a);
        root2 = (-b - sqrt(discriminant)) / (2 * a);
        printf("\n\t\t\t\troot1 = %.2lf and root2 = %.2lf\n\n\t\t\t\t", root1, root2);
        file_w(root1,root2);
    }

    else if (discriminant == 0)
    {
        root1 = root2 = -b / (2 * a);
        printf("\n\t\t\t\troot1 = root2 = %.2lf\n\n\t\t\t\t", root1);
        file_w(root1);
    }

    else
    {
        realPart = -b / (2 * a);
        imagPart = sqrt(-discriminant) / (2 * a);
        printf("\n\t\t\t\troot1 = %.2lf + %.2lfi and root2 = %.2f - %.2fi\n\n\t\t\t\t", realPart, imagPart, realPart, imagPart);
        file_w(realPart,imagPart);
    }
return 0;
}

Upvotes: 0

Views: 667

Answers (1)

Nick
Nick

Reputation: 96

Variable Length Argument in C

Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement.

  1. Sum of given numbers.
  2. Minimum of given numbers. and many more.

Variable number of arguments are represented by three dotes (…)

For example:

#include <stdio.h>
#include <stdarg.h>

double average(int num,...) {

   va_list valist;
   double sum = 0.0;
   int i;

   /* initialize valist for num number of arguments */
   va_start(valist, num);

   /* access all the arguments assigned to valist */
   for (i = 0; i < num; i++) {
      sum += va_arg(valist, int);
   }
    
   /* clean memory reserved for valist */
   va_end(valist);

   return sum/num;
}

int main() {
   printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5));
   printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));
}

Upvotes: 1

Related Questions