ヲヲヲ
ヲヲヲ

Reputation: 61

Compiling C code errors LNK2005 and LNK1169 in Visual Studio 2017

I have problem when compiling the code that calculate the total sum of positive integer. What puzzled me is I managed to compile and run the code successfully on Online C Compiler (https://www.onlinegdb.com/online_c_compiler) but get LNK2005 and LNK1169 error on VS2017. How to fix it?

//Calculate total sum of positive integer.

#include <stdio.h>

int sum(int n);

int main(void) {
    int n;
    printf("Enter positive value of integer: ");
    scanf("%d", &n);
    printf("\nTotal value for %d is = %d\n", n, sum(n));
    return (0);
}

int sum(int n) {
    if (n == 0) return 0;
    else return (n*(n+1)/2);
}

By the way, kindly ignore the scanf warning on VS2017, I will change it back to scanf_s later.

Upvotes: 1

Views: 147

Answers (2)

NutCracker
NutCracker

Reputation: 12263

This is a common error that occurs if you have more than one source file that contains main function.

Upvotes: 4

ヲヲヲ
ヲヲヲ

Reputation: 61

The problem occured because there are 2 source files inside my VS2017 solution. Removing one will allow the program to compile successfully.

Upvotes: 1

Related Questions