473,549 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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("Stochas tic=%d, UMACRO = %lu Stochastic = %d \n",Stochasti c,
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("Stochas tic=%d, UMACRO = %lu Stochastic = %d \n",Stochasti c,
((Stochastic)?n ewMem.i:newMem. f ), Stochastic);
{long unsigned int testval = ((Stochastic)?n ewMem.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
2 3492


Paul E Johnson wrote:
Dear friends in C:

<snip>
We heard you the first time 8-).

Nov 13 '05 #2
In article <pa************ *************** *@ku.edu>,
Paul E Johnson <pa******@ku.ed u> 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("Stochas tic=%d, UMACRO = %lu Stochastic = %d \n",Stochasti c,
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.
}

Nov 13 '05 #3

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

Similar topics

6
3810
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 __________________________________ Yahoo! Messenger
2
2148
by: Claudio | last post by:
hi all i put in this email source code so u can copy and paste to verify strange first : in this example bit size is a BYTE ?! second : in the last printf output is wrong ? ? best regards all
3
4957
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 is called for! Incredible, but true. union mixed
6
2753
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 way assert.h can be implemented without using it? Are these decisions related in anyway?
11
1959
by: jlara | last post by:
Working on an embedded controller, I generally find it convenient to define the following de-referenced pointer: #define portb (*(unsigned char *)(0x03u)) This enable me to write to a port as follows: portb = 0x0f; But when I want to define a register as follows, the code does not
48
2705
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 suggestions?
9
323
by: newbai | last post by:
union { int ival; char ival; }u; main() { u={1,'2'}; printf("%d",sizeof(u)); }
5
3552
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 you something first before i go to my delimma. codes as follows using Mrs. If Else: #!perl/bin/perl use strict; print "Are you sure you...
16
1483
by: mail2sandeepnl | last post by:
hi all what are the applications / examples that requires concatenation of 2 pre-processor variables. i.e applications of " ## " operators in C/C++ i think its only use is in Compiler construction, but how i don't know, pls any one help me. bye sandeep
0
7451
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7475
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6048
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.