C语言 警告:格式 '%d' 需要类型为 'int' 的参数,但参数 2 的类型为 'long int' [-Wformat=]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27725531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
提问by Zeyad Jameel
This code is about.
这段代码是关于。
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:
竞争条件:调度和编译器行为在进程或线程同步中起着重要作用。证明需要同步的最简单场景来自尝试修改共享变量值的两个线程/进程之间创建的竞争条件,这通常会导致数据不一致和错误结果。以下示例演示了这种情况:
I'm new to C and am having trouble with what is happening with this warning. What does the warning mean and how can i fix it. The code i wrote is here:
我是 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);
Here's the code:
这是代码:
// 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;
}
回答by Iharob Al Asimi
You have to change "%d"with "%ld", "%d"is for signed intand here the lstands for longso "%ld"is for signed long int.
您必须更改"%d"with "%ld","%d"表示已签名int,此处的l代表longso"%ld"表示已签名long int。
回答by user3629249
Code should always be written with the idea of a human is going to read it. even the writer had significant problems reading the code as shown by the many 'copy-and-paste' errors
编写代码时应该始终考虑到人类会阅读它。甚至作者在阅读代码时也遇到了严重的问题,如许多“复制和粘贴”错误所示
Note: when a '//' comment is started on a line, ALL the rest of the line is a comment
注意:当“//”注释在一行开始时,该行的所有其余部分都是注释
any functions, other than main, should have a prototype. The method of stating the called functions before any function that calls them will work for trivial programs, but not for multi file programs nor programs with crossing call paths I.E. always supply prototype statements.
除了 main 之外的任何函数都应该有一个原型。在任何调用它们的函数之前声明被调用函数的方法适用于普通程序,但不适用于多文件程序或具有交叉调用路径的程序 IE 总是提供原型语句。
When calling some function, always know (I.E. look it up) the full details about that function. In the case of the OPs posted code, this especially applies to the syscall() function.
在调用某个函数时,始终知道(IE 查找)有关该函数的完整详细信息。对于 OP 发布的代码,这尤其适用于 syscall() 函数。
When writing threads, there is more to know than just the prototype for a thread. As in the OPs code, also need to know how to exit the thread
在编写线程时,需要了解的不仅仅是线程的原型。和OPs代码一样,也需要知道如何退出线程
ALWAYS ALWAYS enable all warnings when compiling. Warnings are problems in the code that should be fixed before continuing.
编译时始终启用所有警告。警告是代码中应该在继续之前修复的问题。
BTW: the use of a single mutex would have eliminated the race condition problem
顺便说一句:使用单个互斥锁可以消除竞争条件问题
// Race condition
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
void *runner( void * );
int program( void );
int x=0;
// <-- compiler raises warning about unused parameter
void * runner(void *arg)
{
// the returned value from the syscall is a long int
//printf("T tid: %d x before: %d\n",
printf("T tid: %ld x before: %d\n",
syscall(SYS_gettid),
x);
int i;
for (i = 0; i < 100000; i++ )
{
x = x + 1;
}
// <-- the returned value from syscall is a long int
//printf("T tid: %d x after: %d\n",
printf("T tid: %ld x after: %d\n",
syscall(SYS_gettid),
x);
// <-- this line missing, compiler raises warning
pthread_exit(NULL);
} // end function: runner
int program()
{
pthread_t t1,t2,t3,t4;
printf("Parent pid: %d x before threads: %d\n",
getpid(),
x);
//int i; // <-- this variable not used, results in compiler warning
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) )
{
// <-- copy and paste error
//printf("Error creating thread 1\n");
printf("Error creating thread 3\n");
return 1;
}
if( pthread_create(&t4, NULL, runner, NULL) )
{
// <-- copy and paste error
//printf("Error creating thread 1\n");
printf("Error creating thread 4\n");
return 1;
}
if( pthread_join(t1, NULL) )
{
printf("error joining thread 1");
return 1;
}
if( pthread_join(t2, NULL) )
{
// <-- copy and paste error
//printf("error joining thread 1");
printf("error joining thread 2");
return 1;
}
if( pthread_join(t3, NULL) )
{
// <-- copy and paste error
//printf("error joining thread 1");
printf("error joining thread 3");
return 1;
}
if( pthread_join(t4, NULL) )
{
// <-- copy and paste error
//printf("error joining thread 1");
printf("error joining thread 4");
return 1;
}
printf("Parent pid: %d x after threads: %d\n",
getpid(),
x);
return 0;
} // end function: program
// <-- this line cause compiler to raise two warnings about unused parameters
//int main(int argc, char *argv[])
int main()
{
int count=0;
// <-- there is no loop code perhaps you meant to put the 'while' on the next line
// loop runs the program count times while(count<5)
while( count < 5 )
{
// <-- program is not run, perhaps you meant to put the 'program()' on the next line
// running program program();
program();
count++;
// <-- x is not being reset, perhaps you menat to put 'x=0;' on the next line
//reset global x for next run of program. x=0;
x=0;
printf("\n\n");
}
return 0;
} // end function: main
回答by chux - Reinstate Monica
The prototype for syscall()returns a long
syscall()的原型返回一个long
#include <sys/syscall.h>
long syscall(long number, ...);
When calling printf(), its format specifiers, like "%d", should match the type of the parameter (after the parameter goes through the usual promotions for a variadic argument). Change to
调用 时printf(),其格式说明符,如"%d",应与参数的类型匹配(在参数通过可变参数的通常提升之后)。改成
v---- long --------v
v----------- int ----------v
printf("T tid: %ld x before: %d\n", syscall(SYS_gettid), x);

