warning: format '%x' expects argument of type 'unsigned int', but argument 5 has type 'long unsigned int' [-Wformat=]
时间: 2024-04-26 08:22:22 浏览: 9
这是一个编译器的警告信息,它告诉你使用了错误的格式化字符。在使用格式化输出函数时,你需要指定正确的格式化字符与对应的参数类型。在这个警告信息中,使用了格式化字符 '%x' 来输出一个长整型参数,但是应该使用 '%lx' 来输出一个无符号长整型参数。如果你不纠正这个错误,程序可能会输出错误的结果或者崩溃。
相关问题
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
这个warning是因为你使用了%d来格式化一个long int类型的变量,而%d只能用来格式化int类型的变量。为了避免这个warning,你可以使用%ld来格式化long int类型的变量。例如:
```
long int num = 123456789;
printf("The number is: %ld\n", num);
```
这样就可以避免这个warning了。
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 格式说明符可以保证输出正确。