[comp.lang.c] Some interesting novice questions from a novice guy

scott@max.u.washington.edu (10/24/90)

Hello, I am a novice C program and such my head is spinning with
questions that are screaming to be answer, otherwise will drive me to
the point of insanity... in turn, hopefully I will not be driving you
insane with "much asked" dull quesitions. Thanks in advance.
 
 
Some specific question:
 
 
Example 1:                               Example 2:
 
        for (i=0; i<10; i++)             for (i=0; i<10; ++i)
        {                                {
           <first statement>               <first statament>
             "        "                      "         "
             "        "                      "         "
           <last  statement>               <last  statement>
        }                                }
 
In example 1, when does the value of "i" gets incremented?
     My guess is after the <last statement> in the block has been
     executed. But I want to be sure.
In example 2, the same question.
     My guess in this one is after "i<10".
 
 
Example 3:                                         
 
        while (*ptr++);                  while (*ptr)
                                           ptr++;
 
In example 3, do the two pieces of codes are equivalent?
 
 
Example 4:
 
        void test(char str[])            void test(char *str)
 
In example 4, do the two pieces of codes are interchangeable?
 
 
 
And lastly, I have one general question.
It has to do with types. In other computer languages like ADA and PASCAL
mixing different types in a expression or assigning the content of
one type of variable to a different type of variable is a no no.
This doesn't mean that you can't use, for example, an integer value in an
expression with reals, but you must first convert the integer value to a     
real format (through a function) before using it with other reals.
But C doesn't seems to fall into this rule. Character variables can
be treated as integers and also "=" to and from a integer. Same thing
goes with unsigned variables. But integers occupy two bytes while
characters only one. So when you int "=" char, what value will get
store in the higher byte of the int? How about with integer and
unsigned? If I am only dealing with positive numbers, there is no
difference between the two? If I write char "=" 65 instead of 'A',
is that a normal way of doing it or bad practice. How about when
the function returns a value 1 to 5, should I make the function's
return type an integer or an unsigned or even a character?
 
 
I will appreciate you help
 
 
Sincerely,
Scott K. Stephen

pfalstad@shine.Princeton.EDU (Paul John Falstad) (10/24/90)

In article <14488.27252b63@max.u.washington.edu> scott@max.u.washington.edu writes:
>Example 1:                               Example 2:
>        for (i=0; i<10; i++)             for (i=0; i<10; ++i)
>        {                                {
>           <first statement>               <first statament>
>             "        "                      "         "
>             "        "                      "         "
>           <last  statement>               <last  statement>
>        }                                }
>In example 1, when does the value of "i" gets incremented?
>In example 2, the same question.

In both cases, i gets incremented after the last statement and before
the i<10.

	for (a; b; c)
		{
		d;
		e;
		}

is entirely equivalent to:

	a;
	while (b)
		{
		d;
		e;
		c;
		}

unless you use a continue statement.

>        while (*ptr++);                  while (*ptr)
>                                           ptr++;
>In example 3, do the two pieces of codes are equivalent?

No they're not.  while(*ptr++) will advance ptr one position farther
than while(*ptr) ptr++.

Also, while (*ptr++); is a BIG no-no in this newsgroup.  To make your
intent crystal-clear, you should use this construct:

#define DO_ABSOLUTELY_NOTHING {  ;  }

	while (*ptr++) { /* warning: confusion ahead */
		DO_ABSOLUTELY_NOTHING ; /* Note: I meant to do this */
		} /* no more confusion, you can relax now */

>        void test(char str[])            void test(char *str)
> 
>In example 4, do the two pieces of codes are interchangeable?

Yes.  But pointers and arrays are not interchangeable in general, of
course.

>goes with unsigned variables. But integers occupy two bytes while
>characters only one. So when you int "=" char, what value will get
>store in the higher byte of the int? How about with integer and

It will set the int's upper bits to the value of the character's sign bit.
If you do int i = c, where c is positive, it will store zeros in the
upper byte of the int.  (At least on some machines.)

>difference between the two? If I write char "=" 65 instead of 'A',
>is that a normal way of doing it or bad practice. How about when

Bad practice.  Use 'A'.  ('A' is an integer, by the way, not a
character.)

>the function returns a value 1 to 5, should I make the function's
>return type an integer or an unsigned or even a character?

Don't bother.  At least on the machines I am familiar with, this will
make no difference.  Byte operations are more costly on many machines.

--
This is a delicate, sensitive, well-brought-up game which does not recognize
the word... well, Whatever it was you just said that we do not recognize.
What would Miss Manners say?  Who the fuck do you think you are, anyway?
Please use another, nice word instead.  [from the game Bureaucracy]

jamiller@hpcupt1.cup.hp.com (Jim Miller) (10/25/90)

I'm glad the previous poster answered your questions, and, indeed, a few
"obvious" questions help you be sure of your understanding.  Having said
that:
$-- FLAME ON --
   (GET and then)  R E A D   T h e  F i n e  M A N U A L -
$-- FLAME OFF --
     (whew!)

Really, if you haven't already, run (not walk) and get (and READ!)
K&R2 a.k.a K&RII a.k.a.
   The C Programming Language (second edition)
      by Kernighan and Ritchie.


   It's a good book and you will be glad you read it.
   Read it twice, it's not that long.
   Refer to it often.

   jim - that felt good - miller
   jamiller@hpmpeb7.cup.hp.com
   (a.k.a James A. Miller; Jim the JAM; stupid; @!?$$!; ... )
   Anything I say will be used against me ...
   But my company doesn't know or approve or condone anything of mine here.
   

peter@ficc.ferranti.com (Peter da Silva) (10/26/90)

In article <3553@idunno.Princeton.EDU> pfalstad@shine.Princeton.EDU (Paul John Falstad) writes:
> 	while (*ptr++) { /* warning: confusion ahead */
> 		DO_ABSOLUTELY_NOTHING ; /* Note: I meant to do this */
> 		} /* no more confusion, you can relax now */

I saw this discussion. Doesn't make sense. Doesn't anyone use:

	while(*foo++)
		continue;

anymore?
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
peter@ferranti.com

dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) (10/27/90)

In <L3O6IN7@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:

>	while(*foo++)
>		continue;

I know I'm not a C guru, and maybe I'm the only one here who gets
confused by autoincrement-right-after-dereferencing, but I have always
hated that "continue" statement, because it bothers my intuition.  I
have always been taught that "continue" means "That's all right, don't
mind me, just keep on doing what you were doing before I interrupted
you."  But the C meaning of "continue" (not to be confused with the
Fortran meaning of "CONTINUE") is more like: "Hey you!  Just what the
heck do you think you are doing?  Stop that right now!  Go back to the
beginning and do it all over again, and try to do it right this time!"

When I see "continue" in the middle of a long loop, I get a little
confused, but I usually recover.  But to see "continue" as the only
thing in a loop really stupifies me.  Consider:

	while(*foo++) {
           Hey you!  Just what the heck do you think you are
           doing?  Stop that right now!  Go back to the
           beginning and do it all over again, and try to do
           it right this time!
        }

I guess part of the problem is the cognitive dissonance (Hah!  I bet
you thought I didn't know anything about music!) that I go through when
I see a C "continue" used in the Fortran sense;  for what the
programmer was *really* trying to say was:

	while(*foo++)		/* very C-ish */
           keep on going;	/* very Fortran-ish */

The confusion just doesn't cut it for me.  So I do the good old simple
stuff, complete with comments that a real guru wouldn't need (but I
do):

        while (*foo != '\0')	/* find trailing null */
           foo++;
        foo++;			/* go past it */

(Actually, when I have to skip the rest of the loop body, more often
than not, I put a label near the end of the loop and do a goto to it.
This way I can search for all occurrences of goto's to that label, and
they are usually unique for a given loop.  A search for "continue", if
I used it, would probably occur in many unrelated places in any given
file, and I would be even more confused than I am now.)
--
Rahul Dhesi <dhesi%cirrusl@oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi
A pointer is not an address.  It is a way of finding an address. -- me

burley@world.std.com (James C Burley) (10/27/90)

In article <2620@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:

   In <L3O6IN7@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:

   >	while(*foo++)
   >		continue;

   I know I'm not a C guru, and maybe I'm the only one here who gets
   confused by autoincrement-right-after-dereferencing, but I have always
   hated that "continue" statement, because it bothers my intuition.  [...]
   --
   Rahul Dhesi <dhesi%cirrusl@oliveb.ATC.olivetti.com>

I agree.  So what's wrong with the simple solution I've learned (or been
taught) to use:

    while(*foo++)
      ;

Doesn't the dangling semi say enough?  (I do the same thing with my empty-
bodied for loops.)

James Craig Burley, Software Craftsperson    burley@world.std.com

peter@ficc.ferranti.com (Peter da Silva) (10/28/90)

> >	while(*foo++)
> >		continue;

> 	while(*foo++) {
>            Hey you!  Just what the heck do you think you are
>            doing?  Stop that right now!  Go back to the
>            beginning and do it all over again, and try to do
>            it right this time!
>         }

You need to reprogram yourself. "continue" means "skip the rest of this loop".
Which makes perfect sense even if the loop's empty. It doesn't mean "go back
to the beginning", because that implies the test will not be performed again
(at least to me).

How many angels can dance on the head of a pin?
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
peter@ferranti.com

stanley@phoenix.com (John Stanley) (10/28/90)

dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:

> I know I'm not a C guru, and maybe I'm the only one here who gets
> confused by autoincrement-right-after-dereferencing, but I have always
> hated that "continue" statement, because it bothers my intuition.  I
> have always been taught that "continue" means "That's all right, don't
> mind me, just keep on doing what you were doing before I interrupted
> you."  But the C meaning of "continue" (not to be confused with the
> Fortran meaning of "CONTINUE") is more like: "Hey you!  Just what the
> heck do you think you are doing?  Stop that right now!  Go back to the
> beginning and do it all over again, and try to do it right this time!"
> 
   Your problems are 1) a fatal overdose of anthropomorphism and 2) a
personally concocted definition of "continue" that does not match the
real world.

   Computers are (relatively) simple machines. They do not respond to
"Hey, you!". They do not think. They are unlike humans in that if they
"try" the same instructions again, they will get the same answer.  If the
answer is not right it is either because the hardware is faulty, in which
case you should take the computer to the repair shop, or because YOU
programmed it wrong, and YOU should be taken to the repair shop.

   Your problem with anthropomorphism leads to a crocked definition of
"continue". The correct definition of "continue", in the C language
context, is "go to the top of the smallest enclosing while, do, or for."
There is no "thinking", no "try to do it right". Certainly no "Hey, you!"
Since you ignore the standard definition, I wonder what other language
elements you are confused by. Do you also choose to use only the algebraic
definition of "=" as "is equal to"? I bet a statement like "a=a+2;" really
drives you around the bend.

> Consider:
> 
> 	while(*foo++) {
>            Hey you!  Just what the heck do you think you are
>            doing?  Stop that right now!  Go back to the
>            beginning and do it all over again, and try to do
>            it right this time!
>         }
> 
   No, thank you. You consider:

	while (*foo++) {
		go to the top of the smallest enclosing while, do
		or for.
	}

> I guess part of the problem is the cognitive dissonance (Hah!  I bet
> you thought I didn't know anything about music!) 

   I still have no proof you know anything about music. It is a well known
fact that Leonard Bernstein was a quiche eater who programmed only in
Pascal. Yo Yo Ma uses APL.

> that I go through when
> I see a C "continue" used in the Fortran sense;  for what the
> programmer was *really* trying to say was:
> 
> 	while(*foo++)		/* very C-ish */
>            keep on going;	/* very Fortran-ish */
> 
   and when you know the REAL definition of "continue" you know that the
programmer said EXACTLY what he *really* meant, before you tried to mix in
the FORTRAN. BTW, "keep on going" is legal syntax in no FORTRAN I know of.
Is this a vendor extension? If not, how is "keep on going" FORTRAN-ish?

> The confusion just doesn't cut it for me.  So I do the good old simple
> stuff, complete with comments that a real guru wouldn't need (but I
> do):
> 
>         while (*foo != '\0')	/* find trailing null */
>            foo++;
>         foo++;			/* go past it */
> 
   At first blush, this looks like a while that might loop forever
