hello,
1) First how following program get executed i mean how output is
printed and also why following program gives different output in Turbo
C++ compiler and Visual c++ 6 compiler?
void main()
{
int val=5;
printf("%d %d %d %d",val,--val,++val,val--);
}
under turbo compiler its giving
4 4 5 5
and under visual c++ its
5 5 6 5
2) How to evaluate following statement
int val =5;
val =- --val - val-- - --val;
Nov 15 '05
70 2796
akarl wrote: 2) How to evaluate following statement int val =5; val =- --val - val-- - --val;
How come statements like these compile when the result is undefined? Is it too hard for the compiler to figure out that it results in undefined behavior?
In general, yes. (I believe GCC spots some simple cases.)
Consider:
... ++*bill + ++*ben ...
This is undefined if bill and ben point to the same location,
but well-defined [assuming no overflow ...] if they don't.
--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
John Bode:
.... Well, "undefined behavior" basically means the compiler is free to handle the problem in any way it wants to. As soon as the Standard mandates that the compiler issue a diagnostic or abort, the behavior is no longer undefined.
If the undefined behavior is also a constraint violation a diagnostic
must be issued. (3.4.3#2, 5.1.1.3)
Jirka
akarl <fu********@com hem.se> writes: [...for something like x[i++] = i...] How come statements like these compile when the result is undefined? Is it too hard for the compiler to figure out that it results in undefined behavior?
Sufficiently new versions of GCC will often warn about statements
like this.
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
akarl wrote: John Bode wrote:
ra*******@gmail .com wrote:
<snip> 2) How to evaluate following statement int val =5; val =- --val - val-- - --val;
How come statements like these compile when the result is undefined? Is it too hard for the compiler to figure out that it results in undefined behavior?
In these simple cases it is reasonably obvious there is undefined
behaviour. However, with
val = (*ptra)-- - --(*ptrb)
How can the compiler reliably determine whether there is undefined
behaviour or not?
So the standard does not require that compilers diagnose undefined
behaviour.
However, some compilers *will* diagnose *some* instances of undefined
behaviour if invoked with the correct switches. For example, the with
gcc -Wsequence-point catches a number of these cases, but it is not perfect.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
akarl wrote:
.... snip ... How come statements like these compile when the result is undefined? Is it too hard for the compiler to figure out that it results in undefined behavior?
Why should it? It doesn't have to, because things are
syntactically correct. You are always allowed to use a safer
language, such as Pascal or Ada, if you wish. They have been
designed to have the necessary redundancy to detect such things. C
was designed to replace assembly language.
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
akarl wrote: John Bode wrote:
ra*******@gmail .com wrote:
hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? It's because you are invoking undefined behavior in both cases. Read the following: http://www.eskimo.com/~scs/C-faq/s3.html
The expression i++ evaluates to the current value of i; the side affect (i incremented by 1) is applied sometime before the next sequence point, but it need not be immediately after the expression is evaluated.
Similarly for ++i; it evaluates to the current value of i + 1, but i is not necessarily incremented immediately.
There is no requirement on when the side affects are applied other than it must happen before the next sequence point.
For example, given the statement
i = j++ + ++k;
the following sequence is possible:
t1 <- j t2 <- k + 1 i <- t1 + t2 j <- j + 1 k <- k + 1
Same thing applies to your printf() statement; there's no sequence point between the autoincrement/decrement expressions, so the behavior is undefined, meaning the compiler can do anything it wants.
void main() You mean int main(void). Strictly speaking, "void main()" is an error.
{ int val=5; printf("%d %d %d %d",val,--val,++val,val--); } under turbo compiler its giving 4 4 5 5 and under visual c++ its 5 5 6 5
2) How to evaluate following statement int val =5; val =- --val - val-- - --val;
How come statements like these compile when the result is undefined? Is it too hard for the compiler to figure out that it results in undefined behavior?
The OP's compiler, Turbo C, shipped in 1989 and isn't exactly the
pinnacle of modern compiler technology (no offense to the Borland
engineers who worked hard on it). Modern compilers will give much
better diagnostics.
To wit, gcc 4 catches all the errors in that code when you turn on
maximum warnings.
void main() {
int val=5;
printf("%d %d %d %d",val,--val,++val,val--);
}
cc -O2 -W -Wall test.c
test.c:2: warning: return type of 'main' is not 'int'
test.c: In function 'main':
test.c:4: warning: implicit declaration of function 'printf'
test.c:4: warning: incompatible implicit declaration of built-in
function 'printf'
test.c:4: warning: operation on 'val' may be undefined
test.c:4: warning: operation on 'val' may be undefined
test.c:4: warning: operation on 'val' may be undefined
-Peter
--
Pull out a splinter to reply.
On 8 Jul 2005 03:11:03 -0700, in comp.lang.c , ra*******@gmail .com
wrote: hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? void main()
main must return an int - void main() is illegal C code.
(snip example of undefined behaviour which is explained in the FAQ)
2) How to evaluate following statement int val =5; val =- --val - val-- - --val;
This is a FAQ. Please read the FAQ, starting with 3.1.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
On Thu, 14 Jul 2005 11:35:18 +0100, Mark McIntyre
<ma**********@s pamcop.net> wrote: On 8 Jul 2005 03:11:03 -0700, in comp.lang.c , ra*******@gmail .com wrote:
hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? void main()
main must return an int - void main() is illegal C code.
It isn't 'illegal' (the only place that word is used in the standard is
in the context of an "illegal instruction" raising a signal). It is
implementation defined whether it is permitted in a hosted environment;
in a freestanding environment it may not even describe the entry point
of the program, and if it does whether it is a valid definition is
implementation defined.
I do hope that no government makes certain C code illegal...
Chris C
In article <sl************ ******@ccserver .keris.net>,
Chris Croughton <ch***@keristor .net> wrote:
:I do hope that no government makes certain C code illegal...
Cryptography, DVD decoders, viruses...
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
"Chris Croughton" writes: I do hope that no government makes certain C code illegal...
Good luck on your campaign to get programmers to speak English. You should
not expect quick results. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Carolyn Gill |
last post by:
I have already created an asp login/database for a learning/quiz section on
a small site. There will be multiple quizzes through the site and what I
need now would be help: tutorials or advice that a complete novice can
understand/follow to create the following:
A small simple quiz--for now each has just one question with 5 multi-choice
buttons. On submit should send the answer to A) a database (using access for
now) to track scoring...
|
by: Susan Bricker |
last post by:
Greetings. Before I begin, I have been stuck on this problem for about a
5 days, now. I have tried and just seem to be not getting anywhere. I
know that the explanation is lengthy, but I am a relative newcomer to
Access and need to be methodical until it become more familiar. Thanks
in advance for your help.
I have a form with a subform that has a subform.
form: frmEvents
subform: sfrmTrialInfo (controlname = )
|
by: Hazz |
last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender'
line of code need to be there in this event handler?
Isn't cboFont passed via the managed heap, not the stack, into this
cboFont_DrawItem event handler? Why does it need to be cast? -hazz
,.................
cboFont.Items.AddRange(FontFamily.Families);
}
private void cboFont_DrawItem(object sender,
|
by: usl2222 |
last post by:
Hi folks,
I appreciate any assistance in the following problem:
I have a form with a bunch of dynamic controls on it. All the controls
are dynamically generated on a server, including all the validators.
The user enters the data, presses OK. My OK button is dynamically
generated as well, with some code-behind logic in
|
by: Susan Rice |
last post by:
I'm comparing characters via
return(str1 - str2);
and I'm having problems with 8-bit characters being treated as signed
instead of unsigned integers. The disassembly is using
movsx eax,byte ptr
to load my character in to EAX register. I need it to use movzx.
| |
by: jacob navia |
last post by:
Hi
I am writing this tutorial stuff again in the holidays and I came across
this problem:
The "width" field in printf is a minimum width. Printf will not truncate
a field. for instance:
#include <stdio.h>
int main(void)
{
|
by: dismantle |
last post by:
Hi all,
this is my 3rd week in studying VB codes and i came across with this codes
from a online tutorial about classes.
Public Function MiddleInitial() As String
MiddleInitial = Left$(middleNameValue, 1)
End Function
Public Function MiddleInitial(ByVal period As Boolean) As String
MiddleInitial = Left$(middleNameValue, 1) & "."
|
by: adam_kroger |
last post by:
BRIEF EXPLANATION:
I have 6 TextBoxes named LIS1, LIS2, LIS3, ... LIS6. I want to be
able to reference them using a For/Next loop and either read ot write
to them. In VBA I would use something like this:
for i = 1 to 6
me.controls("LIS" & i).Value = ""
next i
Nedless to say, the Controls("LIS" & i).text doesn't work...
|
by: vikjohn |
last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified CGI application misbehaved by not returning a complete set of HTTP headers. The scripts are very long but here are the opening statements:
The One that works ....
#!C:\Perl\bin\perl.exe
# openresolver.cgi
#
# OpenResolver - a CGI script for...
|
by: javabeginner123 |
last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is the description:
The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life points, a name, and a bonus factor that is used in special occasions.
In this initial phase of the...
|
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...
| |
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: 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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: 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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |