小编Pra*_*ada的帖子

如何在C中对字符串函数进行链接?

我试图使用sting.h头文件中的两个不同函数(不包括它)strlen()strtok().Strlen成功执行没有任何错误(但有些警告),strtok在运行时失败.如果我不包含头文件,为什么strlen()函数worknig gine而不是strtok()?我想在链接过程中有一些我不明白的东西.请澄清这种行为.但是,如果我打印一个'%c'而不是'%s'(strtok返回一个垃圾值),程序会成功终止.

码:

int main()
{
    char string[] = "This is a String";
    printf("\nLength of sting is %d\n",strlen(string));
}
Run Code Online (Sandbox Code Playgroud)

警告:hello.c:在函数'main'中:hello.c:4:2:警告:内置函数'printf'的不兼容隐式声明[默认启用] hello.c:4:37:警告:不兼容隐式内置函数'strlen'的声明[默认启用] hello.c:4:2:警告:格式'%d'需要类型为'int'的参数,但参数2的类型为'long unsigned int'[-Wformat] ]

输出:$ ./a.out

Length of sting is 16
Run Code Online (Sandbox Code Playgroud)

码:

int main()
{
    char string[] = "This is a String";
    printf("\nLength of sting is %d\n",strlen(string));
    char *a = strtok(string," ");
    printf("%s",a);
}
Run Code Online (Sandbox Code Playgroud)

警告:$ gcc hello.c hello.c:在函数'main'中:hello.c:4:2:警告:内置函数'printf'的不兼容隐式声明[默认启用] hello.c:4:37 :警告:内置函数'strlen'的不兼容隐式声明[默认情况下启用] hello.c:4:2:警告:格式'%d'需要类型为'int'的参数,但参数2的类型为'long unsigned int'[ - Wformat] hello.c:5:12:警告:初始化使得整数指针没有强制转换[默认启用]

输出:$ …

c gcc

2
推荐指数
1
解决办法
458
查看次数

标签 统计

c ×1

gcc ×1