473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

union, ternary operator, and C. What a mess!

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: pa******@ukans.edu
Dept. of Political Science http://lark.cc.ukans.edu/~pauljohn
Nov 13 '05 #1
3 4946
"Paul E Johnson" <pa******@ku.edu> wrote in message
news:pa****************************@ku.edu...
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.


This is likely caused by the different sizes of
"long unsigned int" vs. "double".

I suggest moving the (Stochastic)? test to an if(Stochastic) statement.
Then use hard-coded references to newMem.i or newMem.f in the
appropriate "then" or "else" part of the if() statement.

if(Stochastic)
{
printf(..whatever..,newMem.i);
}
else
{
printf(..whatever..,newMem.f);
}

HTH
Nov 13 '05 #2


Paul E Johnson wrote:
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.
Rarely used? I use them all the time...

<snip>

I was thinking that the problem is the pre-processor, but
here is the output from gcc -E, which appears as expected.
Didn't it also give you a warning about the third printf? If not, try it
again with "-Wall" set.
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.

???


Your macro is fine, it's your printf fomatting that's bad. Try using %e
(for "double") instead of %lu (for "long...") in UMACRO= part and you'll
see the difference.

Ed.

Nov 13 '05 #3


Ed Morton wrote:

Didn't it also give you a warning about the third printf?

s/third/second/

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: praba kar | last post by:
Dear All, I am new to Python. I want to know how to work with ternary operator in Python. I cannot find any ternary operator in Python. So Kindly clear my doubt regarding this ...
2
by: Paul E Johnson | last post by:
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...
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
4
by: raiderdav | last post by:
I understand how the ternary operator (question mark - ?) works in an if/else setting, but what does it mean when used as a type? For instance, I'm trying to add a get/set in some existing code,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.