followed by two increments of foo that are poorly indented. The comments
are visual cognitive dissonance (which has nothing to do with music). 

> (Actually, when I have to skip the rest of the loop body, more often
> than not, I put a label near the end of the loop and do a goto to it.

   Oh, no. I sense the beginning of a goto flame war.

> This way I can search for all occurrences of goto's to that label, and
> they are usually unique for a given loop.  A search for "continue", if
> I used it, would probably occur in many unrelated places in any given
> file, and I would be even more confused than I am now.)

   Just as a search for all occurances of while, if, etc. Why not search
for the comments. Those can be anything, unlike the elements of the C
language. Why twist the C code just so you can search for goto? Or do you
also avoid while's so you can search for labels?

   If you don't want to use the standard definition of the C programming
"words", either expect to be confused by it or don't read C.

"Arinth is a beautiful planet." "Oh, have you been there?"
  "Yes, but not yet." The Doctor. (TB)

dik@cwi.nl (Dik T. Winter) (10/28/90)

In article <L15PR5w161w@phoenix.com> stanley@phoenix.com (John Stanley) writes:
 > dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
...
 > >        But the C meaning of "continue" (not to be confused with the
 > > Fortran meaning of "CONTINUE") is more like: "Hey you!  Just what the
 > > heck do you think you are doing?  Stop that right now!  Go back to the
 > > beginning and do it all over again, and try to do it right this time!"
 >    Your problems are 1) a fatal overdose of anthropomorphism and 2) a
 > personally concocted definition of "continue" that does not match the
 > real world.
Calm down please.  Rahul Dhesis idea is not that wrong.
 >             The correct definition of "continue", in the C language
 > context, is "go to the top of the smallest enclosing while, do, or for."
This is wrong, it is not go to the top, but go to the bottom!
Consider the difference in a 'do {...} while'.  Go to the top will never see
the test.

To correct Rahuls words:
	"Hey you!  I have seen this, now please get the next one, if any!"
--
dik t. winter, cwi, amsterdam, nederland
dik@cwi.nl

stanley@phoenix.com (John Stanley) (10/29/90)

dik@cwi.nl (Dik T. Winter) writes:

> Calm down please.  Rahul Dhesis idea is not that wrong.

   I am perfectly calm. The definition of continue was both fatally
anthropomorphic and incorrect.

>  >             The correct definition of "continue", in the C language
>  > context, is "go to the top of the smallest enclosing while, do, or for."
> This is wrong, it is not go to the top, but go to the bottom!

   Please argue with M.I. Bolsky of the Systems Training Center at AT&T
Bell Labs. This is a direct quote of his. 

   Your definition is also wrong. K&R 2ed, p. 65, "continue ... causes
the the next iteration of the enclosing for, while, or do loop to begin."
It does not refer to "Hey, you! Try again", nor to "go to the bottom".

> To correct Rahuls words:
> 	"Hey you!  I have seen this, now please get the next one, if any!"

   Still fatally anthropomorphic. And impolite. There is no "Thank you"
statement. 


"Arinth is a beautiful planet." "Oh, have you been there?"
  "Yes, but not yet." The Doctor. (TB)

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (10/29/90)

In article <L15PR5w161w@phoenix.com>, stanley@phoenix.com (John Stanley) writes:
>    Computers are (relatively) simple machines.
> They do not respond to "Hey, you!".

