I want to see an error, but the warning says it's an error.

Asked 2 years ago, Updated 2 years ago, 27 views

If stack is empty, I tried to make an error, but I was warned.
Is there a cause?

if(isStackEmpty()){
    error("stack is empty");
    exit(1);

c

2022-09-29 20:30

2 Answers

It contains speculation because only a part of the code is presented, but at least the error function is not defined in the commonly used stdio.h, so if the purpose is to send a message in case of an error, shouldn't we use error as the compiler warns us?

reference:
Standard C Library|Input/Output stdio.h|Wikipedia

(Excerpts only those with "error" in the name)

  • ferror—Determining the Error Indicator
  • perror—Output of error messages to standard error output


2022-09-29 20:30

Sample code below (not required error() pre-declared = bugged or old style)

#include<stdio.h>
# if 0
# include <stdlib.h>
void error(const char*message){
    fprintf(stderr, "%s\n", message);
    exit(0);
}
#endif
int main() {
    error("stackoverflow");
}

Try to compile with gcc-10.2.0

$gcc-c perrortest.c 
perfortest.c:Infunction 'main':
errortest.c:10:2:warning:implicit declaration of function 'error'; did you mean 'error'? [-Wimplicit-function-declaration]
   10 | error ("stackoverflow");
      |  ^~~~~
      |  error
$ 

Translating the warning freely

  • A function called error with a very similar name to the standard function error is used without declaration, but is this a mistake in writing error?

If #if0 is set to #if1, the error() function is declared prototype before use and is used as declared, so there is no warning that the error is incorrect.

So your source code is so fragmented that I can't say for sure, but if you add the original declaration of the error() function, the warning will disappear.


2022-09-29 20:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.