comparison between signed and unsigned

J

John Buckley

Hello!

In my program I need to compare a long variable with a size_t variable. The
long variable's value is guaranteed to be positive since it's initialised by
a function returning a positive value (or -1L when an error occurs, which I
check for). When I compare these variables with >, I get the warning
"comparison between signed and unsigned". Given that the long variable is
guaranteed to hold a positive value, can I ignore this diagnostic or should
I deal with it in some way?

BTW, I'm having endless trouble getting the hang of C. I can't quite
understand it because I tend to grasp languages pretty quickly. How long
does it take for C to truly 'click'?
 
S

Sheldon Simms

Hello!

In my program I need to compare a long variable with a size_t variable. The
long variable's value is guaranteed to be positive since it's initialised by
a function returning a positive value (or -1L when an error occurs, which I
check for). When I compare these variables with >, I get the warning
"comparison between signed and unsigned". Given that the long variable is
guaranteed to hold a positive value, can I ignore this diagnostic or should
I deal with it in some way?

There was a thread recently talking about this. The message ID of the
first post was

<[email protected]>
 
D

Dan Pop

In said:
In my program I need to compare a long variable with a size_t variable. The
long variable's value is guaranteed to be positive since it's initialised by
a function returning a positive value (or -1L when an error occurs, which I
check for). When I compare these variables with >, I get the warning
"comparison between signed and unsigned". Given that the long variable is
guaranteed to hold a positive value, can I ignore this diagnostic or should
I deal with it in some way?

The best thing to do, if you trust your C skills, is to disable (or not
enable) this warning. Most of the time (or all the time if you know what
you're doing) it is merely a source of noise. gcc -Wall, for example,
does not enable such warnings, leaving them for the masochists who also
use -W.
BTW, I'm having endless trouble getting the hang of C. I can't quite
understand it because I tend to grasp languages pretty quickly. How long
does it take for C to truly 'click'?

It depends on how much time you spend trying to understand the language,
rather than playing with it. After reading K&R2 and the c.l.c FAQ, you
should have a fairly clear idea about the language, including most of the
darker spots.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top