Some interesting novice questions from a novice guy

Paul John Falstad pfalstad at shine.Princeton.EDU
Thu Oct 25 00:46:00 AEST 1990


In article <14488.27252b63 at max.u.washington.edu> scott at 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]



More information about the Comp.lang.c mailing list