Sams Teach Yourself C in 21 Days

(singke) #1
in an algebraic statement, you are stating a fact: “x equals 12.” In C, however, it means
something quite different. In C it means “Assign the value 12 to the variable named x.”
You can also initialize a variable when it’s declared. To do so, follow the variable name
in the declaration statement with an equal sign and the desired initial value:
int count = 0;
double percent = 0.01, taxrate = 28.5;
The first statement declares a variable called countas an integer and initializes it to zero.
The second statement declares two variables as doubles and initializes them. The first,
percent, is initialized to 0.01. The second,taxrate, is initialized to 28.5.
Be careful not to initialize a variable with a value outside the allowed range. Here are
two examples of out-of-range initializations:
int weight = 100000;
unsigned int value = -2500;
The C compiler may not catch such errors. Your program might compile and link, but
you might get unexpected results when the program is run.

50 Day 3

DOunderstand the number of bytes that
variable types take for your computer.
DOusetypedefto make your programs
more readable.
DOinitialize variables when you declare
them whenever possible.

DON’Tuse a variable that hasn’t been
initialized. Results can be unpredictable.
DON’Tuse a floatordoublevariable if
you’re only storing integers. Although
they will work, using them is inefficient.
DON’Ttry to put numbers that are too
big or too small into a variable if its type
won’t hold them.
DON’Tput negative numbers into vari-
ables with an unsignedtype.

DO DON’T


Constants ..............................................................................................................


Like a variable, a constantis a data storage location used by your program. Unlike a
variable, the value stored in a constant can’t be changed during program execution. C has
two types of constants, each with its own specific uses:


  • Literal Constants

  • Symbolic Constants


06 448201x-CH03 8/13/02 11:14 AM Page 50

Free download pdf