Hello,

I am trying to write a function such as:

printValues(int nInts, int nFloats) ;

nInts describes the variable number of int values
passed, and nFloats describes the variable number of float
values passed.

Normally I would think of doing something like:

printValues(int num1, int num2, float num3....) ;

But nInts and nFloats needs to describe number of variables so printValues can be called with something like this in main:

printValues(4, 3, 3.3, 56.6) ;

I tried using enums, and unions but am unsure on how to implement them. Here is the code I've been messing with.

Any suggestions would be greatly appreciated.

#include <stdio.h>

union {
	int x; 
	float y; 
	char *z;
} U ;
 
typedef enum {
 
	INT, 
	FLOAT, 
	STRING
 
} Type ;
 
//void printValues(int nInts, int nFloats) ;
 
void printValues(int numI1, int numI2, float numF1, float numF2) ;

int main () {
 
	//printValues(4, 3, 55, 66, 77, 88, 34.5, 23.3, 56.6) ;
	printValues(4, 3, 3.3, 56.6) ;

}
//test to see it print correctly
void printValues(int numI1, int numI2, float numF1, float numF2) {
	
	printf("1: %d 2: %d 3: %f 4: %f \n", numI1, numI2, numF1, numF2) ;
}

/*void printValues(int nInts, int nFloats) {

	Type numInts = INT ;
	Type numFloats = FLOAT ;


}*/

Dani AI

Generated

Two practical ways to do what you want: use a true variadic function (stdarg) as suggested, or use a safer array/pointer interface and pass counts plus pointers. 's approach is fine—just be careful about the common pitfalls with C variadic functions.

Key points if you go variadic:

  • Declare the function with an ellipsis: void printValues(int nInts, int nFloats, ...);.
  • Include <stdarg.h>, call va_start with the last named parameter, and always call va_end.
  • Use the promoted types: float arguments are promoted to double and small integer types to int (so read floats with va_arg(..., double)). Reading a wrong type is undefined behavior.
  • Make sure the counts you pass match what the function reads; there is no runtime type checking. Compile with warnings (e.g., -Wall -Wextra) to catch obvious mistakes.

If you prefer safety and clarity (recommended when you can), pass arrays with counts instead of a ... list. Example pattern:

void print_values(const int *ints, size_t n_ints,
                  const double *flts, size_t n_flts)
{
    size_t i;
    for (i = 0; i < n_ints; ++i)
        printf("i%zu: %d\n", i, ints[i]);
    for (i = 0; i < n_flts; ++i)
        printf("f%zu: %.2f\n", i, flts[i]);
}

/* call example:
int a[] = {10,20,30};
double d[] = {1.5, 2.75};
print_values(a, 3, d, 2);
*/

About the 56.599998 observation (): that is normal binary floating-point inaccuracy plus default printf precision. Use format precision (e.g., %.2f) to print the number the way you expect. The union/enum idea mentioned by can work if you build an array of tagged values (struct with tag + union) and pass that array — but that is more code and complexity than either stdarg or a typed-array API.

Recommended Answers

All 2 Replies

void printValues(int numI1, int numI2, float numF1, float numF2) ;

int main () {
 
	printValues(4, 3, 3.3, 56.6) ;
}
//test to see it print correctly
void printValues(int numI1, int numI2, float numF1, float numF2) {
	
	printf("1: %d 2: %d 3: %f 4: %f \n", numI1, numI2, numF1, numF2) ;
}

What's wrong with this? It seems to work fine without that other stuff.
//edit: except that 56.6 prints out as 56.599998, for me

I have never used a union (never heard of it, actually, except in math.) Though after looking it up it seems like a pretty neat idea! The only time I've used an enum was when I wanted the data enumerated.

>Any suggestions would be greatly appreciated.
I get the impression you want a variable argument list. Something like this:

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

void foo(int ints, int floats, ...)
{
  int sum_int = 0;
  double sum_float = 0;
  va_list args;

  va_start(args, floats);

  /* Get the ints first */
  while (--ints >= 0) {
    sum_int += va_arg(args, int);
  }

  /* Now get the floats */
  while (--floats >= 0) {
    sum_float += va_arg(args, double);
  }

  printf("%-5d%-6.2f\n", sum_int, sum_float);

  va_end(args);
}

int main(void)
{
  foo(1, 2, 5, 1.23, 2.34);
  foo(4, 3, 55, 66, 77, 88, 34.5, 23.3, 56.6);
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.