I just started learning 'C'. I keep getting "collect2.exe: error: ld returned 1 exit status" when I try to run the following code on the terminal

Viewed 40

I wrote this code to find out the average of any 3 numbers. Please help me out. I'd like to know where it went wrong. I wrote the same code in a new file and that was executed without any issues.

This is what I keep getting:

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16' collect2.exe: error: ld returned 1 exit status

#include <stdio.h>
//average of three numbers

int main() {
  int n1, n2, n3;
  printf("enter n1");
  scanf("%d", &n1);

  printf("enter n2");
  scanf("%d", &n2);

  printf("enter n3");
  scanf("%d", &n3);

  int average = n1/3+n2/3+n3/3;
  printf("average is : %d", average);
  return 0;
}

1 Answers

You need to write

int average = ( n1 + n2 + n3 ) / 3;

instead of

int average = n1/3+n2/3+n3/3;

Also you could write using a float result

double average = ( n1 + n2 + n3 ) / 3.0;

and then

printf("average is : %.2f\n", average);

Pay attention to that for readability it is better to add a space after the output of a call pf printf like

printf("enter n1 ");
               ^^^^

In general you should check whether a call of scanf was successful.

As for the run-time error then the reason can be of running the program that has a compiler or linker error.

Related