Basics of Variables for C Programming - dummies

Basics of Variables for C Programming

By Dan Gookin

Your C programs can use two types of values: immediate and variable. An immediate value is one that you specify in the source code — a value you type or a defined constant. Variables are also values, but their contents can change. That’s why they’re called variables and not all-the-time-ables.

A quick example

Who likes to read a lot about something before they try it? Not me!

Exercise1: Start a new project in Code::Blocks, name it ex0601, and type the source code, as shown in Your First Variable. This project uses a single variable, x, which is one of the first computer variable names mentioned in the Bible.


#include <stdio.h>
int main()
  int x;
  x = 5;
  printf("The value of variable x is %d.n",x);

Here’s a brief description using the line numbers assigned in the Code::Blocks editor on the computer screen:

Line 5 contains the variable’s declaration. Every variable used in C must be declared as a specific variable type and assigned a name. The variable in Your First Variable is declared as an integer (int) and given the name x.

Line 7 assigns the value 5 to variable x. The value goes on the right side of the equal sign. The variable goes on the left.

Line 8 uses the variable’s value in the printf() statement. The %d conversion character is used because the variable contains an integer value. The conversion character must match the variable; in Line 8, you see one of each.

Build and run the code. The output looks like this:

The value of variable x is 5.

Variable types

C language variables are designed to hold specific types of values. If C were a genetic programming language, cats and dogs would go into the animal variable type, and trees and ferns would go into the plant variable type. C language variables work along these lines, with specific values assigned to matching types of variables.

The four basic variable types used in C are shown here:

Type Description
char Single-character variable; stores one character of
int Integer variable; stores integer (whole number) values
float Floating-point variable; stores real numbers
double Floating-point variable; stores very large or very small real

When you need to store an integer value, you use an int variable. Likewise, if you’re storing a letter of the alphabet, you use a char variable. The information that the program requires dictates which variable type to declare.

  • The char and int variable types are integer values. char has a shorter range. It’s used primarily to store characters — letters of the alphabet, numbers, and symbols — but it can also be used to store tiny integer values.

  • The float and double types are both floating-point variables that can store very small or very large values.

  • A fifth type, _Bool, is used to store binary values, 1 or 0, often referred to as TRUE and FALSE, respectively. _Bool, a loaner word from C++, must be written with the initial underscore character and a capital B. You may not find _Bool used in many C program source code Listings — most likely, to keep the code compatible with older compilers.