Why do not I get an error even when I have commented the header and used its function?

StackOverflow https://stackoverflow.com/questions/13189109

  •  29-07-2021
  •  | 
  •  

Question

In the following program I commented the statement #include <ctype.h> in the hope that an error will be thrown while using the functions like isupper and isgraph. But to my surprise no error is thrown. Why is this so ? The wikipedia page lists these functions in the header type ctype.h.

#include <stdio.h>
//#include <ctype.h>

int main() {
char ch;
for(;;) {
   ch = getc(stdin);
   if( ch == '.') break;
   int g = isgraph(ch);
   if(isupper(ch) != 0) printf("Is in upper case\n");
}   
return 0;   
 }

Note: I am using gcc to compile on linux (fedora).

Was it helpful?

Solution

By default, gcc runs in a rather permissive mode. You can get a warning by adding, for example:

 gcc -Wall -c yourfile.c

asking for all the main warnings. (There are more warnings you can asl for: -Wextra adds a bunch.) You might also specify -std=c99 (and maybe -pedantic) in order to get more warnings.

C99 requires functions to be defined or declared before they are used.

$ gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -c warn.c
warn.c:4:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]
warn.c: In function ‘main’:
warn.c:4:5: warning: old-style function definition [-Wold-style-definition]
warn.c:9:4: warning: implicit declaration of function ‘isgraph’ [-Wimplicit-function-declaration]
warn.c:10:4: warning: implicit declaration of function ‘isupper’ [-Wimplicit-function-declaration]
warn.c:9:8: warning: unused variable ‘g’ [-Wunused-variable]
$

That's the output of GCC 4.7.1 (on Mac OS X 10.7.5) with the standard set of compilation options I use — run on your source code stashed in a file warn.c.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top