linux显示589:108: warning: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 8 has type ‘Bit32u {aka unsigned int}’ [-Wformat=]
时间: 2024-03-30 21:38:32 浏览: 42
这个警告是由于在格式化输出时使用了错误的格式字符。`%lx` 是用于 long unsigned int 类型的,而你提供的参数的类型是 `Bit32u`,它实际上是一个 unsigned int 类型,因此编译器会发出警告。要修复这个问题,你可以使用 `%u` 或者 `%lu` 格式字符,具体取决于你的参数是否为无符号数。例如,如果你的参数是无符号数,应该使用 `%u` 格式字符,如果你的参数是长无符号整数,应该使用 `%lu` 格式字符。
相关问题
warning: format '%x' expects argument of type 'unsigned int', but argument 5 has type 'long unsigned int' [-Wformat=]
这是一个编译器的警告信息,它告诉你使用了错误的格式化字符。在使用格式化输出函数时,你需要指定正确的格式化字符与对应的参数类型。在这个警告信息中,使用了格式化字符 '%x' 来输出一个长整型参数,但是应该使用 '%lx' 来输出一个无符号长整型参数。如果你不纠正这个错误,程序可能会输出错误的结果或者崩溃。
warning: format ‘%u’ expects type ‘unsigned int’, but argument 2 has type ‘pthread_t’
这个警告是因为使用了错误的格式说明符。在使用 printf() 函数输出 pthread_t 类型的变量时,应该使用 %lu 格式说明符,而不是 %u。因此,将代码中的 printf() 函数中的 %u 替换为 %lu 即可解决该警告。修改后的代码如下:
```c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *func1() {
printf("i am a thread, threadId: %lu\n", pthread_self());
}
int main(int argc, char *argv[]) {
int pid1, pid2, pid3;
pthread_t tid1, tid2, tid3, tid4, tid5, tid6, tid7, tid8;
pid1 = fork();
switch (pid1) {
case 0:
pid2 = fork();
switch (pid2) {
case 0:
printf("i am a process, pid: %d\n", getpid());
pthread_create(&tid1, NULL, func1, NULL);
pthread_create(&tid2, NULL, func1, NULL);
break;
default:
printf("i am a process, pid: %d\n", getpid());
pthread_create(&tid3, NULL, func1, NULL);
pthread_create(&tid4, NULL, func1, NULL);
// exit(0);
break;
}
default:
pid3 = fork();
switch (pid3) {
case 0:
printf("i am a process, pid: %d\n", getpid());
pthread_create(&tid5, NULL, func1, NULL);
pthread_create(&tid6, NULL, func1, NULL);
break;
default:
printf("i am a process, pid: %d\n", getpid());
pthread_create(&tid7, NULL, func1, NULL);
pthread_create(&tid8, NULL, func1, NULL);
// exit(0);
break;
}
}
return 0;
}
```
需要注意的是,pthread_t 类型的变量本质上是无符号长整型(unsigned long),因此使用 %lu 格式说明符可以保证输出正确。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)