Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

C problem

C problem

2008-02-28 by bhadson2001

unsigned char a;
a=255*20/10;

it gives output 254

for
a=255*200/100;

it gives output 111

why it gives different results.

it there any logical relationship.

      kindly help me to resolve the problem.
                 i will be grateful
                           thanks a lot.......

Re: [AVR-Chat] C problem

2008-02-28 by Leon

----- Original Message ----- 
Show quoted textHide quoted text
From: "bhadson2001" <bhadson2001@yahoo.com>
To: <AVR-Chat@yahoogroups.com>
Sent: Thursday, February 28, 2008 8:13 AM
Subject: [AVR-Chat] C problem


> 
> unsigned char a;
> a=255*20/10;
> 
> it gives output 254
> 
> for
> a=255*200/100;
> 
> it gives output 111
> 
> why it gives different results.
> 
> it there any logical relationship.
> 

Try 255*(20/10) and 255*(200/10)

Leon
--
Leon Heller
Amateur radio call-sign  G1HSM
Yaesu FT-817ND transceiver
Suzuki SV1000S motorcycle
leon355@btinternet.com
http://www.geocities.com/leon_heller

Re: [AVR-Chat] C problem

2008-02-28 by Russell Shaw

bhadson2001 wrote:
> unsigned char a;
> a=255*20/10;
> 
> it gives output 254
> 
> for
> a=255*200/100;
> 
> it gives output 111
> 
> why it gives different results.
> 
> it there any logical relationship.
> 
>       kindly help me to resolve the problem.
>                  i will be grateful
>                            thanks a lot.......

255*20/10 -> 510 which overflows "unsigned char" (256).

The remainder is 510 % 256 -> 254.

Re: [AVR-Chat] C problem

2008-02-28 by David Kelly

On Thu, Feb 28, 2008 at 08:13:49AM -0000, bhadson2001 wrote:
> 
> unsigned char a;
> a=255*20/10;
> 
> it gives output 254
> 
> for
> a=255*200/100;
> 
> it gives output 111
> 
> why it gives different results.
> 
> it there any logical relationship.

The intermediate math is signed 16 bit integer. 255*200 = 51,000 which
is -14,546. Divide by 100 = -145 = 111 unsigned.

-- 
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.