473,377 Members | 1,153 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,377 software developers and data experts.

i++ question

hi:

please take a look at the following code:

int a[3] = {1, 3, 2};
int b[3] = {7, 9, 10};

int i;

i = 0;
a[i++] = b[i++];

for (i = 0; i < 3; i ++)
printf("a[%d]: %d, b[%d]: %d\n", i, a[i], i, b[i]);
what is the output?

I run it under two different platform, linux and irix, and get two
different output. Can someone tell me why?

many thanks,

bo

Nov 14 '05 #1
14 3813
Bo Sun <b0*****@cs.tamu.edu> wrote in
news:Pi*******************************@unix.cs.tam u.edu:
hi:

please take a look at the following code:

int a[3] = {1, 3, 2};
int b[3] = {7, 9, 10};

int i;

i = 0;
a[i++] = b[i++];


You cannot do this! Use two different indexes and avoid the undefined
behavior. Please read the FAQ on this.

--
- Mark ->
--
Nov 14 '05 #2
On 2003-12-18, Bo Sun <b0*****@cs.tamu.edu> wrote:
i = 0;
a[i++] = b[i++];


This is a comp.lang.c frequently asked question. Please read the
answer to question 3.1 in the C-faq.

http://www.eskimo.com/~scs/C-faq/top.html

-- James

--
Bo knows C. Read the C-faq!
Nov 14 '05 #3


Bo Sun wrote:
hi:

please take a look at the following code:

int a[3] = {1, 3, 2};
int b[3] = {7, 9, 10};

int i;

i = 0;
a[i++] = b[i++];
Note^
for (i = 0; i < 3; i ++)
printf("a[%d]: %d, b[%d]: %d\n", i, a[i], i, b[i]);
what is the output?

I run it under two different platform, linux and irix, and get two
different output. Can someone tell me why?


There are no intermediate sequence points in the array assignment for
"a[i++] = b[i++]". See
http://publications.gbdirect.co.uk/c...ce_points.html
for details.

Ed.

Nov 14 '05 #4


Bo Sun wrote:

hi:

please take a look at the following code:

int a[3] = {1, 3, 2};
int b[3] = {7, 9, 10};

int i;

i = 0;
a[i++] = b[i++];

for (i = 0; i < 3; i ++)
printf("a[%d]: %d, b[%d]: %d\n", i, a[i], i, b[i]);

what is the output?

I run it under two different platform, linux and irix, and get two
different output. Can someone tell me why?

many thanks,

bo


THIS IS PROBABLY THE MOST COMMON QUESTION IN THIS NEWSGROUP.
Using the ++ or -- operator on the same variable more than once in the
same expression or statement causes undefined behavior. What did you
think would happen? When should i be incremented?

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 14 '05 #5
Bo Sun <b0*****@cs.tamu.edu> wrote in message news:<Pi*******************************@unix.cs.ta mu.edu>...
hi:

please take a look at the following code:

int a[3] = {1, 3, 2};
int b[3] = {7, 9, 10};

int i;

i = 0;
a[i++] = b[i++];

for (i = 0; i < 3; i ++)
printf("a[%d]: %d, b[%d]: %d\n", i, a[i], i, b[i]);
what is the output?

I run it under two different platform, linux and irix, and get two
different output. Can someone tell me why?

many thanks,

bo


It's important to know how the ++ and -- operators actually work. The
expression

i++

evaluates to the current value of i, and as a side effect the variable
i is incremented by one. However, it is not required that the side
effect be applied immediately, only that it be applied before the next
sequence point (check your handy C reference for an explanation of
sequence points; if it doesn't mention sequence points, chuck it in
favor of a better one). For example, given the statement

k = i++ * j++;

the compiler may defer incrementing i and j until after completing the
multiplication and assignment. Or it may increment i and j
immediately after they are evaluated. Or it may do something else.
Here are two of several possible orders of operation:

Option 1 Option 2
-------- --------
t1 <- i t1 <- i
i <- i + 1 t2 <- j
t2 <- j k <- t1 * t2
j <- j + 1 i <- i + 1
k <- t1 * t2 j <- j + 1

