0

Immediately below is the error message I get:

} ^ 1 warning generated. Undefined symbols for architecture x86_64:   "_main", referenced from:
    implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: 
error: linker command failed with exit code 1 (use -v to see invocation)

And here is the code itself:

#include <stdio.h>
#include <stdlib.h>
int isOddEven (int i) {
      for (i=0; i < 100; i++){
        if(i % 2 == 0){
          printf("%d is an even number\n", i);
          return 1;
        }
        else{
          printf("%d is an odd number\n" , i);
          return 0;
        }
      }
}
enzo
  • 9,861
  • 3
  • 15
  • 38

2 Answers2

1

You have not included the main method at all. You have not declared the main method at the top. In c you have to declare the function at the top like a variable first. hence you first have to declare the isOddEven() function at the top.

your code should be something like

#include <stdio.h>
#include <stdlib.h>

void isOddEven();
int main()
{
  isOddEven();
  return 1;
}
void isOddEven() 
{
  for (int i = 0; i < 100; i++)
  {
    if(i % 2 == 0)
    {
      printf("%d is an even number\n", i);
    }
    else
    {
      printf("%d is an odd number\n" , i);
    }
  }
}
enzo
  • 9,861
  • 3
  • 15
  • 38
  • "method" wouldn't be the best way to describe `main` since methods don't exist in C. And you don't "have" to declare functions first before defining, you can just define them before `main`. – mediocrevegetable1 Aug 12 '21 at 04:47
0

You can try below code:

#include <stdio.h>

int isOddEven (int i) {
    if(i % 2 == 0){
        return 1;
    }
    else{
        return 0;
    }
}
int main()
{
    int value = isOddEven(3);  
    if(value == 1){
        printf("Value is even");
    }
    else{
        printf("Value is odd");
    }
    return 0;
}

Here isOddEven returns a int value which you can check in your main function that value is odd or even.