内置函数“printf”的不兼容隐式声明

时间:2021-10-30 06:58:05

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.

我有一个大问题。这是我创建的头文件,有一个小终端菜单。问题是,在函数“menu”(意大利语名)中,当我编译它时,我得到一个警告,说“[警告]内建函数‘printf’的不兼容隐式声明”,然后如果我运行它,它就会崩溃。

#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

我疯了,是的,我在其他问题上找了很多帮助……谢谢你的帮助!:P

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

顺便说一句:“nuovaOpzione”中的strcpy函数也是一个警告,但是yolo…

4 个解决方案

#1


1  

Jens is right about the printf warning.

延斯关于printf的警告是对的。

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

对于崩溃,基于你的程序,你需要一个opz的数组,而不是一个指针。

struct Opzione opz[1000];

#2


2  

You forgot the include:

你忘记了包括:

#include <stdio.h>

#3


0  

incompatible implicit declaration of built-in function 'printf

内建函数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()的调用时,它就自动地(也就是隐式地)定义了如下定义:

printf(char[], char)

printf(char[],字符)

but later on you call it with

但稍后你再打电话给它

printf(char[])

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.

正如其他海报所言,您应该包含适当的定义,并且原则上不要将代码放入头文件中。

#4


0  

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.

尝试将menu()的主体放到.c源文件中(如@ouah所指出的),并添加#include (如@Jens所示),因为编译器必须知道它的签名。 。h文件中的h>

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.

在c中,将代码包含在.h文件中并不是很好的做法。

#1


1  

Jens is right about the printf warning.

延斯关于printf的警告是对的。

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

对于崩溃,基于你的程序,你需要一个opz的数组,而不是一个指针。

struct Opzione opz[1000];

#2


2  

You forgot the include:

你忘记了包括:

#include <stdio.h>

#3


0  

incompatible implicit declaration of built-in function 'printf

内建函数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()的调用时,它就自动地(也就是隐式地)定义了如下定义:

printf(char[], char)

printf(char[],字符)

but later on you call it with

但稍后你再打电话给它

printf(char[])

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.

正如其他海报所言,您应该包含适当的定义,并且原则上不要将代码放入头文件中。

#4


0  

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.

尝试将menu()的主体放到.c源文件中(如@ouah所指出的),并添加#include (如@Jens所示),因为编译器必须知道它的签名。 。h文件中的h>

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.

在c中,将代码包含在.h文件中并不是很好的做法。