473,395 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

error: too many arguments to function add_two_numbers

Trying to learn C and doing my practice on the Code::Blocks 10.05. I get this error trying to compile the following simple code given in the book I am using.

#include <stdio.h>

int main()
{
int var1, var2;
int add_two_numbers ();

var1 = 1;
var2 = 53;

add_two_numbers (var1, var2);
add_two_numbers (1, 2);

exit(0);
}
add_two_numbers (int a, int b) /* Add a and b */
{
int c;

c = a + b;
printf ("%d\n", c);
}

Any advise as what am I doing wrong?
Sep 8 '11 #1
3 5541
Exit does not exist in sdtio.h, you will need to include "#include <stdlib.h>"

add_two_numbers does not return int
void add_two_numbers(...)

move your prototype out of the main (also your prototype needs to know what variable types to expect)


I am quite rusty, but maybe this will help.
Take a systematic approach, first compile with calls to 'add_two_numbers'
commented out, if that works then uncomment one call and re-compile. Repeat this process until your program is successful.

Regards

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void add_two_numbers(int,int);
  5.  
  6. int main()
  7. {
  8.    int var1, var2;
  9.    var1 = 1;
  10.    var2 = 53;
  11.    //add_two_numbers (var1, var2);
  12.    //add_two_numbers (1, 2);
  13.    exit(0);
  14. };
  15.  
  16. void add_two_numbers (int a, int b) /* Add a and b */
  17. {
  18.    int c;
  19.    c = a + b;
  20.    printf ("%d\n", c);
  21. };
  22.  
Sep 8 '11 #2
johny10151981
1,059 1GB
kamellion already answered everything

The problem you have made is
you have declare your function like this
Expand|Select|Wrap|Line Numbers
  1. int add_two_numbers ();
  2.  
but you have defined your function like this
Expand|Select|Wrap|Line Numbers
  1. int add_two_numbers (int, int)
  2. {
  3. ...
  4. }
  5.  
and your compiler made that error cause it remember the declaration without variable
Sep 9 '11 #3
Banfa
9,065 Expert Mod 8TB
You are compiling your C code using a C++ compiler. I can tell this because int add_two_numbers(); has different meanings in C and C++ and the error you are getting "too many arguments to function" is produced as a result of that difference in meaning.

Remember in C++ that you have to provide a complete function prototype before you call that function, however in C you don't although it is best practice. It is this requirement difference that has lead to the different meanings.

So the 2 means are (differences highlighted in bold)

C: A function exists called add_two_numbers that returns an int; I am not telling you anything about what parameters it takes.

C++: A function exists called add_two_numbers that returns an int; it takes no parameters.

In C++ the equivalent of int add_two_numbers(void);

In C when it sees the call add_two_numbers (1, 2); it compiles fine because the compiler, correctly, compiles the 2 parameters since it has no information about what parameters the function accepts and it doesn't need that information to compile the function call.

For the same code in C++ the compiler has been informed that the function takes 0 parameters so it produces an error since you have tried to pass 2 parameters into it.


If you are undertaking a course or project in C and writing C code it is generally a mistake to compile using a C++ compiler.
Sep 9 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gav | last post by:
Hi all, Upon trying to run a storedprocedure I get the error: Procedure or function mystoredprocedure has too many arguments specified. I checked out this site: ...
2
by: jn | last post by:
Hi, I'm passing around 30 arguments to a stored procedure, and I got the following error (see below). It works fine if I narrow down to around 25 args. Is there any workaround to bypass this...
3
by: DC Gringo | last post by:
I have a simple function within a web method. It works fine with one parameter, but gives me a compile error when I try to add a second: "Too many arguments to 'Public Function...
4
by: dbuchanan | last post by:
Hello, I have an audit table into which I insert information about the use of the application. This works sometimes and other times fails. I cannot find any reason for it failing. It is always...
16
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is...
0
by: Mike P | last post by:
I am using a SqlDataSource with a GridView and stored proc, and I have specified exactly the same update parameters for the SqlDataSource in exactly the same order as my stored proc. Yet I keep...
2
by: Celtix | last post by:
I am trying to compile a program for school and i get a error message back. "In function ... too many arguments to function. What does this mean?
0
by: Kil | last post by:
Hello everyone, I would like to use the Alternative PHP Cache (APC, http://pecl.php.net/package/APC). I am using Apache 2.2.3 (with worker MPM), PHP 4.4.4 - all self-builds under Debian...
0
by: David Lozzi | last post by:
Howdy, ASP.Net 2.0 using VB on SQL 2005 This is a two fold issue. I have a DetailsView control which users can insert or edit items. Editing works great. Insert works great however I need...
2
by: sush1512 | last post by:
Hi all , My stored proc is as follows if @Code Is Null BEGIN SELECT * FROM TCode (NOLOCK) END Else BEGIN
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.