6

I'm getting an error. Here's my info from Terminal:

make: *** [tests/myFunction] Error 1
~/workspace/ $ make ./tests/myFunction
clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    tests/myFunction.c  -lcrypt -lcs50 -lm -o tests/myFunction
tests/myFunction.c:7:5: error: implicit declaration of function 'PrintName' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    PrintName(s);
    ^

The code I used is:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    string s = get_string();
    PrintName(s);
}

void PrintName(string name)
{
    printf("hello, %s\n", name);
}

The code I used is from here:

http://docs.cs50.net/2016/fall/notes/1/week1.html

Please, help to fix an error.

4
  • 1
    You have to be careful of your function declarations. Note that PrintName comes after main, meaning that when the program enters main, it doesn't really know the existence of PrintName. There are two possible fixes to that sort of error, but I am not going to give it out. Commented Apr 7, 2017 at 8:58
  • @Lysandros Nikolaou, thank you! I realise this is silly question, but I don't delete it, someone might catch same error. Commented Apr 7, 2017 at 9:02
  • It's actually not a silly question at all. We have all done this very mistake, I am sure. Commented Apr 7, 2017 at 9:57
  • 1
    I know this is an old question, but I think some important info is missing: The C90 Standard allowed implicit function declarations(and because of this, the above code will compile and work), this was changed in C99 standard.
    – Heto
    Commented Jan 14, 2020 at 7:45

1 Answer 1

7

Sorry, beginner error:

But now if we try to print_name(s) in our main function, we still get an error. And that’s because the compiler reads from top to bottom, in order, so at the time main calls print_name, it doesn’t exist yet. So we need to declare it with something called a prototype first:

#include <cs50.h>
#include <stdio.h>

void print_name(string name);

int main(void)
{
    string s = get_string();
    print_name(s);
}

void print_name(string name)
{
    printf("hello, %s\n", name);
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .