Re: comparison between signed and unsigned int



compcreator@xxxxxxxxx wrote:

I have tried the following program. The problem is it is printing
False

.... snip ...

void main() {
unsigned int a = 5;
signed int b = -1;

if (b <= a) printf("True");
else printf("False");
}

Can somebody explain why is this happening....?

a is signed and negative, and cannot fit into the range covered by
b. Therefore it is converted to an unsigned value before
comparing. The conversion results in UINT_MAX, which is
considerably larger than 5. Signed can always be converted to
unsigned, but not the reverse.

Get rid of the 'void main()', which marks you as unknowing. main
returns an int, say and do so. The satisfactory return values are
0, EXIT_SUCCESS, and EXIT_FAILURE. The latter two require #include
<stdlib.h>. Also specify void in the parameter list, unless you
are using argc and argv.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • [PATCH] get rid if __cpuinit and __cpuexit
    ... unsigned long action, void *hcpu) ... unsigned int cpu = hcpu; ... -static int __cpuinit ... __cpu_up(unsigned int cpu) ...
    (Linux-Kernel)
  • [PATCH,RFC 2.6.14 09/15] KGDB: SuperH-specific changes
    ... This adds basic support for KGDB on SuperH as well as adding some architecture ... -static int kgdb_uart_getchar ... -static void kgdb_uart_putchar ... * The command-line option can include a serial port specification ...
    (Linux-Kernel)
  • problem in java swings
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • ToolTips in a View and TTN_NEEDTEXT
    ... extern int g_minWorkPeriod; ... void CDayView::CreateAllFonts ... void CDayView::DrawDayLog(CDC* pDC) ... BOOL CDayView::TimeToY ...
    (microsoft.public.vc.mfc)