In option 1, the side effects are applied immediately. In option 2,
they're deffered until after all the other operations are done.
Therefore, if you try to apply one of these operators to a variable
more than once between sequence points, there's no telling what you'll
get. The behavior is undefined.
Nov 14 '05 #6
"Mark A. Odell" wrote:
Bo Sun <b0*****@cs.tamu.edu> wrote in
please take a look at the following code:

int a[3] = {1, 3, 2};
int b[3] = {7, 9, 10};
int i;

i = 0;
a[i++] = b[i++];


You cannot do this! Use two different indexes and avoid the
undefined behavior. Please read the FAQ on this.


<nit>
You hit another of my raw spots. s/cannot/may not/
</nit>

Note: He just did it.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7

On Fri, 19 Dec 2003, CBFalconer wrote:

"Mark A. Odell" wrote:
Bo Sun <b0*****@cs.tamu.edu> wrote
a[i++] = b[i++];


You cannot do this!


<nit>
You hit another of my raw spots. s/cannot/may not/
</nit>

Note: He just did it.


Yes, but what's the use of being so vague as to say,
"You *may* not do this"? Any child could tell you he
*may* not do it -- but then again it *may* be the case
that he does do it...
s/may not/should not/ and avoid all ambiguity.
Or s/should not/must not/ and retain the original sense,
at the price of sounding like an English nanny. ;)

-Arthur
Nov 14 '05 #8
JV

"Mark A. Odell" <no****@embeddedfw.com> wrote in message
news:Xn********************************@130.133.1. 4...
Bo Sun <b0*****@cs.tamu.edu> wrote in
news:Pi*******************************@unix.cs.tam u.edu:
hi:

please take a look at the following code:

int a[3] = {1, 3, 2};
int b[3] = {7, 9, 10};

int i;

i = 0;
a[i++] = b[i++];


You cannot do this! Use two different indexes and avoid the undefined
behavior. Please read the FAQ on this.

--
- Mark ->
--

Hi,
if I compile the above code with gcc -Wall -ansi -pedantic I get warning
that the "operation on i may be undefined", which is just fine. But why it
is said that "may be undefined" when C standard says that it is undefined?
And if compiler can detect undefined behaivour, shouldn't it report error,
not warining?
-Jyrki
Nov 14 '05 #9
JV <n.*@n.com> scribbled the following:
"Mark A. Odell" <no****@embeddedfw.com> wrote in message
news:Xn********************************@130.133.1. 4...
Bo Sun <b0*****@cs.tamu.edu> wrote in
news:Pi*******************************@unix.cs.tam u.edu:
> hi:
>
> please take a look at the following code:
>
> int a[3] = {1, 3, 2};
> int b[3] = {7, 9, 10};
>
> int i;
>
> i = 0;
> a[i++] = b[i++];
You cannot do this! Use two different indexes and avoid the undefined
behavior. Please read the FAQ on this.

Hi,
if I compile the above code with gcc -Wall -ansi -pedantic I get warning
that the "operation on i may be undefined", which is just fine. But why it
is said that "may be undefined" when C standard says that it is undefined?
And if compiler can detect undefined behaivour, shouldn't it report error,
not warining?


First, undefined behaviour does not have to be erroneous code.
"Undefined behaviour" means behaviour which the standard does not
define, and does not require the implementation to define. It *allows*
the implementation to define it, however.
Strictly speaking, all OS-specific code causes undefined behaviour.
If it was erroneous code, C would be quite useless in OS-specific
applications, don't you think?
Second, the standard only mandates a diagnostic. It does not mandate
the content of that diagnostic. Therefore "The behaviour is undefined",
"The behaviour may be undefined", "You have roused the nasal daemons",
or "Pee Wee Herman for President in 2004!" are all standard-compliant
diagnostics for a[i++]=b[i++].

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
Nov 14 '05 #10
In article <Pi*******************************@unix.cs.tamu.ed u>,
Bo Sun <b0*****@cs.tamu.edu> wrote:
hi:

please take a look at the following code:

int a[3] = {1, 3, 2};
int b[3] = {7, 9, 10};

int i;

i = 0;
a[i++] = b[i++];

for (i = 0; i < 3; i ++)
printf("a[%d]: %d, b[%d]: %d\n", i, a[i], i, b[i]);
what is the output?

