473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

if (--n==n)

Hi All,

I have small fragment of code ...
main()
{
int n=10;

if(--n==n)
{
printf("\n Pre");
}
if(n--==n)
{
printf(" Post");
}

}//main
What shall be the output of the above code.

Thanks
Raman Chalotra

Jan 22 '07 #1
11 1402
Raman <ra***********@gmail.comwrote:
Hi All,

I have small fragment of code ...
main()
{
int n=10;

if(--n==n)
{
printf("\n Pre");
}
if(n--==n)
{
printf(" Post");
}

}//main
What shall be the output of the above code.
Undefined behaviour, see section 3 of the c.l.c. FAQ:

http://docs.mandragor.org/files/Prog.../C-faq/s3.html
Jan 22 '07 #2
Raman wrote:
Hi All,

I have small fragment of code ...
main()
{
int n=10;

if(--n==n)
{
printf("\n Pre");
}
if(n--==n)
{
printf(" Post");
}

}//main
What shall be the output of the above code.
It'll set fire to your computer.

You're invoking undefined behaviour in several places. See link below:
<http://c-faq.com/expr/index.html>

Jan 22 '07 #3
santosh said:
Raman wrote:
>>
What shall be the output of the above code.

It'll set fire to your computer.
The Standard does not actually guarantee this.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 22 '07 #4

Raman wrote:
I have small fragment of code ...
Thank you for posting a complete program...
main()
We prefer "int main(void)", but it's only a stylistic thing, really.
{
int n=10;

if(--n==n)
{
printf("\n Pre");
}
if(n--==n)
{
printf(" Post");
}

}//main
Please don't use "//" comments - they're not in the standard that most
of us are working with, and may upset our compilers.
What shall be the output of the above code.
The standard explicitly says this yields undefined behaviour - demons
flying out of your nose or your computer catching fire are unlikely,
but allowed by the standard. It's not guaranteed that this program will
behave consistently on different platforms, or compiled by different
compilers (or with different compiler optimisation).

Empirically this produced "Pre" on my linux workstation, compiled with
some version of GCC. On an AIX server using Xlc (again version
unspecified) I got "Pre Post".

Why do you ask?

Jan 22 '07 #5
Hi,
I wanted to know what you tell( I was not sure ), that it may produce
post as well..
Thanks
Raman Chalotra

ma**********@pobox.com wrote:
Raman wrote:
I have small fragment of code ...

Thank you for posting a complete program...
main()

We prefer "int main(void)", but it's only a stylistic thing, really.
{
int n=10;

if(--n==n)
{
printf("\n Pre");
}
if(n--==n)
{
printf(" Post");
}

}//main

Please don't use "//" comments - they're not in the standard that most
of us are working with, and may upset our compilers.
What shall be the output of the above code.

The standard explicitly says this yields undefined behaviour - demons
flying out of your nose or your computer catching fire are unlikely,
but allowed by the standard. It's not guaranteed that this program will
behave consistently on different platforms, or compiled by different
compilers (or with different compiler optimisation).

Empirically this produced "Pre" on my linux workstation, compiled with
some version of GCC. On an AIX server using Xlc (again version
unspecified) I got "Pre Post".

Why do you ask?
Jan 22 '07 #6
Ico
Raman <ra***********@gmail.comwrote:
Hi,
I wanted to know what you tell( I was not sure ), that it may produce
post as well..
But do you now understand *why* it can produce 'post' as well ?

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 22 '07 #7
ma**********@pobox.com wrote, On 22/01/07 11:12:
Raman wrote:
>I have small fragment of code ...

Thank you for posting a complete program...
>main()

We prefer "int main(void)", but it's only a stylistic thing, really.
<snip>
>}//main

Please don't use "//" comments - they're not in the standard that most
of us are working with, and may upset our compilers.
Actually whether one uses "main()" is not purely stylistic because it is
not valid in the C standard that allows "//" style comments. So any
program such as Raman's that uses "main()" and "//" style comments is
not valid C according to *any* standard.
--
Flash Gordon
Jan 22 '07 #8
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Actually whether one uses "main()" is not purely stylistic
because it is not valid in the C standard that allows "//"
style comments.
Writing "main()" is fine in C99. It's the absence of an explicit
return type (also seen in the example in question) that C99
disallows.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Jan 22 '07 #9
Raman wrote:
Hi,
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Jan 22 '07 #10
Ben Pfaff wrote, On 22/01/07 17:18:
Flash Gordon <sp**@flash-gordon.me.ukwrites:
>Actually whether one uses "main()" is not purely stylistic
because it is not valid in the C standard that allows "//"
style comments.

Writing "main()" is fine in C99. It's the absence of an explicit
return type (also seen in the example in question) that C99
disallows.
The context was someone saying to use "int main(void)" rather than
"main()". Admittedly I could have made it clear it was the lack of
return type that was the problem in C99.
--
Flash Gordon
Jan 22 '07 #11
In article <51*************@mid.individual.net>,
Default Loser <de************@yahoo.comwrote:
....
>Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Get a life!

Jan 28 '07 #12

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

Similar topics

0
by: L Mehl | last post by:
Hello -- A textbox, txtSubsRate, on a form has its DataFormat property set to Percentage. The result is that a user entering a decimal value of ".nn" sees "nn%" in the text box. In an...
2
by: chirs | last post by:
Hi, The color is represented by #RRGGBB format. On a javascript sample, it uses nn#RRGGBB format. What does nn mean? Here is the sample codes: (1) var button = new ItemStyle(22, 1, '&gt;',...
1
by: Paul THompson | last post by:
I have been working for some time to 1) detect tab and shift tab events 2) control the focus on the basis of these events. I have found that I can do this, but continue to have nagging problems. ...
1
by: gsb | last post by:
I am trying to create a script tag: script = document.createElement('script'); ....and assign its script (IE) : script.txt = " ...my script... "; But this does not work in NN. Any help?...
7
by: Michael McClune | last post by:
Is it possible to change a <TD> background color on the fly with NN? There are a long list of members info that I want to alternate the background color of. I have tried calling variation of the...
1
by: PAD | last post by:
I have written a javascript routine that populates a parent document with a series of <iframe>s. Each <iframe> receives its contents from a series of separate smaller HTML files. Of course my NN...
2
by: Andrew Thompson | last post by:
- NN 4.78 rendering woes, links at far left - I am trying to rework an old site, make it valid html and css, improving the x-browser and 'older browser' compatibility. My efforts so far, have...
5
by: Richard Trahan | last post by:
I have an inscrutable problem with window.open. Consider the following code: var graphwin = window.open("","graphWin","height=400,width=400,resizable=yes"); It works perfectly in one place in...
0
by: Vik | last post by:
1. To show the sort order in a datagrid, a label is added to the datagrid header in the ItemCreated event (this method is taken from a book): With lblSorted ..Font.Name = "Webdings" ..Font.Size =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.