devaditya
devaditya

Reputation: 331

Compiling C program using XCode

i am trying to run a very simple C program using XCode which is typed below

1)   #include <stdio.h>
2)   int main ()
3)   {
4)     printf("Hello, World!\n");
5)     func();
6)     return 0;
7)   }
8)   void func()
9)   {
10)    printf("xxxx");
11)  }

In line number 5 i am getting warning "Implicit declaration of func is invalid in c99" and in line number 8 i am getting an error "conflicting types for func"

please advise Thanks,

Upvotes: 1

Views: 3111

Answers (3)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

Well, the error messages tell you exactly what is wrong. Functions being used must be declared first, either in the same source code unit, or in a header file.

If func() is not declared yet, the compiler assumes an int result.

The first error says you should declare func() before using it:

void func(void);

int main()
{
    etc...    

The second error tells you that func() does not return int after all. If you had declared func() first, both errors wouldn't have happened.

Upvotes: 1

MByD
MByD

Reputation: 137272

You need to declare func(); before using it (in main), otherwise it is declared as a function that returns int, and when the compiler gets to line 8, it sees a different declaration of the same function that returns void.

#include <stdio.h>
void func(void);
int main ()

Upvotes: 2

Hayri Uğur Koltuk
Hayri Uğur Koltuk

Reputation: 3020

you called func() before declaring or defining, that's why.

add void func(); before main

Upvotes: 0

Related Questions