1

I am pretty new to coding. I used a simple code:

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

int main()
{
   Sayhi();
   return 0;
}

void Sayhi()
{ 
   printf("hi");
}

So when I compile the code it says function "sayhi" was not declared in this scope. I even tried a different code which used "void" as a function but it didn't work.

Jens
  • 69,818
  • 15
  • 125
  • 179
JOJO100
  • 11
  • 2
  • 1
    Put the ' Sayhi()' function above main(). Also, please format code as code:( – Martin James Jul 03 '22 at 19:08
  • You forgot to prototype `Sayhi()` – Irelia Jul 03 '22 at 19:14
  • 2
    Does this answer your question? [What is the difference between a definition and a declaration?](https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration) – Oka Jul 03 '22 at 19:14
  • 1
    @xing That's not a prototype, it's an old-style function declaration. The OP should use `void sayHi(void);` Oh, and `int main(void) {...}` – Jens Jul 03 '22 at 19:21
  • 1
    @JOJO100 Not related to your problem, but on an unrelated note: get in the habit of typing `printf("hi\n");`. You usually want a newline at the end of every line. – Steve Summit Jul 03 '22 at 19:27
  • Assuming it is not referenced from out-of-file, `static void Sayhi(void)`, would be better, `static` because, among other things, 1) it doesn't pollute the global namespace since it is only valid in this compilation unit, 2) it is easier for your compiler to optimize, 3) it is immediately apparent when reading the code that it is a private function. – Neil Jul 03 '22 at 20:59
  • @JOJO100: hopefully your question is answered/problem resolved. Please let us know! – paulsm4 Jul 04 '22 at 02:57
  • yea sorry for the late update it was a simply a syntax error still new to programming – JOJO100 Sep 22 '22 at 18:36

1 Answers1

2
  • This should work - simply declare and define "Sayhi()" before you use it:

    #include <stdio.h>
    #include <stdlib.h>
    
    void Sayhi();
    { 
       printf("hi");
    }
    
    int main()
    {
       Sayhi();
       return 0;
    }
    
  • A "better" approach would be to create a prototype for "Sayhi()":

    #include <stdio.h>
    #include <stdlib.h>
    
    void Sayhi(void);
    
    int main()
    {
       Sayhi();
       return 0;
    }
    
    void Sayhi();
    { 
        printf("hi");
    }
    

Q: So what's a "prototype"?

https://www.programiz.com/c-programming/c-user-defined-functions

A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body.

A function prototype gives information to the compiler that the function may later be used in the program.

Prototypes should always list the function's parameters. If no parameters, it should list "void".

The value of prototypes shines as your application increases in size and complexity. You'll want to move code OUT of "main()" and into separate .c source files (e.g. "mycomponent.c") and corresponding header files (e.g. "myheader.h").

One additional note: you should always NAME the variables in your prototypes (e.g. void myfunc(int i);.

Q: Do you understand why you were getting the compile error (the function needed to be declared somehow before you used it), and how you can fix it?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 4
    `void Sayhi();` is not a prototype, it's a dangerous declaration that allows passing arguments. `void Sayhi(void);` is a prototype. – aschepler Jul 03 '22 at 19:35