Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Without including #include <ctype.h>

Tags:

c

header-files

I have written the below programs without including #include <ctype.h>. I am able to execute the program. Where are these prototypes declared? I am using gcc.

1.

#include <stdio.h>
int main()
{
    if(isalnum(';'))
        printf("character ; is not alphanumeric");
    if(isalnum('A'))
        printf("character A is alphanumeric ");
    return 0;
}

2.

#include <stdio.h>
int main()
{
    printf("Lower case of A is %c \n", tolower('A'));
    printf("Lower case of 9 is %c \n", tolower('9'));
    printf("Lower case of g is %c \n", tolower('g'));
    printf("ASCII value of B is %d \n", toascii('B'));
    printf("Upper case of g is %c \n", toupper('g'));
    return 0;
}
like image 810
venkat Avatar asked Jan 10 '11 10:01

venkat


1 Answers

  1. In your code these functions are implicitly declared, so they are not included from any particular header. If you crank up warning level for your compiler, you will see (e.g. GCC):

    $ gcc -Wall -o a.c
    a.c: In function ‘main’:
    a.c:4: warning: implicit declaration of function ‘isalnum’
    

    If the definition of the function is not available, the compiler assumes i's a function taking any number of arguments and returning int. For example, the following compiles:

    main(){fgetc(1,2,3,4,5);}
    
  2. As to where they should be declared, it's the <ctype.h> header. Of course, different C implementations may include this header in other headers, so the code may appear to work without including <ctype.h>, but if you want your code to compile without warnings across different C implementations, you should include this header.

like image 87
Alex B Avatar answered Oct 03 '22 18:10

Alex B