setting to zero

B

Ben Bacarisse

Bill Cunningham said:
No Ben. uh-uh. You're not tricking me on this one. That's c99. Been
there done that.

I don't know why you think I am trying to trick you.
 
B

Bill Cunningham

(e) /* foo */ for (int i = 0; i < n; i++)

For some people (and I think Bill is one of them) it solves the problem.
No Ben. uh-uh. You're not tricking me on this one. That's c99. Been
there done that.

Bill
 
B

Bill Cunningham

Fair point. I must admit I didn't realise BC was using a conforming
C99 implementation.

I'm not. Especially when I stick a macro of __STRICT_ANSI__ in my code.

Bill
 
B

Bill Cunningham

Find a way to quote properly. Do it manually if you have to, or
install OE-QuoteFix (Google it), or use a different newsreader.
Do this before posting again.
[snip]

I've been doing it manually. There hasn't been any complaints for awhile.
Maybe I'm screwing it up again.

Bill
 
B

Bill Cunningham

Use better names for your variables. The name "i" is commonly used
for a loop index; you're using it for an array, which makes your code
more difficult to read. "j" and "k" are sometimes used secondary loop
indices; "j" is ok, but you're using "k" as a sum.

I did not know anything about this.
Here's a version of your program with better names for the variables
and no other changes. I've left all your errors in place. It should
now be easier to see at least some of them.

#include <stdio.h>
#define __STRICT_ANSI__

int main(void)
{
int arr[4], i, sum;
sum = 0;
for (i = 0; sum < 4; ++i)
sum = sum + arr;
printf("%d\n", sum);
}


<OT> I have some good news though this is OT. They have decided to take
me off of klonopin. So far I have been able to reduce by a mg. So I'm taking
1 mg 2x a day. If I can get to 1.5 mg a day or less I'm hoping my mind will
come back and I can read and know what I just read and learn some things
including C.

</OT>

Bill
 
B

Barry Schwarz

... I'm getting a huge number when it should be zero.
int main(void)
{
int i[4], j, k;
k = 0;
for (j = 0; k < 4; ++j)
k = k + i[j];
printf("%d\n", k);
}
I know in this case a simple alternative would be
int i[4]={0,0,0,0};

*i type of non-character, i'm not sure if this is portable/right:
memset(i, 0, sizeof i);

This will set every byte of the array i to all bits zero. On most
systems, this will cause each int in the array to have the value 0.
 
S

Seebs

You know that's c99. I use c89. You know if I tried what you intended my
compiler would hang.

It would never in a million years have occurred to me to suspect such a thing.
I'd expect a compiler to error out in such a case, not hang. But even so,
I just assume that most compilers, by now, support mixed declarations and
code and C99/C++ style for loops.

-s
 
B

Ben Bacarisse

Barry Schwarz said:
... I'm getting a huge number when it should be zero.
int main(void)
{
int i[4], j, k;
k = 0;
for (j = 0; k < 4; ++j)
k = k + i[j];
printf("%d\n", k);
}
I know in this case a simple alternative would be

int i[4]={0,0,0,0};

*i type of non-character, i'm not sure if this is portable/right:
memset(i, 0, sizeof i);

This will set every byte of the array i to all bits zero. On most
systems, this will cause each int in the array to have the value 0.

It will have that effect on all systems. The promise does not extend
to floating point objects or pointers (as you know) but it does apply
to all integer types.
 
K

Keith Thompson

Barry Schwarz said:
... I'm getting a huge number when it should be zero.
int main(void)
{
int i[4], j, k;
k = 0;
for (j = 0; k < 4; ++j)
k = k + i[j];
printf("%d\n", k);
}
I know in this case a simple alternative would be

int i[4]={0,0,0,0};

*i type of non-character, i'm not sure if this is portable/right:
memset(i, 0, sizeof i);

This will set every byte of the array i to all bits zero. On most
systems, this will cause each int in the array to have the value 0.

In fact the standard guarantees that, for any integer type,
all-bits-zero is a representation of 0. This guarantee isn't stated
in C90 or C99, but it was added in one of the post-C99 Technical
Corrigenda. (I can look up the specifics later if anyone is
interested.)
 
K

Keith Thompson

Bill Cunningham said:
You know that's c99. I use c89. You know if I tried what you intended my
compiler would hang.

Your compiler would not "hang". At worst, it would report a syntax
error. But if you invoke gcc with "-std=c99", it will support that
feature.

But if you choose to stick to C89/C90, you can certainly do so.
 
B

Ben Bacarisse

Bill Cunningham said:
You know that's c99. I use c89. You know if I tried what you intended my
compiler would hang.

No I don't (because it won't). Anyway, I now know you use only C89.
I am still baffled by why you'd assume I am trying to trick you.
 
L

lovecreatesbeauty

i meant the elements in the array `i' mentioned in this thread:
int main(void)
{
int i[4], j, k;
Nope. The values of *variables* which aren't initialized aren't zero
by default. If something is even partially initialized, though, it is
treated as though it were fully initialized with anything not initialized
explicitly being initialized to zero.

the array `i' isn't static durable, i think only the first element are
initialized in the following, and the rest of them, i[1], i[2], i[3]
are indeterminate.

int main(void)
{
int i[4] = {0};
/* ... */

Please correct me if I am wrong. Thanks.
 
S

Seebs

the array `i' isn't static durable, i think only the first element are
initialized in the following, and the rest of them, i[1], i[2], i[3]
are indeterminate.

int main(void)
{
int i[4] = {0};
/* ... */

Please correct me if I am wrong. Thanks.

As I said in the previous post: Yes, you are wrong.

.... Oh, you probably want some kind of supporting evidence.

6.7.8, paragraph 21:

If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there
are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration.

Or paragraph 19:

The initialization shall occur in initializer list order, each
initializer provided for a particular subobject overriding any
previously listed initializer for the same subobject) all subobjects
that are not initialized explicitly shall be initialized implicitly
the same as objects that have static storage duration.

-s
 
B

Ben Bacarisse

i meant the elements in the array `i' mentioned in this thread:
int main(void)
{
int i[4], j, k;
Nope. The values of *variables* which aren't initialized aren't zero
by default. If something is even partially initialized, though, it is
treated as though it were fully initialized with anything not initialized
explicitly being initialized to zero.

the array `i' isn't static durable, i think only the first element are
initialized in the following, and the rest of them, i[1], i[2], i[3]
are indeterminate.

int main(void)
{
int i[4] = {0};
/* ... */

Please correct me if I am wrong. Thanks.

You are. The poster Seebs (Peter Seebach) is (unsurprisingly)
correct. The wording is complicated, but the key part is 6.7.8 p19:

The initialization shall occur in initializer list order, each
initializer provided for a particular subobject overriding any
previously listed initializer for the same subobject all subobjects
that are not initialized explicitly shall be initialized implicitly
the same as objects that have static storage duration.

A cute fact that {}s may optionally be also used when initialising
objects that are *not* aggregates, so {0} is a "universal"
initialiser:

float i = {0}, *ip = {0}, fa[3] = {0};
 
L

lovecreatesbeauty

the array `i' isn't static durable, i think only the first element are
initialized in the following, and the rest of them, i[1], i[2], i[3]
are indeterminate.
int main(void)
{
int i[4] = {0};
/* ... */
Please correct me if I am wrong. Thanks.

As I said in the previous post: Yes, you are wrong.
6.7.8, paragraph 21:
initialized implicitly the same as objects that have static storage
^^^^^^
Or paragraph 19:
the same as objects that have static storage duration.
^^^^^^
They all mentioned `static'. I can understand that three rest of `a'
and `b' are zero. Array `i' is an auto variable, three rest of `i' are
indeterminate.

int a[4] = {100};
int main(void)
{
int i[4] = {0};
static int b[4] = {1000};
/* ... */
 
B

Ben Bacarisse

the array `i' isn't static durable, i think only the first element are
initialized in the following, and the rest of them, i[1], i[2], i[3]
are indeterminate.
int main(void)
{
int i[4] = {0};
/* ... */
Please correct me if I am wrong. Thanks.

As I said in the previous post: Yes, you are wrong.
6.7.8, paragraph 21:
initialized implicitly the same as objects that have static storage ^^^^^^
Or paragraph 19:
the same as objects that have static storage duration.
^^^^^^
They all mentioned `static'.

Yes, because the standard writer does no want to repeat the complex
wording used to describe the zero initialisation of statics, so the
effect of a partial initialisation is described by referring to the
method used for statics. p19 does, none the less, apply to aggregates
with automatic storage duration.

There are 22 paragraphs in that section (and 17 clarifying examples)
so you probably have to read the whole thing to see the big picture.
I can understand that three rest of `a'
and `b' are zero. Array `i' is an auto variable, three rest of `i' are
indeterminate.

int a[4] = {100};
int main(void)
{
int i[4] = {0};
static int b[4] = {1000};
/* ... */

a[0] == 100 and b[0] == 1000, but every other int object here is
initialised to zero.
 
L

lovecreatesbeauty

the array `i' isn't static durable, i think only the first element are
initialized in the following, and the rest of them, i[1], i[2], i[3]
are indeterminate.
int main(void)
{
int i[4] = {0};
/* ... */
Please correct me if I am wrong. Thanks.
As I said in the previous post: Yes, you are wrong.
6.7.8, paragraph 21:
initialized implicitly the same as objects that have static storage ^^^^^^
Or paragraph 19:
the same as objects that have static storage duration.
^^^^^^
They all mentioned `static'.

Yes, because the standard writer does no want to repeat the complex
wording used to describe the zero initialisation of statics, so the
effect of a partial initialisation is described by referring to the
method used for statics. p19 does, none the less, apply to aggregates
with automatic storage duration.

This I did't know before!
There are 22 paragraphs in that section (and 17 clarifying examples)
so you probably have to read the whole thing to see the big picture.

Thanks Ben, I'll try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top