473,698 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validity of several ++operators on the same line

Hello,

I am wondering of the validity of those lines, not meaning i'm using
them, but i cant point out in the norm if they are legal, and in such a
case the expected behaviour.

int a = 2;
int b = (++a) * ((++a)+2);
printf( "%d %d", ++b, ++b );
They compiled in Commeau, but that doesn't mean it's legal :)
Any help or suggestion is welcome!
Thanks,

/kufa

Jan 16 '07 #1
11 1324
On Jan 16, 1:25 pm, "Kufa" <pota...@gmail. comwrote:
Hello,

I am wondering of the validity of those lines, not meaning i'm using
them, but i cant point out in the norm if they are legal, and in such a
case the expected behaviour.

int a = 2;
int b = (++a) * ((++a)+2);
printf( "%d %d", ++b, ++b );

They compiled in Commeau, but that doesn't mean it's legal :)
Any help or suggestion is welcome!
No, they are not legal. If in doubt just try to evaluate the expression
in your head. Remember that there are no rules that say the just
because A comes before B in an expression A will be evaluated first.

--
Erik Wikström

Jan 16 '07 #2
Kufa wrote:
I am wondering of the validity of those lines, not meaning i'm using
them, but i cant point out in the norm if they are legal, and in such a
case the expected behaviour.

int a = 2;
int b = (++a) * ((++a)+2);
printf( "%d %d", ++b, ++b );
The behavior is undefined as per clause [5/4] of the standard.
Best

Kai-Uwe Bux
Jan 16 '07 #3
No, they are not legal. If in doubt just try to evaluate the expression
in your head. Remember that there are no rules that say the just
because A comes before B in an expression A will be evaluated first.
Well i'd think this is legal with undefined behaviour. Can you point me
somewhere in the norm that'd say it's not legal, really can't find
anything.

Thanks,

/kUfa

Jan 16 '07 #4
Kufa wrote:
Hello,

I am wondering of the validity of those lines, not meaning i'm using
them, but i cant point out in the norm if they are legal, and in such a
case the expected behaviour.

int a = 2;
int b = (++a) * ((++a)+2);
This one has undefined behavior, because a is modified twice without a
sequence point in between.
printf( "%d %d", ++b, ++b );
Undefined too, similar to the previous one.
They compiled in Commeau, but that doesn't mean it's legal :)
Well, it's not a syntacital error. A compiler doesn't need to complain about
it.
Jan 16 '07 #5
So to sum up and check i did not misunderstand you, it's legal, but
undefined behaviour ?

Thanks for your answers!

/Kufa

Jan 16 '07 #6
"Kufa" <po*****@gmail. comwrote in news:1168952389 .509578.101930@
38g2000cwa.goog legroups.com:
So to sum up and check i did not misunderstand you, it's legal, but
undefined behaviour ?
That depends on what you define as "legal". In my definition, "legal" code
doesn't invoke Undefined Behaviour. If by "legal" you mean "syntactica lly
correct", then yes it's legal.
Jan 16 '07 #7
Kufa wrote:
So to sum up and check i did not misunderstand you, it's legal, but
undefined behaviour ?

Thanks for your answers!

/Kufa
Kai-Uwe Bux already told you where to look in the standard. 5/4.

If you are unfamiliar with that notation, it means paragraph 4 of
section 5, not section 5.4.
Jan 16 '07 #8

Kufa wrote:
No, they are not legal. If in doubt just try to evaluate the expression
in your head. Remember that there are no rules that say the just
because A comes before B in an expression A will be evaluated first.

Well i'd think this is legal with undefined behaviour.
"Undefined behavior" means the program is not well formed, hense
illegal.

Jan 16 '07 #9
Noah Roberts wrote:
Kufa wrote:
>>No, they are not legal. If in doubt just try to evaluate the expression
in your head. Remember that there are no rules that say the just
because A comes before B in an expression A will be evaluated first.
Well i'd think this is legal with undefined behaviour.

"Undefined behavior" means the program is not well formed, hense
illegal.
Does that mean I'll be arrested for writing code like that?

Seriously, according to the standard, programs are well-formed or
ill-formed, and some well-formed programs have undefined behavior. It
doesn't say anything about legal or illegal. As always, using undefined
terms leads to confusion.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jan 16 '07 #10

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

Similar topics

5
2841
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
7
1464
by: Michael Sgier | last post by:
Hi folks class X { private: CTexture *modelTex; // texture data } CTexture* X::Load()
16
3374
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data of the program till the last byte. Here we find static tables, global context pointers, etc. This are the global variables of the program.
6
8977
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32 programming in straight C, the Win32 API sends windows messages to applications with various settings stored in bit fields, in which case changing the settings of the message is a matter of using the bitwise operators. This was a good practical example of...
6
2737
by: Protoman | last post by:
I'm writing a program to calc truth tables of arguments to prove that they are logically valid. Currently, I have to tell the program to print a truth table, and check it by hand to prove it's valid. I'm tired of doing this. I need to write a fn that, given three functions/one function and two variables or anyother combo, run through all four rows, and determine if it's valid. An argument is invalid iff any row has true premises and a...
33
4544
by: a | last post by:
Hi, I have a pointer that points to an unknown heap memory block, is it possible to check the pointer + 3 is valid or not? If it is impossible, how can I do the check? Thanks
9
1645
by: Andy Dingley | last post by:
Here's a chunk of a longer piece of punditry I'm working on, re: the choices between doctypes for authoring and the State of the Union for validity. I've got a bucketload of nasty code and a bunch of developers who need education on validation, validity and all the usual good stuff. In particular it's a problem of triage on the legacy code: what's correctable now, what's going to need serious work, what's going to be a total re-write. ...
18
2633
by: Zach | last post by:
Can someone list the various macro operators and what they mean. Came across a function macro: #define max(a, b) ((a)>(b)?(a):(b)) What does "?" amd ":" mean in this statement? Zach
9
2973
by: xhe | last post by:
Hi, I need to program to check the validity of IP address through PHP Initially I used this one: $url="http://www.ntc.gov.au/ViewPage.aspx? page=A02400304500100020"; $fp=fopen($url,"r"); if(!$fp) {
0
8601
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,...
0
9156
Oralloy
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...
0
9021
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8860
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 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...
1
6518
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1998
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.