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.

For example:

int count;
char key;
char lastname[30];

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:

int busysignals;

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.

Comments (7)

  1. Posted by not one not
    This is for "beginners"..NOT. Here's why I say this. Examples of what's not explained : -variable (what is that? all it says it that it can contain numbers of whatever. But WHAT is it?) -There are several different types of numeric variables, depending on the size and precision of the number. (WHAT does "pprecision of the number" mean?) -compiler (WHAT is that?) -an integer variable (WHAT is an integer?) -The compiler knows that integer values fit into the count variable (WHAT is "compiler"? WHAT is "integer"? WHAT does "variable" mean?) -Declare your variables near the beginning of your program, just after the line with the initial curly bracket. Cluster them all up right there. (WHAT does "initial curly bracket" mean?) These are one "SOME" of the questions a beginner has, like me. If I put the rest here, it probably won't fit in this text box (yes, I have THAT many!). So, with all these "What" questions, I can't understand ANY of the text above, and with NO explanations of these things, how can I or anyone with similiar questions. So, this is NOT "beginner level", cause in order to understand it you have to ALREADY KNOW THIS STUFF, but actual "beginners" DON'T.
  2. Posted by Nicole
    to not one not ^ Although I am not absolutely a beginner (I just finished an intro to programming class at my college) I can see your point. Some of this is a bit confusing. I would suggest reading the section "Looking at the C language" before reading this. To answer some of your questions though, a variable can be a bit tough to understand at first. My teacher explained it as a cup that can hold different things. When you are talking to the computer, you have to explain that in this cup there is a number, or in this cup is a word. Imagine you are at a party and you wrote your name on one of those red plastic cups. Now imagine the rest of the people as the computer. They have to look at each name on each cup to tell whose hand it should be in. The concept gets easier as you learn more. An integer is simply a whole number (1,2,3,4,...). A number with a decimal point (5.0) is called a float. I believe that was what they were getting at when talking about precision. A compiler is just the program you write all of this in. It's what translates your words into something the computer can understand and process (ones and zeros, or binary). In terms of the curly bracket, at the beginning of all C language code you will see something that looks like this {. That is a curly bracket. C loves them. The initial curly bracket is just the first one you see. I hope this helps. It can be very hard to write articles for beginners. You have to understand that once you reach a certain level of understanding in programming it is hard to remember where you had to start from. Most of these terms can be googled easily though. I suggest that if you are really interested in programming you take a class or find someone who can teach you. It can be very challenging to learn on your own.
  3. Posted by Peter
    For general programming purposes (understandably this can't be done for all C versions) it is considered bad practice to declare all of the variables that the method uses in one block at the beginning of the method. The variables should be declared as close to where they are actually used as possible. In general, tt reduces the chance for errors and makes the code easier to read.
  4. Posted by Andy Huang
    I was much pleased readin this! As I reached climax, I also reached for the knife.
  5. Posted by NewB
    I was just wondering if variable names could be started with a underscore _ or something of that sort ... and if the names can have spaces in them...
  6. Posted by dilan from Sri Lanka
    hi, i tried to declare a variable after some lines. my program was like this, int arr_size; arr_size = 5 int my_arr[arr_size]; here of course i coud have declared that arr_size and assigned the value in da same line..but i wanted to do it this way... when i tried to compile the program, iam getting an error saying that "declaration is not allowed here" error how could i overcome this problem and create variables in the middle of the program.. iam using turbo c++ compiler v 3.0 in DOS i think what ever the error here is with my compiler. eventhough it's a C++ compiler not a C compiler, they say that we can compile C also with this compiler cheeeerzzzzz :)
  7. Posted by Doodiadit
    Of course, what a great site and informative posts, I will add backlink - bookmark this site? Regards, Reader.

Leave a Reply


Post Comment

Connect with For Dummies

Sign Up for RSS Feeds

Computers & Software

Inside Dummies.com

Dummies.com Sweepstakes

Win a Netbook with Windows 7!