警告:格式“%d"需要“int"类型的参数,但参数 2 的类型为“long int"[-Wformat=] [英] warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]

查看:71
本文介绍了警告:格式“%d"需要“int"类型的参数,但参数 2 的类型为“long int"[-Wformat=]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码是关于.

竞争条件:调度和编译器行为在进程或线程同步中起着重要作用.证明需要同步的最简单场景来自尝试修改共享变量值的两个线程/进程之间创建的竞争条件,这通常会导致数据不一致和错误结果.以下示例演示了这种情况:

Race conditions: Scheduling and compiler behaviour play a significant role in process or thread synchronization. The simplest scenario to demonstrate the need to synchronization comes from the race conditions created between two threads/process trying to modify a value of a shared variable, which typically results in data inconsistency, and erroneous results. The following example demonstrates this situation:

我是 C 新手,对此警告所发生的事情有疑问.警告是什么意思,我该如何解决.我写的代码在这里:

q1.c: In function ‘runner’:
q1.c:13:1: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
 printf("T tid: %d x before: %d\n", syscall(SYS_gettid),x); int i;
 ^
q1.c:19:1: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
 printf("T tid: %d x after: %d\n", syscall(SYS_gettid),x);

代码如下:

// Race condition
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
int x=0;
void * runner(void *arg)
{
printf("T tid: %d   x before: %d\n", syscall(SYS_gettid),x); int i;
for (i = 0; i < 100000; i++ )
{
x = x + 1;
}
printf("T tid: %d   x after: %d\n", syscall(SYS_gettid),x);
}

int program()
{
pthread_t t1,t2,t3,t4;
printf("Parent pid: %d  x before threads: %d\n", getpid(),x); int i;
if(pthread_create(&t1,NULL, runner, NULL)){ printf("Error creating thread 1\n"); return 1;
}
if(pthread_create(&t2,NULL, runner, NULL)){ printf("Error creating thread 2\n"); return 1;
}
if(pthread_create(&t3,NULL, runner, NULL)){ printf("Error creating thread 1\n"); return 1;
}
if(pthread_create(&t4,NULL, runner, NULL)){ printf("Error creating thread 1\n"); return 1;
}

if(pthread_join(t1,NULL)){ printf("error joining thread 1"); return 1;
}
if(pthread_join(t2,NULL)){ printf("error joining thread 1"); return 1;
}
if(pthread_join(t3,NULL)){ printf("error joining thread 1"); return 1;
}

if(pthread_join(t4,NULL)){ printf("error joining thread 1"); return 1;
}
printf("Parent pid: %d  x after threads: %d\n", getpid(),x); return 0;
}

int main(int argc, char *argv[]) { 
int count=0;
// loop runs the program count times 
while(count<5)
{
// running program program();
count++;
//reset global x for next run of program. x=0;
printf("\n\n");
}
return 0;
}

推荐答案

You have to change "%d" with "%ld", "%d" 代表有符号的 int,这里的 l 代表 long 所以 "%ld"用于带符号的long int.

You have to change "%d" with "%ld", "%d" is for signed int and here the l stands for long so "%ld" is for signed long int.

这篇关于警告:格式“%d"需要“int"类型的参数,但参数 2 的类型为“long int"[-Wformat=]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