a.c:7:13:+warning:+format+‘%d’+expects+argument+of+type+‘int+*’,+but+argument+2+has+type+‘long+int+*
时间: 2023-12-14 09:33:07 浏览: 55
这个警告是由于格式化字符串中使用了%d,但是传入的参数类型是long signed int,而不是int。解决这个问题的方法是将格式化字符串中的%d改为%ld,表示long signed int类型。例如:
```c
long signed int num = 1234567890;
printf("The number is: %ld", num);
```
相关问题
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 格式说明符可以保证输出正确。