C99删除stricmp()和strnicmp()? [英] C99 remove stricmp() and strnicmp()?
问题描述
时的功能 stricmp()
和 strnicmp()
C99中去掉?
我总是得到funtion stricmp预警的隐式声明()(也是 strnicmp())当我尝试编译它针对C99。
例如,简单的code下面让我警告。
Is the functions stricmp()
and strnicmp()
removed in C99?
I always get warning implicit declaration of funtion stricmp() (and also strnicmp() ) when I try to compile it against C99.
For example, the simple code below get me that 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;
}
当我尝试编译这块code对C99(的gcc -Wall -std = C99的main.c -o主
),我得到的警告。但是,当我编译它,而不 -std = C99
,没有警告将被抛出。
然而,即使是隐式声明的警告,我的code还是顺利实施。
When I try to compile this piece of code against C99(gcc -Wall -std=c99 main.c -o main
), I get that warning. But when I compile it without the -std=c99
, no warning will be thrown.
However, even though there is warning of implicit declaration, my code still work out right.
这是为什么?那是一个错误吗?如果没有错误,那么什么是C99的完全变化,这使该警告发生的?
Why is that? Is that a bug? If not a bug, then what is exactly the change of C99 which make that warning happen?
推荐答案
在code。与C99编译,它符合C99标准,它不具有 stricmp()
。当code编译没有C99交换机,它符合一个未知的标准实现 stricmp()
。 (鉴于 GCC
没有 -std = C99
,可能编译到C89 / 90标准wihich允许隐式声明。)
When code compiles with C99, it conform to the C99 standard, which does not have stricmp()
. When code compile without C99 switch, it conforms to an unknown standard that implements stricmp()
. (Given gcc
without -std=c99
, likely compiles to the C89/90 standard wihich allows implicit declarations.)
由于 @Joachim Pileborg 评论,比较不敏感是不是C标准的一部分。
As @Joachim Pileborg commented, insensitive compares are not part of the C standard.
使用C99隐函数需要一个诊断(一个在这种情况下,警告)。如果没有C99,函数的隐式使用不会产生警告。该功能存在的这个的编译器的库 - 它只是一个问题是在使用前声明的函数。
With C99 implicit functions require a diagnostic (a warning in this case). Without C99, the implicit use of the function generates no warning. The functions exists in this compiler's library - it is just a question of are the functions declared before use.
很容易的让自己的:
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;
}
请注意:当编码并试图让 AZ
匹配 AZ
,字符串大小写比较程序往往工作均匀良好。但是,试图向的为了的字符串,事情很快变得不可收拾时。 ABC与_bc来临之前或其他依靠,如果同情做大写或小写之后。 '_'
,在ASCII,上和小写字母之间。随着国际和区域问题,情况变得更加复杂。我的code示例使用转换的一个来回,以应对那里的大写字母数字符
没有一个1对1的映射与小写字母的问题。 IMO强劲不区分大小写的比较复杂责成使用UTF编码和病例定义的。
Note: When coding and trying to make A-Z
match a-z
, string insensitive compare routines tend to work uniformly well. But when trying to to order strings, things quickly get out of hand. "abc" vs. "_bc" can come before or after the other depending on if compassion was done as upper or lower case. '_'
, in ASCII, exists between the upper and lower case letters. With internationalization and locale issues, the situation becomes more complex. My code example uses a round-trip of conversion to cope with issues where the number of uppercase char
does not have a 1-to-1 mapping with lowercase ones. IMO the complexities of robust case insensitive compares obliges the use of UTF encoding and its case definition.
这篇关于C99删除stricmp()和strnicmp()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!