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

Home Posts Topics Members FAQ

Need some explanation

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 2795
In article <is************ *************** *****@4ax.com>, Mark McIntyre
<ma**********@s pamcop.net> writes
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.


Main only returns int in a hosted environment.

In free stranding environments there is nothing to return to. So

void main()

Is the only practical real world solution (and quite legal). The entry
point to the C program does not *have* to be called main but for the
majority of debugging tools it needs to be.
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Nov 15 '05 #21
On Thu, 14 Jul 2005 14:35:19 +0100, in comp.lang.c , Chris Croughton
<ch***@keristor .net> wrote:
On Thu, 14 Jul 2005 11:35:18 +0100, Mark McIntyre
<ma**********@s pamcop.net> wrote:
main must return an int - void main() is illegal C code.
It isn't 'illegal'


Illegal is CLC shorthand for "disallowed by the C standard" as I'm
sure you well know.
(the only place that word is used in the standard is
in the context of an "illegal instruction" raising a signal). It is
implementati on defined whether it is permitted in a hosted environment;


This is a very old topic of discussion. The C99 standard requires main
to return an int in a hosted environment. There's no implementation
defined-ness about it, and I believe its now widely agreed that the
final phrase of 5.1.2.2.1 (1) refers to the arguments of main.

--
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 =----
Nov 15 '05 #22
On Thu, 14 Jul 2005 18:20:44 +0100, in comp.lang.c , Chris Hills
<ch***@phaedsys .org> wrote:
Main only returns int in a hosted environment.

In free stranding environments there is nothing to return to


Absolutely. Was the OP writing for a freestanding implementation?

I also note for reference that discussion of freestanding
implementations is generally regarded as offtopic here (or at least
largely pointless), since there are few requirements placed on such
implementations by the standard.

So

--
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 =----
Nov 15 '05 #23
On Thu, 14 Jul 2005 14:23:00 +0000 (UTC), Walter Roberson
<ro******@ibd.n rc-cnrc.gc.ca> wrote:
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, those are applications and can be written in any language. I'm
referring to C code itself, like making it a criminal offence to use
goto or break...

Chris C
Nov 15 '05 #24
On Thu, 14 Jul 2005 08:54:40 -0700, osmium
<r1********@com cast.net> wrote:
"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.


Indeed, even getting them to speak American would be hard <g>...

Chris C
Nov 15 '05 #25
On Thu, 14 Jul 2005 18:20:44 +0100, Chris Hills
<ch***@phaedsys .org> wrote:
Main only returns int in a hosted environment.

In free stranding
Is that like lightweight threads? <g>
environments there is nothing to return to. So

void main()

Is the only practical real world solution (and quite legal). The entry
point to the C program does not *have* to be called main but for the
majority of debugging tools it needs to be.


I don't think I've come across an embedded debugging tool which cared
what the entry point is called, they seem to only need to know the start
address (if that isn't hardwired by the processor). Certainly the
mobile phone systems I've programmed at that level have had all sorts of
names for the entry point (boot, entry, startup, etc.) at a whim of the
original programmer or designer.

However, there are probably loads of debuggers I haven't used, every
processor manufacture seems to have their own favourites...

Chris C
Nov 15 '05 #26
In article <0c************ *************** *****@4ax.com>, Mark McIntyre
<ma**********@s pamcop.net> writes
On Thu, 14 Jul 2005 18:20:44 +0100, in comp.lang.c , Chris Hills
<ch***@phaedsy s.org> wrote:
Main only returns int in a hosted environment.

In free stranding environments there is nothing to return to


Absolutely. Was the OP writing for a freestanding implementation?

I also note for reference that discussion of freestanding
implementation s is generally regarded as offtopic here (or at least
largely pointless), since there are few requirements placed on such
implementation s by the standard.


However there are probably as many free standing implementations as
hosted. Besides who is the authority to say half the C world is off
topic in a C language group.

However I take your point that in SOME areas the freestanding
implementation is unspecified or implementation defined

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys. org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Nov 15 '05 #27
Mark McIntyre <ma**********@s pamcop.net> wrote:
This is a very old topic of discussion. The C99 standard requires main ^^^^^ to return an int in a hosted environment.
Would you please explain this to me, or give me some pointers
to past discussions where I could read about this myself?

I did google a bit, and found no proof of your assertion.
In fact, opinions on this (coming from the top clc gurus)
varied both ways.
There's no implementation
defined-ness about it, and I believe its now widely agreed that the
final phrase of 5.1.2.2.1 (1) refers to the arguments of main.


--
Stan Tobias
mailx `echo si***@FamOuS.Be dBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #28
S.Tobias wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote:
This is a very old topic of discussion. The C99 standard requires main ^^^^^
to return an int in a hosted environment.


Would you please explain this to me,


Well, if you want your program to be portable, you have to return int.
Implementations are allowed to define behaviours for alternative return
types for main (they were in C90 too, but C99 merely makes this explicit),
so you can argue that void main is implementation-defined, but ONLY for
implementations that define it! In other implementations , it remains
undefined, and so it's not unreasonable to argue that the C99 standard
requires programs-intended-to-be-portable to return int.
or give me some pointers
to past discussions where I could read about this myself?
Pick 20 articles at random from comp.lang.c's archives. I should think that
at least 6 of them will be about void main.

I did google a bit, and found no proof of your assertion.
In fact, opinions on this (coming from the top clc gurus)
varied both ways.


The safe thing to do is go with int main, unless your implementation
actually forbids it (eg freestanding).
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #29


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
This is because turbo compiler executes such single line code from
right to left.
i.e., val-- gets first evaluated, next ++val and so on.
val -- prints a '5'(the value of variable val),substract value of 'val'
by 1, so that now val=4
Now ++val gets executed, ++ before val increments val before printing,
so you obtain a '5'.Now val=5
After this -- val gets executed, substracting 1 from val before
printing as said before and you get a '4' at the output.At this point
val=4
So finally val gets printed and you obtain the current value of the
'val' to be 4

It should be noted that though execution takes place from right to
left, printing takes place in the order defined in printf

It should also be noted that void main() is not a good practice, better
use int main() along with a return 0; just before you end the main()
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 #30

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

Similar topics

2
1843
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...
2
2125
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 = )
17
2672
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,
4
10145
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
18
1749
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.
12
2108
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) {
4
1534
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) & "."
4
1889
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...
1
2930
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...
1
2097
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...
0
8680
marktang
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...
0
9169
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...
1
8899
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,...
0
8871
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...
0
7738
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
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...
1
3052
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
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.