-1
auto.cpp: In function ‘int autooo(unsigned int)’:
auto.cpp:33:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

im doing the makefile , and i already run the makefile and make an auto.o but still i get this error, below is my autooo.cpp , auto.h

i dont undestand what is unsigned and signed :\ please help

auto.h

#ifndef function_H_
#define function_H_

int autooo(unsigned);
int interr(unsigned);


#endif /* function_H_ */

autooo.cpp

#include <iostream>   
#include <cstdlib>  //for random functions
#include "prime.h"
#include "auto.h"
using namespace std;


#ifndef auto_CPP_
#define auto_CPP_

int autooo(unsigned);

int autooo(unsigned a)
{       
    int b=50;
    unsigned numberFound = 0;

    do
    {    
        ++a;    

        if (isPrime(a))    
        {    
            ++numberFound;    
            cout << a << "is prime number" <<endl;    
        }    
    } while (numberFound < b);

    return 0;
}

#endif
Barry
  • 286,269
  • 29
  • 621
  • 977
  • `auto.cpp:33:25: warning: comparison between signed and unsigned`. This seems to be a **warning**, not an error. You program should run OK until you use really big positive numbers. – thor Nov 06 '14 at 17:58

5 Answers5

2

The compiler warns that the code contains comparison of unsigned int with a signed int in the line

while (numberFound < b);

This has nothing to do with makefiles or make.

You can fix that by changing

int b=50;

to

unsigned b = 50;

or by changing

unsigned numberFound = 0;

to

int numberFound = 0;

The problems you might run into when comparing signed int and unsigned int are explained in this answer to another SO question

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

At this line

while (numberFound < b);

The first is an unsigned int and the second an int. So you have to make them the same type, or if you are completely sure cast one of them.

As Etan commented:

"Blindly casting away a warning just to avoid the warning is a mistake. You need to understand what the warning is telling you and decide what the right fix is."

gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

You are getting this warning about comparing signed and unsigned types because the ranges of signed and unsigned ints are different. If you have to make such a comparison, you should explicitly cast one of the values to be compatible with the other, but a check is required to make sure value your cast is valid.

For e.g:-

int i = someIntValue();

if (i >= 0)
{
    // i is non-negative, so it is safe to compare to unsigned value
    if ((unsigned)i >= u)
        // do something
}
ravi
  • 10,994
  • 1
  • 18
  • 36
0

It says you are comparing two different things. Most notably the range of one does not fit into the range of another.

I.e there. Exists a number in the unsigned range that cannot be expressed as a signed number

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
-1

Type cast the code before you were comparing the signed and unsigned code to avoid warning

int a;
unsigned int b;

if(a==b) gives warning

if(a == (int)b)

will resolve your issue

EDIT

Blind casting will lead to some unexpected results

The warning is because ranges for signed and unsigned are different.

Casting will work fine when signed integer you were used for comparison was greater than zero.

so have check whether the signed integer is greater that zero before comparing

More info here

Community
  • 1
  • 1
VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38