comp.lang.c
[Top] [All Lists]

Re: assigment statement can use an expression when declaring a variable?

Subject: Re: assigment statement can use an expression when declaring a variable?
From: Eric Sosman
Date: Thu, 27 Mar 2008 18:20:34 -0400
Newsgroups: comp.lang.c


vlsidesign wrote:
#include
int main(void)
{
float bread_price = 0.99;
int bread_count = 3;
float total_price = bread_count * bread_price; // this compiles okay

printf("total price is %.2f  \n", total_price);

return 0;
}

I am a newbie, and my understanding is that, roughly speaking, an
expression is usually a mix of operators and operands, and computes a
value. When you use an assigment operator during a declaration and
initialize, you can use an expression on the right hand side of the
assigment state? I do this above, and it compiles okay and seems to
work.

    Nomenclature: What you have above are not assignment
statements, but declarations that provide initializers for
the variables they declare.  Yes, there are equal signs,
and yes, the things after the equal signs are expressions.
But the declarations are not statements, and the equal
signs are not operators.  I'm being a little bit picky, but
my intent is honorable: I'm hoping to dispel some confusion,
and perhaps steer you toward the relevant sections (note the
plural) of your C textbook.  Now, on with the show:

I believe you are only supposed to do this if the variables in the
expression have already had been assigned values.

    The general rule is that you cannot make use of the value
of a variable that has not yet been given a value.  Well, you
can try it, but there's no telling what will happen: You might
get a zero or NULL or some such, or you might get some kind of
random garbage, or your program might crash, or your computer
might melt into a sticky mound of chocolate pudding.  Some of
these outcomes may be rather unlikely, but the point is this:
If you try to use the value of a variable that doesn't yet have
one, all bets are off.

    A variable declared "at file scope" (outside of any function)
or a variable declared with the `static' keyword can never be in
this indeterminate, uninitialized state.  If you don't provide
explicit initializers, such variables are silently initialized
to zero or NULL -- or to lots of zeroes and NULLs if they are
arrays or structs.  But "automatic" variables -- roughly, those
declared inside functions and without `static' -- do not get
such treatment; you must initialize them and/or assign values
to them before you try to use their values.  The same goes for
data allocated dynamically by malloc() and realloc().

I changed the above
to not have them initialized and it still compiled, and then printed
out 0.00. Maybe the below is not legal C syntax, but I have a smart
compiler?

    "Dumb compiler," I'd call it.  A smart compiler would notice
the infraction and burp up a warning that you've attempted an
unreliable and unpredictable operation.  Many compilers support
command-line flags or similar controls that influence their
level of smartness; if you're using the popular gcc compiler
I'd suggest invoking it as

        gcc -W -Wall -ansi -pedantic -O2 ...

[...] I believe also
I am supposed to use the cast since I am mixing datatypes.

    Most uses of casts are mistakes.  Not all, obviously --
if that were so, there'd be no reason for the language to
offer them!  But it is certainly not necessary to use a cast
when multiplying a float by an int; C's built-in rules for
reconciling mixed types handle it for you.

--
Eric.Sosman@xxxxxxx

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy