Help with code snippet

C

cougre.j

I've got a take home final for my computer class and it has a bonus
question concerning
some programming that we didn't have time to cover. I've already got
an A in the class, so
it won't affect my grade one way or another, however my obsessive/
compulsive tendencies
just can't let it go. Could I get some help in explaining what this
code snippet does;

char foo[8]=" bcd f\n\0";
int i=2;

while (foo[i++]) putchar(foo);

Thanks in advance,
Cougar
 
R

Robert Bauck Hamar

I've got a take home final for my computer class and it has a bonus
question concerning
some programming that we didn't have time to cover. I've already got
an A in the class, so
it won't affect my grade one way or another, however my obsessive/
compulsive tendencies
just can't let it go. Could I get some help in explaining what this
code snippet does;

char foo[8]=" bcd f\n\0";
int i=2;

while (foo[i++]) putchar(foo);


Why don't you try it?
 
C

Christopher.E.Sanborn

I've got a take home final for my computer class and it has a bonus
question concerning
some programming that we didn't have time to cover. I've already got
an A in the class, so
it won't affect my grade one way or another, however my obsessive/
compulsive tendencies
just can't let it go. Could I get some help in explaining what this
code snippet does;
char foo[8]=" bcd f\n\0";
int i=2;
while (foo[i++]) putchar(foo);


Why don't you try it?


Well, I don't have access to C so I can't try it. From what I've been
able to glean from the book is that its declaring an array of 8
integers
and increasing it by 2 during each loop. I don't understand the 'bcd'
part.
Can someone give me a hand?
 
C

cringecoder

I've got a take home final for my computer class and it has a bonus
question concerning
some programming that we didn't have time to cover. I've already got
an A in the class, so
it won't affect my grade one way or another, however my obsessive/
compulsive tendencies
just can't let it go. Could I get some help in explaining what this
code snippet does;

char foo[8]=" bcd f\n\0";
int i=2;

while (foo[i++]) putchar(foo);

Thanks in advance,
Cougar


Declares an 8-character wide array, and puts in it ' ', 'b', 'c', 'd',
' ', 'f', '\n', '\0' ('\n' is newline, '\0' is the null terminating
character)
Then int i is declared, and later is used as an index. It starts at
position two (the third character, c, in the array foo)

while(foo[i++]) putchar(foo);

Is essentially the same as...

while(foo)
{
i++;
putchar(foo);
}

The null terminating character ('\0') is the only character that's
zero as an integer and will terminate the loop. So , while the
character at the current index is not the null-terminator, and after
incrementing the index, print the character to stdout.
It evaluates to true for 'c', then is incremented, and prints out 'd'.
Then it evaluates true for 'd' and prints the next character, a space.
Then it evaluates true for the space and prints the next character,
'f', etc... Until it finally evaluates false for '\0' (In C, zero is
considered false and anything else is considered true).

Did this help at all? And good job getting an A!

-Dan
 
M

Marcus Kwok

char foo[8]=" bcd f\n\0";

Declares an 8-character wide array, and puts in it ' ', 'b', 'c', 'd',
' ', 'f', '\n', '\0' ('\n' is newline, '\0' is the null terminating
character)

When declaring a string literal, it automatically adds the null
terminator to the end. Therefore, when I try to include the line above
in a program, VS 2005 tells me that the array bounds overflow, since it
is trying to put {' ', 'b', 'c', 'd', ' ', 'f', '\n', '\0', '\0'} (which
is 9 characters) into an array that can hold only 8 characters.
 
J

Juha Nieminen

char foo[8]=" bcd f\n\0";

Declares an 8-character wide array, and puts in it ' ', 'b', 'c', 'd',
' ', 'f', '\n', '\0' ('\n' is newline, '\0' is the null terminating
character)

Isn't " bcd f\n\0" actually a const char[9]? What happens if you
initialize a char[8] with it? Is that valid?
 
R

Robert Bauck Hamar

I've got a take home final for my computer class and it has a bonus
question concerning
some programming that we didn't have time to cover. I've already got
an A in the class, so
it won't affect my grade one way or another, however my obsessive/
compulsive tendencies
just can't let it go. Could I get some help in explaining what this
code snippet does;
char foo[8]=" bcd f\n\0";
int i=2;
while (foo[i++]) putchar(foo);


Why don't you try it?

Well, I don't have access to C so I can't try it.

This is a C++ newsgroup, so you don't need C. ;-)
From what I've been
able to glean from the book is that its declaring an array of 8
integers

Yes. The integers are of type char.

Then this array is initialised with nine elements, which is an error. You
are not allowed to initialise an array with more elements than it can hold.
Thus, the answer to your question: It does not compile.
and increasing it by 2 during each loop.

You can't increase an array.
I don't understand the 'bcd'
part.

bcd stands in between the double quotes, so those characters are part of a
string literal: " bcd f\n\0".
Can someone give me a hand?

If we change your example to this:
#include <stdio.h>

int main() {
char foo[/*look nothing*/] = " bcd f\n\0";
int i=2;
while (foo[i++]) putchar(foo);
}

then

char foo[/*look nothing*/] = " bcd f\n\0";

defines foo to be a char array of nine elements:
' ' (space), b, c, d, ' ', f, \n (newline character), \0 (character with
value 0), and \0 (This is an extra 0. String literals always hold an extra
0 at the end.)

So foo[0] == ' ', foo[1] == 'b', foo[2] == 'c' ... foo[8] == char(0).

int i=2;

defines i to be an int, and initialises it to 2.

while (foo[i++]) putchar(foo);

1. Checks whether the ith element of foo is not zero.
2. Increments i.
3. If the test in 1 was true, it writes the (new) ith element of foo to the
standard output stream. If the test was false, it continues.

}

returns the value 0 to the environment.
 
A

ajones

(e-mail address removed) wrote:
I've got a take home final for my computer class and it has a bonus
question concerning
some programming that we didn't have time to cover. I've already got
an A in the class, so
it won't affect my grade one way or another, however my obsessive/
compulsive tendencies
just can't let it go. Could I get some help in explaining what this
code snippet does;
char foo[8]=" bcd f\n\0";
int i=2;
while (foo[i++]) putchar(foo);



I don't understand all the descriptions that everyone has posted here.
This code just appears to print cd f to standard out. This code is
just
trying to change directories.
char foo[8]=" bcd f\n\0";

As for the syntax for the initialization for the array, it doesn't
appear to be correct.

That's my two cents...take it for what it is worth....
 
A

ajonesfl

[..]
char foo[8]=" bcd f\n\0";
int i=2;
while (foo[i++]) putchar(foo);

I don't understand all the descriptions that everyone has posted here.
This code just appears to print cd f to standard out. This code is
just
trying to change directories.

It's not trying to change anything except the contents of the standard
output.


I stand corrected. It just prints cd f to the standard out, as long as
standard out has not been redefined.....
 
D

Default User

Robert said:
(e-mail address removed) wrote:
char foo[8]=" bcd f\n\0";
From what I've been
able to glean from the book is that its declaring an array of 8
integers

Yes. The integers are of type char.

Then this array is initialised with nine elements, which is an error.
You are not allowed to initialise an array with more elements than it
can hold. Thus, the answer to your question: It does not compile.

This points out one of the subtle differences between C and C++. That
would be a valid initalization in C, as it would skip implictly adding
the automatic null terminator. One is explicitly specified, so it would
even be a legal C string.




Brian
 
J

James Kanze

I've got a take home final for my computer class and it has a bonus
question concerning
some programming that we didn't have time to cover. I've already got
an A in the class, so
it won't affect my grade one way or another, however my obsessive/
compulsive tendencies
just can't let it go. Could I get some help in explaining what this
code snippet does;
char foo[8]=" bcd f\n\0";
int i=2;
while (foo[i++]) putchar(foo);


It confuses the reader, and shows that 1) the author doesn't
know C or C++ very well, if at all (since the final \0 in the
string is superflu), and 2) doesn't care whether the code is
understandable or not (since he practices conciseness to the
point of obfuscation).

Presumably, what the author was trying to do was something like:
std::cout << "cd f" << std::endl ;
or:
std::string foo( "cd f\n" ) ;
std::cout << foo ;
Both of which are significantly easier to read.

I don't know the exact context of the question, but any course
which teaches you to write things like "while (foo[i++])" pretty
much makes you unemployable in a company which values quality
code.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

[..]
char foo[8]=" bcd f\n\0";
int i=2;
while (foo[i++]) putchar(foo);

I don't understand all the descriptions that everyone has posted here.
This code just appears to print cd f to standard out. This code is
just
trying to change directories.

It's not trying to change anything except the contents of the standard
output.


I stand corrected. It just prints cd f to the standard out, as long as
standard out has not been redefined.....


Sorry, but wrong again. It will always print to standard out, regardless
whether it has been redirected or not. However you'll only see it in
your terminal if it has not been redirected, but that's of no concern
for the program.
 
A

ajonesfl

Sorry, but wrong again. It will always print to standard out, regardless
whether it has been redirected or not. However you'll only see it in
your terminal if it has not been redirected, but that's of no concern
for the program.

Before you accuse me of being wrong. Please get your facts correct and
go reread my last post. I never said that it would not go to standard
out. Maybe I needed to qualify my statement. I assumed that most
people know that by default standard out is the terminal. You can
redirect standard out to be just about anything you want it to be.
Just because you redirect standard out to be a file does not change
that it is standard out. I did not think I had to explain that. Based
It just prints cd f to the standard out, as long as
standard out has not been redefined.....

The assumption was that the chararcters would be printed to the
terminal vs. a file or socket.....

Adam Jones
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top