Warning: format '% d expects an argument of type' int, but argument 2 is of type 'long int [-Wformat =]

This code is about.

Race conditions: Planning and compiler behavior play a significant role in synchronizing processes or threads. The simplest scenario demonstrating the need for synchronization is based on race conditions created between two threads / process trying to change the value of a common variable, which usually leads to data inconsistency and erroneous results. The following example demonstrates this situation:

I am new to C and am having problems with what is happening with this warning. What the warning means and how I can fix it. The code I wrote is here:

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 is 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;
}
+4
3

"%d" "%ld", "%d" int, l long, "%ld" long int.

+10

, . " "

: "//",

, , . , , , .. .

- (I.E. ) . , OPs, syscall().

, . OPs, ,

. - , .

BTW:

// 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
0

syscall() long

#include <sys/syscall.h>
long syscall(long number, ...);

When called, printf()its format specifiers, such as "%d", must match the type of parameter (after the parameter passes through regular promotions for the variational argument). Change to

                 v---- long --------v 
                              v----------- int ----------v
printf("T tid: %ld x before: %d\n", syscall(SYS_gettid), x);
0
source

Source: https://habr.com/ru/post/1569573/


All Articles