linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* printf format type, precision...
@ 2004-07-02 22:32 J.
  2004-07-02 23:10 ` wwp
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: J. @ 2004-07-02 22:32 UTC (permalink / raw)
  To: linux-c-programming

Saturday, July 03 00:18:09

Hello,

I am trying to convert an IP address to it's eq. in base10
[homework]. However the outcome from the formula listed below isn't giving
me the right results.  Is this because I'm using the wrong precision type
for the `result' variable ? If that's the case what other precision type
should I use instead ? Right now I can't see the format tree's trhu the
precision forrest anmore .. ehum.

#include <stdio.h>

int main(void) {
 int var1 = 192; 
 int var2 = 168;
 int var3 = 1;
 int var4 = 10;
 long int result = 0;

 result = var1 * (256 ^ 3) + var2 * (256 ^ 2) * var3 * 256 + var4; 
 printf("%ld\n", value);

 return 0;
}

Your comments and suggestions will be greatly appreciated..

Thnks..!

J.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: printf format type, precision...
  2004-07-02 22:32 printf format type, precision J.
@ 2004-07-02 23:10 ` wwp
  2004-07-02 23:29   ` J.
  2004-07-02 23:11 ` Glynn Clements
  2004-07-02 23:19 ` wwp
  2 siblings, 1 reply; 6+ messages in thread
From: wwp @ 2004-07-02 23:10 UTC (permalink / raw)
  To: linux-c-programming

Hello J.,


On Sat, 3 Jul 2004 00:32:27 +0200 (CEST) "J." <mailing-lists@xs4all.nl> wrote:

> Saturday, July 03 00:18:09
> 
> Hello,
> 
> I am trying to convert an IP address to it's eq. in base10
> [homework]. However the outcome from the formula listed below isn't giving
> me the right results.  Is this because I'm using the wrong precision type
> for the `result' variable ? If that's the case what other precision type
> should I use instead ? Right now I can't see the format tree's trhu the
> precision forrest anmore .. ehum.
> 
> #include <stdio.h>
> 
> int main(void) {
>  int var1 = 192; 
>  int var2 = 168;
>  int var3 = 1;
>  int var4 = 10;
>  long int result = 0;
> 
>  result = var1 * (256 ^ 3) + var2 * (256 ^ 2) * var3 * 256 + var4; 
>  printf("%ld\n", value);
> 
>  return 0;
> }
> 
> Your comments and suggestions will be greatly appreciated..

You meant:
 printf("%ld\n", result);
instead of:
 printf("%ld\n", value);

What do you get, what do you expect?


Regards,

-- 
wwp

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: printf format type, precision...
  2004-07-02 22:32 printf format type, precision J.
  2004-07-02 23:10 ` wwp
@ 2004-07-02 23:11 ` Glynn Clements
  2004-07-02 23:19 ` wwp
  2 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2004-07-02 23:11 UTC (permalink / raw)
  To: linux-c-programming


J. wrote:

> I am trying to convert an IP address to it's eq. in base10
> [homework]. However the outcome from the formula listed below isn't giving
> me the right results.  Is this because I'm using the wrong precision type
> for the `result' variable ? If that's the case what other precision type
> should I use instead ? Right now I can't see the format tree's trhu the
> precision forrest anmore .. ehum.
> 
> #include <stdio.h>
> 
> int main(void) {
>  int var1 = 192; 
>  int var2 = 168;
>  int var3 = 1;
>  int var4 = 10;
>  long int result = 0;
> 
>  result = var1 * (256 ^ 3) + var2 * (256 ^ 2) * var3 * 256 + var4; 
>  printf("%ld\n", value);
> 
>  return 0;
> }
> 
> Your comments and suggestions will be greatly appreciated..

In C, the ^ operator performs bitwise exclusive-or (XOR), not
exponentiation.

C doesn't have an exponentiation operator, and even if it did, it
wouldn't make sense to use it for powers of two. Use the left shift
operator (<<) instead.

BTW, the result should be unsigned, and displayed with %u, otherwise
addresses where the topmost byte is >= 128 will result in a negative
number.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: printf format type, precision...
  2004-07-02 22:32 printf format type, precision J.
  2004-07-02 23:10 ` wwp
  2004-07-02 23:11 ` Glynn Clements
@ 2004-07-02 23:19 ` wwp
  2004-07-02 23:36   ` printf format type, precision... Solved J.
  2 siblings, 1 reply; 6+ messages in thread
From: wwp @ 2004-07-02 23:19 UTC (permalink / raw)
  To: linux-c-programming; +Cc: mailing-lists

Hello J.,


On Sat, 3 Jul 2004 00:32:27 +0200 (CEST) "J." <mailing-lists@xs4all.nl> wrote:

> Saturday, July 03 00:18:09
> 
> Hello,
> 
> I am trying to convert an IP address to it's eq. in base10
> [homework]. However the outcome from the formula listed below isn't giving
> me the right results.  Is this because I'm using the wrong precision type
> for the `result' variable ? If that's the case what other precision type
> should I use instead ? Right now I can't see the format tree's trhu the
> precision forrest anmore .. ehum.

See if the following gives what you expect. Please not the () around the <<
operation, try w/o them (you'll see the difference about operator precedence).


#include <stdio.h>

int main(void) {
 int var1 = 192; 
 int var2 = 168;
 int var3 = 1;
 int var4 = 10;
 long unsigned int result = 0;

 result = (var1 << 24) + (var2 << 16) + (var3 << 8) + var4;
 printf("%lu %0lx\n", result, result);

 return 0;
}


Regards,

-- 
wwp

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: printf format type, precision...
  2004-07-02 23:10 ` wwp
@ 2004-07-02 23:29   ` J.
  0 siblings, 0 replies; 6+ messages in thread
From: J. @ 2004-07-02 23:29 UTC (permalink / raw)
  To: linux-c-programming

On Sat, 3 Jul 2004, wwp wrote:

> Hello J.,

Hello wwp ;-)
> On Sat, 3 Jul 2004 00:32:27 +0200 (CEST) "J." <mailing-lists@xs4all.nl> wrote:

<CUT>... question/code </CUT>

> > Your comments and suggestions will be greatly appreciated..
> 
> You meant:
>  printf("%ld\n", result);
> instead of:
>  printf("%ld\n", value);

I am sorry, that was a sloppy mistake from my side while cutting and
pasting the code into the message and making sure the code didn't got
garbled... 

But I am affraid my problem/point still stands, it still prints
the incorrect value.
 
> What do you get, what do you expect?
> 
> Regards,
> 
> -- 
> wwp

Thnkx.. !

J.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: printf format type, precision... Solved..
  2004-07-02 23:19 ` wwp
@ 2004-07-02 23:36   ` J.
  0 siblings, 0 replies; 6+ messages in thread
From: J. @ 2004-07-02 23:36 UTC (permalink / raw)
  To: linux-c-programming

Saturday, July 03 01:32:25

Ohh.. super guys.. That solved it. Thankx for the complete answers and
especially the explanation behind it and how to improve...

I am seeing the operator tree's in the c wood again ;-)

Thankx again.. !

J. 


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-07-02 23:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-07-02 22:32 printf format type, precision J.
2004-07-02 23:10 ` wwp
2004-07-02 23:29   ` J.
2004-07-02 23:11 ` Glynn Clements
2004-07-02 23:19 ` wwp
2004-07-02 23:36   ` printf format type, precision... Solved J.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).