0

I just started learning C and trying to run the following code (given in the book I am using) in Code::Blocks 10.05 compiler for windows.

"error: too many arguments to function add_two_numbers ()"

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

Please advise.

Edited by Narue: Added code tags

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by Aman6o
0

Please advise.

You're compiling as C++. Compile as C instead and the error will go away, but the code is still slightly off in terms of modern correctness. This is more correct:

#include <stdio.h>

/* Full prototype (file scope is conventional) */
int add_two_numbers(int a, int b);

/* Full prototype & definition */
int main(void)
{
    /* Better to initialize rather than assign */
    int var1 = 1;
    int var2 = 53;

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

    /*
        Return is better than exit() (especially
        since you didn't include <stdlib.h>
    */
    return 0;
}

/* No implicit int because it was removed in C99 */
int add_two_numbers(int a, int b)
{
    int c = a + b;
    
    printf("%d\n", c);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.