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; 70 2754 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?
#include <stdio.h> void main()
is not a portable standard definition for main, use:
int main(/*int argc, char **argv*/) { int val=5; printf("%d %d %d %d",val,--val,++val,val--);
/*falling off main, is a rather recent addition C99 onwards, AFAIK,
hence*/
return 0; } under turbo compiler its giving 4 4 5 5 and under visual c++ its 5 5 6 5
The order in which the arguments to printf are evaluated is *not* the
same across different compiler/system pairs. 2) How to evaluate following statement int val =5; val =- --val - val-- - --val;
You might like to read the FAQ, and some stuff on side-effects and
sequence points. 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() { 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;
Goto http://www.eskimo.com/~scs/C-faq/s3.html. 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() { 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;
In the first case the behavior is undefined (evaluation order of
function parameters). The statement in (2) is very hard to read and
should (in practice) be rewritten into something more comprehensible.
Moreover:
- `void main()' is not a valid ANSI C signature for the main function.
- You have not included stdio.h
- Your coding style is inconsistent and the indentation is strange to
say the least. 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;
akarl wrote: 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;
In the first case the behavior is undefined (evaluation order of function parameters). The statement in (2) is very hard to read and should (in practice) be rewritten into something more comprehensible.
The second case is also undefined, for the same reason the first one is
(repeated modifications of an object without an intervening sequence
point).
Christian ra*******@gmail .com wrote: 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;
Get your C book or the standard and read up on sequence points
and/or parameter evaluation order. This is elementary. And if you
really mean C++ this is not the place.
--
"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
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?
akarl <fu********@com hem.se> wrote: John Bode wrote: ra*******@gmail .com 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 a simple case like this, perhaps not; but in the general case, yes.
Richard
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
[snip] How come statements like these compile when the result is undefined?
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.
Is it too hard for the compiler to figure out that it results in undefined behavior?
I'm not a compiler writer by trade, so I can't say for sure. In the
obvious case (i = i++), it shouldn't be too hard. But imagine a case
like this:
foo.c
-----------------------
int foo(int *p, int *q)
{
return (*p)++ + ++(*q);
}
main.c
-----------------------
extern int foo(int *p, int *q);
int main(void)
{
int i, *j, *k;
int x;
k = &i;
i = 5;
j = k;
x = foo(j, k);
return 0;
}
Not only is i aliased by pointers, the foo() function is compiled
separately from the main() function. There's simply no way to detect
that i is being modified more than once between sequence points at
compile time.
Situations like that would be impossible to guard against. That's why
the Standard leaves the behavior "undefined" ; there's simply no good
way to catch all instances of this problem at compile time, so no
requirement is placed on the compiler implementor to do so. 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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
| |