C99 remove stricmp () and strnicmp ()?

Are the functions stricmp() and strnicmp() removed in C99? I always get a warning implicit declaration of funtion stricmp () (as well as strnicmp () ) when I try to compile it with C99. For example, the simple code below gives me this warning.

 #include<string.h> #include<stdio.h> char arr[100]="hello"; char arr2[100]="hEllo"; int main() { int n=-1; printf("%d\n",n); n=strnicmp(arr,arr2,3); // the same when use the function stricmp(); printf("%d\n",n); getchar(); return 0; } 

When I try to compile this piece of code with C99 ( gcc -Wall -std=c99 main.c -o main ), I get this warning. But when I compile it without -std=c99 , there will be no warning. However, despite the fact that there is a warning about the implicit declaration, my code still works correctly.

Why? This is mistake? If not an error, then what exactly is the change in C99, what is the reason for this warning?

+3
source share
2 answers

When code is compiled with C99, it conforms to the C99 standard, which does not have stricmp() . When code is compiled without a C99 switch, it conforms to an unknown standard that implements stricmp() . (Given gcc without -std=c99 , the C89 / 90 standard is likely to compile, allowing for implicit declarations.)

As @Joachim Pileborg notes , insensitive comparisons are not part of the C standard.

Using C99 for implicit functions requires diagnostics (warning in this case). Without C99, implicit use of a function does not generate any warnings. Functions exist in this compiler library - we are talking only about functions declared before use.

Easy enough to make your own:

 int wal_stricmp(const char *a, const char *b) { int ca, cb; do { ca = (unsigned char) *a++; cb = (unsigned char) *b++; ca = tolower(toupper(ca)); cb = tolower(toupper(cb)); } while (ca == cb && ca != '\0'); return ca - cb; } 

Note. When coding and trying to make AZ match AZ , string methods for checking for unevenness work equally well. But when trying to order strings, things quickly get out of hand. "abc" vs. "_bc" may appear before or after another, depending on whether compassion was done as upper or lower case. '_' , in ASCII, exists between uppercase and lowercase letters. In the context of internationalization and localization, the situation becomes more complicated. My code example uses reverse conversion to deal with problems when the number of capital letters char does not match 1 to 1 with lowercase. IMO The complexity of strong case-insensitive errors requires the use of UTF encoding and the determination of its case.

+6
source

stricmp and strincmp are non-standard functions. They have never been part of standard C.

+1
source

Source: https://habr.com/ru/post/989979/


All Articles