Introduction to Variables

Variable names are used in a program in much the same way as they are in algebra. The difference is that rather than represent that value itself, a variable in a program represents the memory location where the value is stored.

In C each variable must be declared before being used. The program must therefore contain a statement specifying exactly what kind of information the variable will contain. For example, if we wish to use the variable x to store an integer value, we must declare that x is of the type int. The word int is a keyword in C, keywords must always be typed in lowercase.

Variable names must begin with a letter of the alphabet. No variable name may be a keyword. Variable names are case-sensitive.

We give values to variables using assignment statements. An assignment statement uses the equals sign, which is called the assignment operator, to store a value in the memory of the computer under the symbolic variable name.

#include <stdio.h>

int main(void){

  /*
   * declare a variable
   */
  int x;

  /*
   * assign a value to the variable
   */
  x = 1729;

  /*
   * display the variable using
   * the %d conversion specification
   */
  printf("The Hardy–Ramanujan number is %d.", x);

  return 0; 
}

A conversion specification is a composite symbol that states that a literal or variable value of a specific type is to be inserted at that position. There is a one-to-one correspondence between conversion specifications and the variables associated with them. The conversion specification for an integer value type is %d.

In our next program, the printf() statement contains two conversion specifications within the control string. This is so that we can print out the values stored in the two variables using a single statement. Both variable identifiers must be listed at the end of the printf() statement in the order that they are to be inserted. The variable identifiers must be separated from each other and from the control string by commas.

#include <stdio.h>

int main(void){

  /*
   * declare and initialize variables
   * in one statement
   */
  int minutes = 60;
  int hours = 24;

  /*
   * display values using 
   * multiple conversion specifications
   */
  printf("%d in an hour, %d hours in a day", minutess, hours);

 return 0; 

}

C uses the same arithmetic operators as seen in algebra. These operators are sometimes called binary operators because they operate on two terms or values at a time.

As we have seen earlier, C provides a way to combine a declaration with an assignment. We can even assign the value of an expression to a variable.

#include <stdio.h>

int main(void){
  int speed, time, distance;

  speed = 88;
  time = 3;

  distance = speed * time;

  printf("Traveling at %d MPH for %d hours, ", speed, time);
  printf(" we cover %d miles.\n", distance);

  return 0; 
}

As we have seen above, an expression used in an assignment operation may even contain other variables that have been previously declared and initialized.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s