Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
C: Variables


1. C: Variables

2. C: Variables



A computer variable is a named storage location.

3. Variable names
A mathematical variable is a placeholder for a value.

Variable names start with a letter and continue with a letter or digit with no spaces. An underscore character can be used but should not be used as the first character of a variable name.

A variable name identifies that variable. Such names are called identifier

4. Consistency
Whatever method you use for naming variables, be consistent within the same program or group of programs.

5. Example variable name
There are many ways to create a variable for "total hours". Here are some ways. Do not use TOTALHOURS as all uppercase is used (by tradition) for constants (named literals).

A name such as th (for "total hours") is not very meaningful and should be avoided.

6. Variable declarations
The English word "declare" means to say something of importance. Example: The Declaration of Independence.

In many computer languages, computer variables must be declared and initialized before being used. (Being used means being used in an expression).

Some programming languages have implicit declarations.

C requires that all variables be declared (and initialized) before use.

7. Variable declaration and initialization
Here is the C code.

Here is the output of the C code.

Note the use of the format specifier lf in the printf statement and not f.

8. Variable declaration and initialization
Here is what happens with no variable declarations.

Here is the C code.

Here is the error text of the C code.


9. Variable declaration and initialization
Here is what happens with no variable initializations.

Here is the C code.

Here is the output of the C code.

C may not raise an exception if variables are not initialized. This can be the source of very hard-to-find programming errors.

10. End of page

11. Multiple choice questions for this page