Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Union, ternary operator, Macro, printf don't cooperate for me. Help?

Reply
Thread Tools

Union, ternary operator, Macro, printf don't cooperate for me. Help?

 
 
Paul E Johnson
Guest
Posts: n/a
 
      10-17-2003

Dear friends in C:

I'm completely baffled by this problem I have with
printf statements and conditional operators in C.
I'm using gcc-3.3.

I have a project where the rarely used Union type
is called for! Incredible, but true.

union mixed
{
long unsigned int i;
double f;
} newMem;


It is no trouble to retrieve the values with newMem.i or newMem.f.
However, I want to make the choice between the two automatic by writing a
macro like this, which keys on the value of a variable "Stochastic":

#define UMACRO(var) ((Stochastic) ? var.i : var.f )

So if Stochastic ==1 then I want to get back newMem.i,
otherwise I want newMem.f.

I get this psychotic behavior. WHen the macro UMACRO is in a
printf statement, things go completely berserk!

Here's a code snip:

printf("newMem = %lu \n", newMem.i);
printf("Stochastic=%d, UMACRO = %lu Stochastic = %d \n",Stochastic,
UMACRO(newMem), Stochastic);
{
long unsigned int testval = UMACRO(newMem);
printf("testval %lu\n", testval);
}


And here's the output it produces when Stochastic==1 and
newMem.i = 97:

newMem = 97
Stochastic=1, UMACRO = 0 Stochastic = 1079525376
testval 97

When you look at the ouptut in line 2, you figure UMACRO
does not work at all, and it does something horrible affect
the printout of the second Stochastic in that line. However,
note the 3rd line, which is produced by first retrieving the value from
the union and then printing it out. That is correct.


I was thinking that the problem is the pre-processor, but
here is the output from gcc -E, which appears as expected.

printf("newMem = %lu \n", newMem.i);
printf("Stochastic=%d, UMACRO = %lu Stochastic = %d \n",Stochastic,
((Stochastic)?newMem.i:newMem.f ), Stochastic);
{long unsigned int testval = ((Stochastic)?newMem.i:newMem.f );
printf("testval %lu\n", testval);}

So maybe the ternary conditional is not working in the
right hand side of the printf? Is that a known thing? And
why does it make the output of the second Stochastic in line 2 turn into
crapola 1079525376.

???

--
Paul E. Johnson email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Dept. of Political Science http://lark.cc.ukans.edu/~pauljohn
 
Reply With Quote
 
 
 
 
Ed Morton
Guest
Posts: n/a
 
      10-17-2003


Paul E Johnson wrote:

> Dear friends in C:

<snip>
We heard you the first time .

 
Reply With Quote
 
 
 
 
Christian Bau
Guest
Posts: n/a
 
      10-17-2003
In article <(E-Mail Removed)>,
Paul E Johnson <(E-Mail Removed)> wrote:

> #define UMACRO(var) ((Stochastic) ? var.i : var.f )
>
> So if Stochastic ==1 then I want to get back newMem.i,
> otherwise I want newMem.f.
>
> I get this psychotic behavior. WHen the macro UMACRO is in a
> printf statement, things go completely berserk!


What is the type of ((Stochastic) ? var.i : var.f) ?

The type of var.i is int.
The type of var.f is double.

Whatever value is picked depending on the value of Stochastic, will then
be converted according to the "usual arithmetic conversion"s to type
double, and that is the type of result.

> Here's a code snip:
>
> printf("newMem = %lu \n", newMem.i);
> printf("Stochastic=%d, UMACRO = %lu Stochastic = %d \n",Stochastic,
> UMACRO(newMem), Stochastic);


%lu is the wrong format to print a double value. You get undefined
behavior, which causes the rather interesting output. Use a double
format instead.

> {
> long unsigned int testval = UMACRO(newMem);
> printf("testval %lu\n", testval);


Here you convert the double value to unsigned long, so you use %lu
to print an unsigned long value. Everything is fine here. If var.f had a
fractional part and Stochastic is zero then the fractional part would be
lost.

> }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get os.system () call to cooperate on Windows Anthra Norell Python 9 10-28-2009 03:37 PM
WE SUGGEST TO COOPERATE uab.parra@gmail.com Computer Support 38 05-06-2006 11:22 AM
806 refusing to cooperate. Sol Cisco 10 04-27-2006 05:42 AM
Dell won't cooperate with Del Maybe Computer Support 0 12-29-2005 03:09 AM
Business opportunity: Cooperate with us in the field of Audio&Video products! Jenny Jiang DVD Video 0 01-13-2005 08:21 AM



Advertisments