Rachel
Rachel

Reputation: 19

C: Ignoring return value of scanf

int main()
{
int i, n, m;

while((scanf("%d",&n))!=EOF)
    {
        int a[n];

        for(i=0; i<n; i++){
            scanf("\n\n%d",&a[i]);}

        scanf("\n\n\n\n%d",&m);

        int b[m];

        for(i=0;i<m;i++){
            scanf("\n\n\n\n\n\n%d",&b[i]);}

        int count=0, place[n];

        for(i=0; i<n; i++){
            if(array_cmp(i,m,a,b)==1){
                count++;
                place[i]=i;
            }
        }
}

I keep getting the error, warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]

I can't seem to figure out whats wrong. The code is supposed to read input in the format:

n

0 0 0 0

m

0 0 0

n and m are the lengths of the first and second array.

Upvotes: 0

Views: 1271

Answers (1)

gsamaras
gsamaras

Reputation: 73366

scanf() returns a value, and you do not do anything with that in all your function calls, except from here:

scanf("%d",&n))!=EOF

where you actually do something with that the function returns.

You can avoid this message, by compiling with the specified by the compiler, flag (i.e. -Wno-unused-result).


PS: All the newline characters you have in the scanf() do nothing, and harm readability, discard them.

For instance, change this:

scanf("\n\n\n\n%d",&m);

to this:

scanf("%d", &m);

Note: The warning is generated because it is almost invariably a bad idea to ignore the return value of scanf(). It is better to explicitly cast the result with (void)scanf(…) on those rare occasions when you really don't need to worry about whether it succeeds or not.

Upvotes: 2

Related Questions