无法在用户定义的函数中调用库函数
Can't call library function in user defined function
我正在用 c.
创建一个用户定义的 strcmp() 函数
#include <stdio.h>
#include <string.h>
int la,lb; // la and lb for length of 2 strings
int scompare(char [],char []);
int main() {
char a[50],b[50]; // a and b are 2 strings
printf("Enter 2 strings \n");
gets(a);
gets(b);
la =strlen(a);
lb =strlen(b);
printf("%d",scompare(a,b));
}
int scompare(char a[la],char b[lb])
{
for(int i=0 ;i<max(la,lb);i++)
{ // loop for comparing characters of 2 strings
// here in vs code it is showing error --->
// warning: implicit declaration of function 'max' [-Wimplicit-function-declaration]
for(int i=0 ;i<max(la,lb);i++)
int k = a[i]-b[i];// k is the sum of ascii of characters of a and b
if(k!=0 &&toupper(a[i])==toupper(b[i]))
return (k>0)?1:-1;
// other error is showing here in function toupper ---> warning: implicit declaration of function 'toupper' [-Wimplicit-function-declaration]
if(k!=0 &&toupper(a[i])==toupper(b[i]))
else if(k!=0)
return k;
}
return 0;
}
在调用一个函数之前,您需要提供它的声明,例如可以在 header.
中出现的声明
C中没有标准函数max
,需要自己写这样的函数
要使用标准函数 toupper
,您需要包含 header <ctype.h>
#include <ctype.h>
这个循环(假设函数max
定义在某处)
for(int i=0 ;i<max(la,lb);i++)
可以调用未定义的行为然后字符串的长度彼此不相等。
注意函数gets
不是标准的C函数。这是不安全的。你应该使用 fgets
.
以及这个函数声明
int scompare(char a[la],char b[lb]);
没有意义。
函数应该这样声明
int scompare( const char a[], const char b[] );
在您的代码中,max 和 toupper 未在任何 headers 中定义。
我觉得应该是这样的。
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define max(a,b) (((a) > (b)) ? (a) : (b))
int la, lb;
int scompare(char [], char []);
int main()
{
char a[50], b[50];
printf("Enter 2 strings \n");
gets(a);
gets(b);
la = strlen(a);
lb = strlen(b);
printf("%d\n", scompare(a, b));
}
int scompare(char a[], char b[])
{
for (int i = 0; i < max(la, lb); i++)
{
int k = a[i] - b[i];
if (k && toupper(a[i]) == toupper(b[i]))
{
return (k > 0 ? 1 : -1);
}
else if (k)
{
return k;
}
}
return 0;
}
但是,我认为你的 scompare 函数效率不高。
在我看来
int scompare(char a[], char b[])
{
for (int i = 0; i < max(la, lb); i++)
{
int k = a[i] - b[i];
if (k == 0) continue;
return k > 0 ? 1 : -1;
}
return 0;
}
如果您使用其他库中的函数,您需要包含这些库:
#include <ctype.h>
...
if(k!=0 &&toupper(a[i])==toupper(b[i]))
return (k>0)?1:-1;
...
如果使用自己的函数,需要在使用前声明,并在同一个文件中定义。假设 int max(int, int)
是您的函数:
#include <stdio.h>
/* function declaration */
int max(int lhs, int rhs);
...
/* usage */
int scompare(char a[la],char b[lb])
{
for(int i=0 ;i<max(la,lb);i++)
{
...
/* definition */
int max(int lhs, int rhs)
{
/* implementation */
}
我正在用 c.
创建一个用户定义的 strcmp() 函数#include <stdio.h>
#include <string.h>
int la,lb; // la and lb for length of 2 strings
int scompare(char [],char []);
int main() {
char a[50],b[50]; // a and b are 2 strings
printf("Enter 2 strings \n");
gets(a);
gets(b);
la =strlen(a);
lb =strlen(b);
printf("%d",scompare(a,b));
}
int scompare(char a[la],char b[lb])
{
for(int i=0 ;i<max(la,lb);i++)
{ // loop for comparing characters of 2 strings
// here in vs code it is showing error --->
// warning: implicit declaration of function 'max' [-Wimplicit-function-declaration]
for(int i=0 ;i<max(la,lb);i++)
int k = a[i]-b[i];// k is the sum of ascii of characters of a and b
if(k!=0 &&toupper(a[i])==toupper(b[i]))
return (k>0)?1:-1;
// other error is showing here in function toupper ---> warning: implicit declaration of function 'toupper' [-Wimplicit-function-declaration]
if(k!=0 &&toupper(a[i])==toupper(b[i]))
else if(k!=0)
return k;
}
return 0;
}
在调用一个函数之前,您需要提供它的声明,例如可以在 header.
中出现的声明C中没有标准函数max
,需要自己写这样的函数
要使用标准函数 toupper
,您需要包含 header <ctype.h>
#include <ctype.h>
这个循环(假设函数max
定义在某处)
for(int i=0 ;i<max(la,lb);i++)
可以调用未定义的行为然后字符串的长度彼此不相等。
注意函数gets
不是标准的C函数。这是不安全的。你应该使用 fgets
.
以及这个函数声明
int scompare(char a[la],char b[lb]);
没有意义。
函数应该这样声明
int scompare( const char a[], const char b[] );
在您的代码中,max 和 toupper 未在任何 headers 中定义。 我觉得应该是这样的。
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define max(a,b) (((a) > (b)) ? (a) : (b))
int la, lb;
int scompare(char [], char []);
int main()
{
char a[50], b[50];
printf("Enter 2 strings \n");
gets(a);
gets(b);
la = strlen(a);
lb = strlen(b);
printf("%d\n", scompare(a, b));
}
int scompare(char a[], char b[])
{
for (int i = 0; i < max(la, lb); i++)
{
int k = a[i] - b[i];
if (k && toupper(a[i]) == toupper(b[i]))
{
return (k > 0 ? 1 : -1);
}
else if (k)
{
return k;
}
}
return 0;
}
但是,我认为你的 scompare 函数效率不高。 在我看来
int scompare(char a[], char b[])
{
for (int i = 0; i < max(la, lb); i++)
{
int k = a[i] - b[i];
if (k == 0) continue;
return k > 0 ? 1 : -1;
}
return 0;
}
如果您使用其他库中的函数,您需要包含这些库:
#include <ctype.h>
...
if(k!=0 &&toupper(a[i])==toupper(b[i]))
return (k>0)?1:-1;
...
如果使用自己的函数,需要在使用前声明,并在同一个文件中定义。假设 int max(int, int)
是您的函数:
#include <stdio.h>
/* function declaration */
int max(int lhs, int rhs);
...
/* usage */
int scompare(char a[la],char b[lb])
{
for(int i=0 ;i<max(la,lb);i++)
{
...
/* definition */
int max(int lhs, int rhs)
{
/* implementation */
}