Too few arguments to function (C language Error)

In this article, we are going to learn about an error which occurs in C programming language when we use less argument while calling a function. If we do this compiler will generate an error "Too few arguments to function".
Submitted by IncludeHelp, on May 15, 2018

Too few arguments to function in C language

This error occurs when numbers of actual and formal arguments are different in the program.

Let’s understand first, what actual and formal arguments are?

Actual arguments are the variables, values which are being passed while calling a function and formal arguments are the temporary variables which we declare while defining a function.

Consider the given example:

int sum(int a, int b, int c)
{
	return  (a+b+c);
}

int main()
{
	int x,y,z;
	x = 10; y = 20; z = 30;
	
	printf("sum = %d\n",sum(x, y, z));
	
	return 0;
}

Here, actual arguments are x, y and z. Formal arguments are a, b and c.

When, the error "too few arguments to function is occurred"?

Remember: Number of actual arguments (the arguments which are going to be supplied while calling the function) must equal to number of formal arguments (the arguments which are declared while defining a function).

This error will occur if the number of actual and formal arguments is different.

Consider the above example, and match with these below given calling statements, these statements will generate errors:

printf("sum = %d\n", sum(x, y));
printf("sum = %d\n", sum(x));
printf("sum = %d\n", sum(10, 20));

Example: (by calling functions with less number of arguments)

#include <stdio.h>

int sum(int a, int b, int c)
{
	return  (a+b+c);
}
int main()
{
	int x,y,z;
	x = 10; y = 20; z = 30;
	
	printf("sum = %d\n", sum(x, y));
	printf("sum = %d\n", sum(x));
	printf("sum = %d\n", sum(10, 20));
	
	return 0;
}

Output

prog.c: In function ‘main’:
prog.c:12:23: error: too few arguments to function ‘sum’
  printf("sum = %d\n", sum(x, y));
                       ^~~
prog.c:3:5: note: declared here
 int sum(int a, int b, int c)
     ^~~
prog.c:13:23: error: too few arguments to function ‘sum’
  printf("sum = %d\n", sum(x));
                       ^~~
prog.c:3:5: note: declared here
 int sum(int a, int b, int c)
     ^~~
prog.c:14:23: error: too few arguments to function ‘sum’
  printf("sum = %d\n", sum(10, 20));
                       ^~~
prog.c:3:5: note: declared here
 int sum(int a, int b, int c)
     ^~~
prog.c:9:10: warning: variable ‘z’ set but not used [-Wunused-but-set-variable]
  int x,y,z;
          ^

So, while calling a function, you must check the total number of arguments. So that you can save your time by this type of errors.

Example: (by calling functions with correct number of arguments)

#include <stdio.h>

int sum(int a, int b, int c)
{
	return  (a+b+c);
}
int main()
{
	int x,y,z;
	x = 10; y = 20; z = 30;
	
	printf("sum = %d\n", sum(x, y, z));
	printf("sum = %d\n", sum(x,y,z));
	printf("sum = %d\n", sum(10, 20,30));
	
	return 0;
}

Output

sum = 60
sum = 60
sum = 60



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.