0

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;
}

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
Ishan Ahmed
  • 105
  • 11
  • 1
    I removed the [tag:c++11] tag since your question seems to be about C, and C++ is quite a different language. – Nate Eldredge Apr 13 '21 at 05:11
  • 1
    To pass differnet count of arguments, use `float file_w(float root1,...)` and call `file_w(1.0)`, `file_w(1.0, 2.0)`, or `file_w(1.0, 2.0,3.0, 4.0)`.. What remains is how to you want to convey the count of arguments. – chux - Reinstate Monica Apr 13 '21 at 05:12
  • 1
    This isn't a good solution for your problem. It is possible to have functions which take a variable number of arguments, but the function is not told how many arguments were actually passed. So if the first argument is, say, `3`, you have no way of knowing whether it's meant to be `3` or `3+4i`, and if you guess wrong you'll cause undefined behavior (likely using garbage values). – Nate Eldredge Apr 13 '21 at 05:12
  • **The usual practice is to avoid [variadic functions](https://en.cppreference.com/w/c/variadic) in C, because they are not typesafe**. If you absolutely need them consider using `va_start`, `va_arg`, `va_end` as documented in [stdarg(3)](https://man7.org/linux/man-pages/man3/stdarg.3.html) – Basile Starynkevitch Apr 13 '21 at 05:13
  • `while(!feof(file))` --> [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/2410359) – chux - Reinstate Monica Apr 13 '21 at 05:15

1 Answers1

1

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));
}
Nick
  • 96
  • 5