I beg to differ.  The Burroughs machines have an instruction to
interrupt another processor in the same cluster, and the name
of that instruction is "HEYU".		[It's a _joke_!]

-- 
The problem about real life is that moving one's knight to QB3
may always be replied to with a lob across the net.  --Alasdair Macintyre.

rpg@xcc.uucp (Ralf Gans) (10/30/90)

In article <2620@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>In <L3O6IN7@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:
>
>>	while(*foo++)
>>		continue;
>
>I know I'm not a C guru, and maybe I'm the only one here who gets
.....
>do):
>
>        while (*foo != '\0')	/* find trailing null */
>           foo++;
>        foo++;			/* go past it */
>

It is a very nice C-BASIC! But that's not C.

Ralf Gans
rpg@xcc.uucp via unido
-- 

------

 Ralf P. Gans 	  UUCP:     rpg@xcc.uucp

karl@haddock.ima.isc.com (Karl Heuer) (10/31/90)

In article <2620@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>maybe I'm the only one here who gets confused by autoincrement-right-after-
>dereferencing [as in "*s++"]

"*s++" as an rvalue is just the string equivalent of "getc()".

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint

mark@DRD.Com (Mark Lawrence) (11/03/90)

burley@world.std.com (James C Burley) wrote:
[with reference to null body loops and use of continue]
} I agree.  So what's wrong with the simple solution I've learned (or been
} taught) to use:
} 
}     while(*foo++)
}       ;

I personally like Larry Walls semantics in Perl --

#define next continue

while(*foo++)
   next;    /* there, Rahul, isn't that better? */
-- 
mark@DRD.Com uunet!apctrc!drd!mark$B!J%^!<%/!!!&%m!<%l%s%9!K(B

asylvain@felix.UUCP (Alvin "the Chipmunk" Sylvain) (11/03/90)

In article <14488.27252b63@max.u.washington.edu> scott@max.u.washington.edu writes:
> Hello, I am a novice C program and such my head is spinning with
> questions that are screaming to be answer, otherwise will drive me to
> the point of insanity... in turn, hopefully I will not be driving you
> insane with "much asked" dull quesitions. Thanks in advance.

[... dull questions which would been better left to make him insane ...]

Are you trying to tell us that in the entire city of Seattle, or even
the entire state of Washington, that you can not find a single person to
answer these questions one-on-one?

Forgive me if you're typing this from your death-bed and can't get out much.

Otherwise, I really suggest you find someone to talk to who's answered
questions like these before.  Like someone else mentioned, RTFM, but if
it's written in Swahili or something (sometimes they seem to be), FSWKAA
(Find Somebody Who Knows And ASK).

Doing this has at least 3 benefits
1) You don't waste expensive bandwidth with dull questions
2) You don't waste expensive bandwidth with dull answers (such as mine)
3) You'll get your answers much more quickly and interactively.  I.e.,
      you can keep asking when you don't understand the answer.

Summary:  RTFM   (Read The Friendly Manual)
	  FSWKAA (Find Somebody Who Knows And ASK)

If these both fail, then you are more than welcome to tap the vast
expertise of the folks on the Net.  Asking the Net sooner is hitting a
Tack with a ThermoNuclear SledgeHammer.

boyd@necisa.ho.necisa.oz (Boyd Roberts) (11/05/90)

In article <L3O6IN7@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:
>
>I saw this discussion. Doesn't make sense. Doesn't anyone use:
>
>	while(*foo++)
>		continue;
>
>anymore?

No, that's just overly verbose.

	while (*p++)
		;

Is just fine.  I can see the semicolon.  That's all you need to see.

Hiding the semicolon `while (*p++);' just causes confusion.



Boyd Roberts			boyd@necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''

gwyn@smoke.brl.mil (Doug Gwyn) (11/07/90)

In article <L3O6IN7@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:
>I saw this discussion. Doesn't make sense. Doesn't anyone use:
>	while(*foo++)
>		continue;
>anymore?

Nope.  I use
	while ( *foo++ != '\0' )	/* or != NULL, depending */
		;
I figure that if anybody doesn't immediately understand this they shouldn't
be messing with it in the first place.