How to Use Variables when Programming in C

Most, if not all, of your future C language programs will employ variables. There are basic three steps for using variables in the C language:

  1. Declare the variable, giving it a variable type and a name.

  2. Assign a value to the variable.

  3. Use the variable.

All three steps are required for working with variables in your code, and these steps must be completed in that order.

To declare a variable, place a statement near the start of a function, such as the main() function in every C program. Place the declaration after the initial curly bracket. The declaration is a statement on a line by itself, ending with a semicolon:

type name;

type is the variable type.

In the preceding example, name is the variable’s name. A variable’s name must not be the name of a C language keyword or any other variable name that was previously declared. The name is case sensitive, although, traditionally, C language variable names are written in lowercase. You can also add numbers, dashes, or underscores to the variable name, but always start the name with a letter.

The equal sign is used to assign a value to a variable. The format is very specific:

variable = value;

Read this construct as, “The value of variable equals value.”

Here, variable is the variable’s name. It must be declared earlier in the source code. value is either an immediate value, a constant, an equation, another variable, or a value returned from a function. After the statement is executed, the variable holds the value that’s specified.

Assigning a value to a variable satisfies the second step in using a variable, but you really need to do something with the variable to make it useful. Variables can be used anywhere in your source code that a value could otherwise be specified directly.

In Working with Variables, four variable types are declared, assigned values, and used in printf() statements.

WORKING WITH VARIABLES

#include <stdio.h>
int main()
{
  char c;
  int i;
  float f;
  double d;
  c = 'a';
  i = 1;
  f = 19.0;
  d = 20000.009;
  printf("%c\n",c);
  printf("%d\n",i);
  printf("%f\n",f);
  printf("%f\n",d);
  return(0);
}

Exercise 1: Type the source code for Working with Variables into the editor. Build and run.

The output looks something like this:

a
1
19.000000
20000.009000

In Line 10, the single character value a is placed into char variable a. Single characters are expressed using single quotes in C.

In Line 15, you see the %c placeholder used in the printf() statement. That placeholder is designed for single characters.

Exercise 2: Replace Lines 15 through 18 with a single printf() statement:

printf("%c\n%d\n%f\n%f\n",c,i,f,d);

Build and run the code.

The printf() formatting string can contain as many conversion characters as needed, but only as long as you specify the proper quantity and type of variables for those placeholders, and in the proper order. The variables appear after the formatting string, each separated by a comma, as just shown.

Exercise 3: Edit Line 12 so that the value assigned to the f variable is 19.8 and not 19.0. Build and run the code.

Did you see the value 19.799999 displayed for variable f? Would you say that the value is imprecise?

Exactly!

The float variable type is single precision: The computer can accurately store only eight digits of the value. The internal representation of 19.8 is really the value 19.799999 because a single-precision (float) value is accurate only to the eighth digit. For mathematical purposes, 19.799999 is effectively 19.8; you can direct the code to display that value by using the %.1f placeholder.

Exercise 4: Create a project named ex0605. In the source code, declare an integer variable blorf and assign it the value 22. Have a printf() statement display the variable’s value. Have a second printf() statement display that value plus 16. Then have a third printf() statement that displays the value of blorf multiplied by itself.

Here’s the output from the sample program solution:

The value of blorf is 22.
The value of blorf plus 16 is 38.
The value of blorf times itself is 484.

Exercise 5: Rewrite the source code for Exercise 4. Use the constant value GLORKUS instead of the blorf variable to represent the value 16.

  • A variable name must always begin with a letter, but you can also start the name with an underscore, which the compiler believes to be a letter. Generally speaking, variable names that begin with underscores are used internally in the C language. Avoid that naming convention for now.

  • It isn't a requirement that all variables be declared at the start of a function. Some programmers declare variables on the line before they’re first used. This strategy works, but it’s nonstandard. Most programmers expect to find all variable declarations at the start of the function.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com