Hi Everyone. Thanks for the help with the qudratic equation problem...I
didn't think about actually doing the math...whoops. Anyway... I'm
having some trouble getting the following program to work. I want to
output a bit pattern from base 10 input. All I get is a zero after the
input...I've looked over the code but can't see the problem...any ideas?
/* display the bit pattern corresponding to
a signed decimal integer */
#include<stdio. h>
#include<stdlib .h>
int main(void)
{
int a, b, m, count, nbits;
unsigned mask;
/* determine the word size in bits and set the initial mask */
nbits = 8 * sizeof(int);
m = 0x1 << (nbits - 1); /* Place 1 in leftmost position */
/* main loop */
do {
/* read a signed integer */
printf("\n\nEnt er an integer value (0 to stop): ", a);
scanf("%d", &a);
/* output the bit pattern */
mask = m;
for(count = 1; count <= nbits; count++); {
b = (a & mask) ? 1 : 0; /* set display bit on or off */
printf("%x", b); /* print display bit */
if (count % 4 == 0)
printf(" "); /* blank space after ever 4th digit */
mask >>= 1; /* shift mask 1 position to the right */
}
} while (a != 0);
return EXIT_SUCCESS; /* If it's there why not use it people! */
}
Nov 14 '05
47 5291
In article <qs************ @brenda.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.uk> wrote: If you had: if ((c=b)==f(c)) or: if ((a=c)==f(c)) you would have undefined behaviour.
The second one has no undefined behavior.
On Thu, 09 Sep 2004 19:40:51 GMT
CBFalconer <cb********@yah oo.com> wrote: Flash Gordon wrote: ... snip ... If you had: if ((c=b)==f(c))
Yes
or: if ((a=c)==f(c))
No.
you would have undefined behaviour.
In the latter case c is being accessed only to extract its value, not to alter it. Assuming it is not a volatile value.
Sorry, I flipped languages for a moment to one that allows pass by
reference.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
On Thu, 9 Sep 2004, Flash Gordon wrote: so************@ yahoo.com (Mark Piffer) wrote: so************@ yahoo.com (Mark Piffer) wrote... Ok, how about the following: int a,b,c; int f(int);
if( (a=b) == f(c) ) { // undefined behaviour? }
As the order of execution is unspecified (is it?), the evaluation sequence could be:[1] a=b [2] f(c) // sequence point (according to 6.5.2.2/10) [3] == // using the result of [1] and [2]
[...] Can someone enlighten me about this? Either I got ignored or the posting vanished too soon from the servers, but I think the question isn't trivial (ok, for regulars here maybe it is) and certainly not OT. The order of evaluation of a=b and f(c) does not affect the result in this case, so why would it be undefined behaviour?
Because the Standard says so. Dan Pop's response is correct, as usual;
and most of the other replies are wrong. If you're having trouble
figuring out why the Standard says it's UB, it might help to consider the
perfectly reasonable function definition
int f(int x) { a = x; }
(Then again, it might not. ;-)
Admittedly the value of a might not be updated until after the comparison has been done, but the comparison still has to be done against the correct value.
Not if it's undefined. :)
If you had: if ((c=b)==f(c)) or: if ((a=c)==f(c)) you would have undefined behaviour.
Those examples aren't any different from the original example; in fact,
they're just special cases of the original (the first where a:=:c and the
second where b:=:c).
HTH,
-Arthur
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote: so************@ yahoo.com (Mark Piffer) wrote...
if( (a=b) == f(c) ) { // undefined behaviour?
As the order of execution is unspecified (is it?), the evaluation sequence could be:[1] a=b [2] f(c) // sequence point (according to 6.5.2.2/10) [3] == // using the result of [1] and [2]
The order of evaluation of a=b and f(c) does not affect the result in this case, so why would it be undefined behaviour?
Because the Standard says so. Dan Pop's response is correct, as usual; and most of the other replies are wrong. If you're having trouble figuring out why the Standard says it's UB, it might help to consider the perfectly reasonable function definition
int f(int x) { a = x; }
How about this:
int c;
memset(x, c = 0, sizeof x);
In article <84************ **************@ posting.google. com>, ol*****@inspire .net.nz (Old Wolf) writes: int c; memset(x, c = 0, sizeof x);
That looks OK to me. N869 6.5.16p4 says that accessing the result of
the assignment operator *after the next sequence point* causes UB;
that's what made
if ((a=b) == f(c))
undefined (because there's a sequence point after the arguments to f
are evaluated). In your example, the result of the assignment
operator (in "c = 0") is accessed before the sequence point that
occurs when all of memset's arguments have been evaluated, because
accessing it is part of evaluating memset's arguments; hence no UB.
So, curiously enough,
if ((a=b) == c)
is legal, but eg
if ((a=b) == (c,d))
is not, because of the sequence point caused by evaluating the first
operand of the comma operator.
OK, now someone should point out where I got it wrong...
(I'm glad Mark re-posted his question. This was a tricky one, and
I certainly hadn't noticed it before.)
--
Michael Wojcik mi************@ microfocus.com
The lark is exclusively a Soviet bird. The lark does not like the
other countries, and lets its harmonious song be heard only over the
fields made fertile by the collective labor of the citizens of the
happy land of the Soviets. -- D. Bleiman
In article <ch*********@ne ws3.newsguy.com >, mw*****@newsguy .com (Michael Wojcik) wrote: In article <84************ **************@ posting.google. com>, ol*****@inspire .net.nz (Old Wolf) writes: int c; memset(x, c = 0, sizeof x);
That looks OK to me. N869 6.5.16p4 says that accessing the result of the assignment operator *after the next sequence point* causes UB; that's what made
if ((a=b) == f(c))
undefined (because there's a sequence point after the arguments to f are evaluated). In your example, the result of the assignment operator (in "c = 0") is accessed before the sequence point that occurs when all of memset's arguments have been evaluated, because accessing it is part of evaluating memset's arguments; hence no UB.
So, curiously enough,
if ((a=b) == c)
is legal, but eg
if ((a=b) == (c,d))
is not, because of the sequence point caused by evaluating the first operand of the comma operator.
OK, now someone should point out where I got it wrong...
That has been discussed to some length in comp.std.c.
You are not "accessing" the value of the assignment operator. You can
only "access" lvalues. The result of (a=b) is not an lvalue. You can use
it without accessing it.
In article <ch************ *************** ******@slb-newsm1.svr.pol. co.uk>, Christian Bau <ch***********@ cbau.freeserve. co.uk> writes: You are not "accessing" the value of the assignment operator. You can only "access" lvalues. The result of (a=b) is not an lvalue. You can use it without accessing it.
Wouldn't that render N869 6.5.16p4 meaningless? It reads:
If an attempt is made to modify the result of an assignment
operator or to access it after the next sequence point, the
behavior is undefined.
If "access" only applies to lvalues, and the result of an assignment
operator is never an lvalue, then this statement describes a case
that can't occur.
Was this corrected in the final version of C99?
--
Michael Wojcik mi************@ microfocus.com
Thus, the black lie, issuing from his base throat, becomes a boomerang to
his hand, and he is hoist by his own petard, and finds himself a marked man.
-- attributed to a "small-town newspaper editor in Wisconsin"
In article <ci********@new s2.newsguy.com> , mw*****@newsguy .com (Michael Wojcik) wrote: In article <ch************ *************** ******@slb-newsm1.svr.pol. co.uk>, Christian Bau <ch***********@ cbau.freeserve. co.uk> writes: You are not "accessing" the value of the assignment operator. You can only "access" lvalues. The result of (a=b) is not an lvalue. You can use it without accessing it.
Wouldn't that render N869 6.5.16p4 meaningless? It reads:
If an attempt is made to modify the result of an assignment operator or to access it after the next sequence point, the behavior is undefined.
If "access" only applies to lvalues, and the result of an assignment operator is never an lvalue, then this statement describes a case that can't occur.
Please read the start of this thread again where I gave an example. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christian Stork |
last post by:
Hello everybody,
I am using Python to prototype a compression algorithm for tree-shaped
data, e.g., XML data or abstract syntax trees. I use a variant of the
visitor design pattern--called weaver/yarn pattern--in order to
traverse the tree that is being compressed or decompressed. I do not
know of any other interesting application of the weaver/yarn pattern,
but it seems like a very suitable subject of study for different...
|
by: Andy Read |
last post by:
Hello all,
I have the requirement to produce source code that produces an object
hierarchy.
Example:
Root
|
Folder 1
|
by: Medi Montaseri |
last post by:
Hi,
Given a collection of similar but not exact entities (or products)
Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern
to provide a common interface to these products. So I shall have an
Abstract Base called 'Car' implemented by Toyota, Ford, and Buick.
Further I'd like to enable to client to say
Car *factory;
|
by: Omer van Kloeten |
last post by:
The Top Level Design:
The class Base is a factory class with a twist. It uses the Assembly/Type
classes to extract all types that inherit from it and add them to the list
of types that inherit from it. During run time, using a static method, the
class creates an instance of the derived class using the Activator class and
returns it. This design pattern is very similar to the design pattern
applied by the Assembly class.
The twist is...
|
by: shonend |
last post by:
I am trying to extract the pattern like this :
"SUB: some text LOT: one-word"
Described, "SUB" and "LOT" are key words; I want those words,
everything in between and one word following the "LOT:". Source text
may contain multiple "SUB: ... LOT:" blocks.
For example this is my source text:
| |
by: Krivenok Dmitry |
last post by:
Hello All!
I am trying to implement my own Design Patterns Library.
I have read the following documentation about Observer Pattern:
1) Design Patterns by GoF
Classic description of Observer. Also describes implementation
via ChangeManager (Mediator + Singleton)
2) Pattern hatching by John Vlissides
Describes Observer's implementation via Visitor Design Pattern.
3) Design Patterns Explained by Alan Shalloway and James Trott
|
by: konrad Krupa |
last post by:
I'm not expert in Pattern Matching and it would take me a while to come up
with the syntax for what I'm trying to do.
I hope there are some experts that can help me.
I'm trying to match /d/d/d/s/d/d in any text.
There could be spaces in front or after the pattern (the nnn nn could be
without spaces also) but it shouldn't pick it up in case like this
1234 56768
|
by: =?Utf-8?B?QWFyb24=?= |
last post by:
Hi,
I'm having a tricky problem where I want to accept a regular expression
pattern from user input but can't get teh escape characters to be prcoessed
correctly. If I take the same pattern and declare it in code with a
preceeding @ character it works fine.
To get the pattern to work from teh suer all \ have to be escaped, e.g.
instead of \d a user would have to enter \\d.
|
by: George Sakkis |
last post by:
I have a situation where one class can be customized with several
orthogonal options. Currently this is implemented with (multiple)
inheritance but this leads to combinatorial explosion of subclasses as
more orthogonal features are added. Naturally, the decorator pattern
comes to mind (not to be confused with the the Python meaning of
the term "decorator").
However, there is a twist. In the standard decorator pattern, the
decorator...
|
by: Slaunger |
last post by:
Hi all,
I am a Python novice, and I have run into a problem in a project I am
working on, which boils down to identifying the patterns in a sequence
of integers, for example
..... 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6
1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6
1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6
1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |