Prev NEXT

The Basics of C Programming

Variables

As a programmer, you will frequently want your program to "remember" a value. For example, if your program requests a value from the user, or if it calculates a value, you will want to remember it somewhere so you can use it later. The way your program remembers things is by using variables. For example:

int b;

This line says, "I want to create a space called b that is able to hold one integer value." A variable has a name (in this case, b) and a type (in this case, int, an integer). You can store a value in b by saying something like:

Advertisement

b = 5;

You can use the value in b by saying something like:

printf("%d", b);

In C, there are several standard types for variables:

  • int - integer (whole number) values
  • float - floating point values
  • char - single character values (such as "m" or "Z")

We will see examples of these other types as we go along.