Re: Some Questions #2
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Fri, 14 Apr 2006 14:46:38 -0400
fctk wrote On 04/14/06 13:21,:
some other doubts:
1) K&R, p50, 2.10:
"if expr1 and expr2 are expressions, then
expr1 op= expr2
is equivalent to
expr1 = (expr1) op (expr2)
except that expr1 is computed only once."
what does "except that expr1 is computed only once" mean? can you make
any example in which this propriety is important?
array[ expensive_function(x) ] += 1;
It becomes even more important if the computation doesn't
always return the same answer:
array[ getchar() ] += 1;
2) is it true that the value of a character constant such as 'C' depends
on the system character set (ASCII, EBCDIC, ...)? so if i write
something as 'C' - 20, will my program become not portable?
Yes, it is true that the numeric value of 'C' depends on
the character encoding. Yes, it is true that doing arithmetic
of the kind you've shown produces a result whose meaning will
vary from one implementation to another. (Exception: the codes
for the digits must be consecutive and in ascending order, so
'0'+1 == '1', ..., '8'+1 == '9'. However, 'a'+1 == 'b' is not
guaranteed.)
3) is it true that a char *must* be 1 byte? here instead
http://www-ccs.ucsd.edu/c/types.html#Basic%20Integer%20Types is written
that the requisite is that the *minimum* range for unsigned char, for
example, must be from 0 to 255, so it may be 2 bytes or 200 bytes
without any problem.
In C, a byte is defined as synonymous with a char. Some
other fields use the term "byte" to mean "a unit of storage
comprising eight bits," but C does not: in C, a byte is a char
no matter how wide a char is.
This is really the same situation that confronts you in
understanding other words: Different realms of discourse use
the same word with different meanings, and you must know the
context to find the proper meaning. For example, consider the
various meanings "file" might have if you're talking about
- how to use the fopen() function
- the formations exhibited by a marching band
- the contents of a metalworker's toolbox
- recipes from a Cajun cookbook (well, the pronunciation
might tip you off about this one)
4) i have some problems at distinguish declaration from definitions.
for example:
double function(double x, double y); /* prototype (a kind of function
declaration) */
double function(double x, double y) { /* function definition */
int a; /* definition? declaration? */
}
A declaration describes the type of object or function
an identifier designates. A definition also brings the
object or function into existence, allocating storage for
objects or providing the executable code for functions.
Every definition is also a declaration, but a declaration
is not necessarily a definition. When I say "fctk is a
student of C" I make a declaration about what "fctk" means;
a definition is more like "fctk is a student of C, and here
he is (smile, fctk, smile and wave)."
A prototype is a description of a function's parameters.
A prototype may appear as part of a function declaration or
as part of a function definition, but the prototype is only
the part about the parameter list. Your first example is a
function declaration using a prototype; your second is a
function definition using a prototype. Neither of them "is"
a prototype in its entirety.
also, are 'double x, double y' in the function definition two
definitions or two declarations?
Formally speaking, they are declarations. Note that some
of the "decorations" that are possible with definitions are
not possible with function parameters; these are illegal:
double f(static double x, extern double y,
double z = 42.0, typedef double trouble)
Function parameters don't need definitions as such; if you
want, you can think of them as being "defined" as part of
the process of calling the function. Somehow the compiler
causes the parameters to come into existence and be initialized
with the argument values as part of the function invocation,
and somehow the compiler causes them to vanish again when the
function returns. If you are interested in how compilers and
related tools work, the mechanics of this buck-passing are of
importance to you; if you simply need to use C, they are not.
--
Eric.Sosman@xxxxxxx
.
- Prev by Date: Re: Some Questions
- Next by Date: Re: folds and formatting
- Previous by thread: Re: Some Questions #2
- Next by thread: Re: Some Questions #2
- Index(es):
Relevant Pages
|