I run it under two different platform, linux and irix, and get two
different output. Can someone tell me why?


Throw away the outputs that you got, then think about the problem and
try to find out what output you expect and why you expect it. If you
have a problem figuring out what the output should be then think about
why there is a problem and you will learn something.
Nov 14 '05 #11
In message <KN******************@news1.nokia.com>
"JV" <n.*@n.com> wrote:
Hi,
if I compile the above code with gcc -Wall -ansi -pedantic I get warning
that the "operation on i may be undefined", which is just fine. But why it
is said that "may be undefined" when C standard says that it is undefined?
Maybe the compiler's not sure that the code actually is undefined behaviour.
That's the likely case for a lot of things declared as "undefined behaviour".
If they were easy to check for at compile time, they would probably have been
made constraints requiring diagnostics.
And if compiler can detect undefined behaivour, shouldn't it report error,
not warining?


Arguably, yes, to improve the quality of implementation. But it could only
do this if it was _certain_ it had seen undefined behaviour, otherwise it
would be in danger of rejecting valid code.

For example, my compiler only gives warnings for such things as "xxx may be
used before being set", which it can't always be certain about, but an error
for "narrow type supplied to va_arg".

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #12
CBFalconer <cb********@yahoo.com> wrote in
news:3F***************@yahoo.com:
You cannot do this! Use two different indexes and avoid the
undefined behavior. Please read the FAQ on this.


<nit>
You hit another of my raw spots. s/cannot/may not/
</nit>

Note: He just did it.


True, for permission "he may not do it" for possible actions "he can do
it" but of course he shouldn't.

--
- Mark ->
--
Nov 14 '05 #13
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
JV <n.*@n.com> scribbled the following:
"Mark A. Odell" <no****@embeddedfw.com> wrote in message
news:Xn********************************@130.133.1. 4...
Bo Sun <b0*****@cs.tamu.edu> wrote in
news:Pi*******************************@unix.cs.tam u.edu:
> hi:
>
> please take a look at the following code:
>
> int a[3] = {1, 3, 2};
> int b[3] = {7, 9, 10};
>
> int i;
>
> i = 0;
> a[i++] = b[i++];

You cannot do this! Use two different indexes and avoid the undefined
behavior. Please read the FAQ on this.

Hi,
if I compile the above code with gcc -Wall -ansi -pedantic I get warning
that the "operation on i may be undefined", which is just fine. But why it
is said that "may be undefined" when C standard says that it is undefined?
And if compiler can detect undefined behaivour, shouldn't it report error,
not warining?


First, undefined behaviour does not have to be erroneous code.
"Undefined behaviour" means behaviour which the standard does not
define, and does not require the implementation to define. It *allows*
the implementation to define it, however.
Strictly speaking, all OS-specific code causes undefined behaviour.
If it was erroneous code, C would be quite useless in OS-specific
applications, don't you think?
Second, the standard only mandates a diagnostic. It does not mandate
the content of that diagnostic. Therefore "The behaviour is undefined",
"The behaviour may be undefined", "You have roused the nasal daemons",
or "Pee Wee Herman for President in 2004!" are all standard-compliant
diagnostics for a[i++]=b[i++].


I don't believe the standard mandates any diagnostic for undefined
behavior in general, or for that particular example in specific.

Of course it allows a diagnostic for virtually anything...

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) fl***@barrow.com
Nov 14 '05 #14
On Thu, 18 Dec 2003 22:36:05 -0500 (EST), in comp.lang.c , "Arthur J.
O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
Yes, but what's the use of being so vague as to say,
"You *may* not do this"? Any child could tell you he
*may* not do it -- but then again it *may* be the case
that he does do it...
I believe "May" as a verb has two meanings.
One of them ,the one that CBF was using, means to permit. One tells a
child "you may not pour coffee in Granny's ear" meaning that you are
forbidden from doing it.

The other, the one you're using, is used to mean that a choice exists,
and the decision is not taken as to which choice is chosen. One tells
a child "If you're good we may go to the park", meaning that a
decision is yet to be taken.

s/may not/should not/ and avoid all ambiguity.
Or s/should not/must not/ and retain the original sense,
at the price of sounding like an English nanny. ;)


Yes'm.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #15

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

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.