1

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,

3 Answers 3

2

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 ()
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Well, in old, really old C, this was not necessary, IIRC.
0

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

add void func(); before main

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.