Declaring Variables in C
Variables are what make your programs zoom. Programming just can’t get done without them. So if you haven’t been introduced to variables yet, here you go.
Valerie Variable is a numeric variable. She loves to hold numbers — any number; it doesn’t matter. Whenever she sees an equal sign, she takes to a value and holds it tight. But see another equal sign, and she takes on a new value. In that way, Valerie is a little flaky. You could say that Valerie’s values vary, which is why she’s a variable.
Victor Variable is a string variable. He contains bits of text — everything from one character to several of them in a row. As long as it’s a character, Victor doesn’t mind. But which character? Victor doesn’t care — because he’s a variable, he can hold anything.
- Yes, there is a point here. There are two main types of variables in C: numeric variables that hold only numbers or values, and string variables that hold text, from one to several characters long.
- There are several different types of numeric variables, depending on the size and precision of the number.
- Before you use a variable, it must be declared. This is — oh, just read the next section.
“Why must I declare a variable?”
You are required to announce your variables to the C compiler before you use them. You do this by providing a list of variables near the beginning of the program. That way, the compiler knows what the variables are called and what type of variables they are (what values they can contain). Officially, this process is known as declaring your variables.
Three variables are declared here: an integer variable, count; a character variable, key; and a character variable, lastname, which is a string that can be as many as 30 characters long.
Doing this at the beginning of the program tells the compiler several things. First, it says, “These things are variables!” That way, when the compiler sees lastname in a program, it knows that it’s a string variable.
Second, the declarations tell the compiler which type of variable is being used. The compiler knows that integer values fit into the count variable, for example.
Third, the compiler knows how much storage space to set aside for the variables. This can’t be done “on the fly” as the program runs. The space must be set aside as the compiler creates the program.
- Declare your variables near the beginning of your program, just after the line with the initial curly bracket. Cluster them all up right there.
- Obviously, you won’t know all the variables a program requires before you write it. (Although they teach otherwise at the universities, such mental overhead isn’t required from you.) So, if you need a new variable, use your editor to declare it in the program. Rogue variables generate syntax or linker errors (depending on how they’re used).
- If you don’t declare a variable, your program does not compile. The proper authorities issue a suitable complaint message.
- Most C programmers put a blank line between the variable declarations and the rest of the program.
- There’s nothing wrong with commenting a variable to describe what it contains. For example:
int count; /* busy signals from tech support. */
- However, cleverly named variables may avoid this situation:
Variable names verboten and not
What you can name your variables depends on your compiler. There are a few rules, plus some names you cannot use for variables. When you break the rules, the compiler lets you know by flinging an error at you. To avoid that, try to keep the following guidelines in the back of your head when you create new variables:
- The shortest variable name is a letter of the alphabet.
- Use variable names that mean something. Single-letter variables are just hunky-dory. But index is better than i, count is better than c, and name is better than n. Short, descriptive variable names are best.
- Variables are typically in lowercase. (All of C is lowercase for the most part.) They can contain letters and numbers.
- Uppercase letters can be used in your variables, but most compilers tend to ignore the differences between upper- and lowercase letters. (You can tell the compiler to be case-sensitive by setting one of its options; refer to your programmer’s manual.)
- You should not begin a variable name with a number. They can contain numbers, but you begin it with a letter.
- C lords use the underline, or “underscore,” character in their variable names: first_name, zip_code, and so on. This technique is fine, though it’s not recommended to begin a variable name with an underline.
- Avoid naming your variables the same as C language keywords or functions. Don’t name your integer variable int, for example, or your string variable char. This may not generate an error with your compiler, but it makes your source code confusing.
- Also avoid using the single letters l (lowercase L) and o (lowercase O) to name variables. Little L looks too much like a 1 (one), and O looks too much like a 0 (zero).
- Don’t give similar names to your variables. For example, the compiler may assume that forgiveme and forgivemenot are the same variable. If so, an ugly situation can occur.
- Buried somewhere in one of the massive tomes that came with your compiler are the official rules for naming variables. These rules are unique to each compiler.