Το παρακάτω πρόγραμμα prototype.c μεταγλωττίζεται με μήνυμα
implicit declaration of function ‘print_err’. Γιατί; Διορθώστε το χωρίς να
μετακινήσετε τον ορισμό της print_err.
#include <stdio.h>
int main() {
print_err("hello");
return 0;
}
int print_err(char * msg) {
return fprintf(stderr, "%s\n", msg);
}
$ gcc -o prototype prototype.c
prototype.c: In function ‘main’:
prototype.c:4:3: warning: implicit declaration of function ‘print_err’
[-Wimplicit-function-declaration]
4 | print_err("hello");
| ^~~~~~~~~
Μετά τη διόρθωση:
$ gcc -o prototype prototype.c
$ ./prototype
hello
Ο μεταγλωττιστής της C διαβάζει το αρχείο μία φορά, από πάνω προς τα κάτω. Τι
χρειάζεται να ξέρει για την print_err όταν φτάσει στη γραμμή 4, και πώς του το
λέτε χωρίς να γράψετε το σώμα της;
Αριθμός στον οδηγό: Α23.1
(στο κεφάλαιο) ·
Μόνιμο αναγνωριστικό: slides-lec23-implicit-declaration ·
Σύνδεσμος: https://progintro.github.io/study/questions/slides/slides-lec23-implicit-declaration.html ·
Markdown (GitHub)