Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programming language

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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.