DmitryB
DmitryB

Reputation: 129

incompatible implicit declaration of built-in function 'printf"

I have a big problem. This is the header file I created to have a little terminal menu. The problem is, in function "menu" (italian name) , that when I compile it I get a warning that says "[Warning] incompatible implicit declaration of built-in function 'printf' [enabled by default]" And then if I run it it CRASHES.

#ifndef HEADER
#define HEADER

int numeroOpz = 0;
int selezON = 0;
int tasto;

struct Opzione{
 char *testo;
 int selez;
 };

struct Opzione *opz;

void nuovaOpzione(char *testoOpz){
 strcpy(opz[numeroOpz].testo, testoOpz);   //Il testo dell'opzione viene copiato
 opz[numeroOpz].selez = 0;          //Nessuna opzione viene inizialmente selezionata
 numeroOpz++;
 }

void menu(){
 opz[0].selez = 1;

 while(tasto != 13){
 int i;
 for(i=0;i < numeroOpz;i++){
           if(opz[i].selez == 1){
                    printf("||%s||\n", opz[i].testo);
                           }
           else if(opz[i].selez == 0){
                    printf("%s\n", opz[i].testo);
                           }
           tasto = getch();

           switch (tasto){
                  case 72:  //SU
                     if(selezON > 0){
                         opz[selezON].selez = 0;
                         opz[selezON-1].selez = 1;
                         selezON--;
                                    }
                     else{
                         opz[selezON].selez = 0;
                         opz[numeroOpz-1].selez = 1;
                     }
                  break;
                  case 80:  //GIU
                     if(selezON < numeroOpz){
                         opz[selezON].selez = 0;
                         opz[selezON+1].selez = 1;
                         selezON++;
                                    }
                     else{
                         opz[selezON].selez = 0;
                         opz[0].selez = 1;
                     }
                  break;
                  default:
                         printf("Error\n\n\n\n");
                  break;
                  }
           }

   }
 }

#endif

And HERE is the source file:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Opzioni.h"

int main(){
nuovaOpzione("Ciao");
nuovaOpzione("Bellaaa");
nuovaOpzione("Hey");

menu();

getch();
return 0;
}

I'm just getting crazy, and yes, I searched a lot for help in other questions... Thanks for helping if you do! :P

btw: the strcpy function in "nuovaOpzione" is a warning as well, but yolo...

Upvotes: 2

Views: 15542

Answers (4)

Matthieu
Matthieu

Reputation: 3097

Try putting the body of menu() in the .c source file instead (as @ouah pointed out) and add the #include <stdio.h> in your .h file (as @Jens indicated), as it is mandatory for your compiler to know its signature.

Including code in a .h file is NOT good practice in C. Some compiler might turn on/off specifics when processing one or the other.

Upvotes: 0

rivimey
rivimey

Reputation: 931

incompatible implicit declaration of built-in function 'printf

The problem is that when the compiler first encountered the call to printf(), it made up for itself (that is, implicitly) a definition like:

printf(char[], char)

but later on you call it with

printf(char[])

so it complained.

As other poster have said, you should include to incorporate the proper definition, and on principle don't put code in header files.

Upvotes: 0

codnodder
codnodder

Reputation: 1675

Jens is right about the printf warning.

For the crashing, based on your program, you need an array for opz, not a pointer.

struct Opzione opz[1000];

Upvotes: 1

Jens Gustedt
Jens Gustedt

Reputation: 78903

You forgot the include:

#include <stdio.h>

Upvotes: 3

Related Questions