Roles of Variables

Variables have in programs some typical uses that are called roles of variables. For example, a variable whose value is never changed after initialization is called a fixed value. Roles of variables must not be mixed up with the basic types of variables (e.g., in the C programming language int, char, float, etc.) since the concept of roles is a classification telling what kind of a task a variable has in a certain program.

The following eleven roles are enough to cover almost all variables in simple programs. Roles appearing the most often are fixed value, stepper and most-recent holder which cover about 70 % of all variables. On the other hand, all variables don't necessary have any of the roles listed below.

The roles of variables (presented more accurately below) are:

The meaning of line numbering in example programs is to ease refering to lines. Line numbers have no effect on program execution since they are comments in C.

Icon for the role Fixed value

The role of a variable is a fixed value, if its value is not changed in run-time after initialization. The example program asks user the radius of a circle and then prints out the area of that circle. Variable r is a fixed value. This variable gets its value (on line 7) during the run-time only once. The value never changes after that. Fixed value can be used in different places in a program - twice on line 8 in the example.

/* 1 */   #include <stdio.h>
/* 2 */   #define PII 3.14
/* 3 */   int main()
/* 4 */   {
/* 5 */     float r;
/* 6 */     printf("Enter the radius of a circle: ");
/* 7 */     scanf("%f", &r);
/* 8 */     printf("The area of the circle is %.2f\n", PII * r * r);
/* 9 */     exit(0);
/* 10 */  }

Icon for the role Stepper

Stepper goes through a succession of values in some systematic way. Below is an example of a loop structure where the variable multiplier is used as a stepper. The example program outputs a multiplication table while the stepper goes through the values from one to ten.

/* 1 */  #include <stdio.h>
/* 2 */  int main()
/* 3 */  {
/* 4 */    int multiplier;
/* 5 */    for (multiplier = 1; multiplier <= 10; multiplier++)
/* 6 */      printf("%2d * 3 = %2d\n", multiplier, multiplier * 3);
/* 7 */    exit(0);
/* 8 */  }
A stepper can also be used, for example, for counting and traversing the indexes of an array.

Icon for the role Most-recent holder

A variable is a most-recent holder, if its value is the latest gone through value of a certain group or simply the latest input value. The example program repeteadly (on line 7) asks the user for the input until the input is valid. In this program the variable s is a most-recent holder since it holds the latest input value at the time.

/* 1 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     float s=0;
/* 5 */     while (s <= 0) {
/* 6 */       printf("Enter the length of the side of a square: ");
/* 7 */       scanf("%f", &s);
/* 8 */     }
/* 9 */     printf("The area of the square is %.2f\n", s * s);
/* 10 */    exit(0);
/* 11 */  }

Icon for the role Most-wanted holder

The value of a most-wanted holder is the "best" or otherwise the most-wanted value out of the values gone through so far. The exists no restrictions for measuring the superiority of the values: the most-wanted can mean, for example, the smallest or the biggest number or a number closest to a certain value.

The example program finds out which is the smallest of the ten integers given as an input. The variable smallest is a most-wanted holder since it is given (on line 9) the most recent value if it is smaller than the smallest one so far.

/* 1 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     int i, smallest, number;
/* 5 */     printf("Enter the 1. number: "); scanf("%d", &smallest);
/* 6 */     for (i = 2; i <= 10; i++) {
/* 7 */       printf("Enter the %d. number: ", i);
/* 8 */       scanf("%d", &number);
/* 9 */       if (number < smallest) smallest = number;
/* 10 */    }
/* 11 */    printf("The smallest number was %d\n", smallest);
/* 12 */    exit(0);
/* 13 */  }

(The variable i is a stepper and number is a most-recent holder.)

Icon for the role Gatherer

The value of a gatherer accumulates all the values gone through so far. The example program accepts integers given as an input one by one until the user inputs the number -999 after which the program calculates the mean value of the inputs. The variable sum is a gatherer: the total of the inputs is gathered (on line 9) in it.

/* 1 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     int count=0;
/* 5 */     float sum=0, number=0;
/* 6 */     while (number != -999) {
/* 7 */       printf("Enter a number, -999 to quit: ");
/* 8 */       scanf("%f", &number);
/* 9 */       if (number != -999) {sum += number; count++; }
/* 10 */    }
/* 11 */    if (count) printf("The mean is %.5f\n", sum / count);
/* 12 */    exit(0);
/* 13 */  }

(The variable count is a stepper and number is a most-recent holder.)

Icon for the role Follower

A follower always gets the old value of another known variable as its new value. The example program asks the user for twelve integers and finally tells the biggest difference between the two successive input integers. The variable previous is a follower: it follows the variable current (on line 9).

/* 1 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     int month, current, previous, biggestDiff;
/* 5 */     printf("Enter the 1. value: "); scanf("%d", &previous);
/* 6 */     printf("Enter the 2. value: "); scanf("%d", ¤t);
/* 7 */     biggestDiff = current - previous;
/* 8 */     for (month = 3; month <= 12; month++) {
/* 9 */       previous = current;
/* 10 */      printf("Enter the %d. value: ", month);
/* 11 */      scanf("%d", ¤t);
/* 12 */      if (current - previous > biggestDiff)
/* 13 */        biggestDiff = current - previous;
/* 14 */    }
/* 15 */    printf("The biggest difference was %d\n", biggestDiff);
/* 16 */    exit(0);
/* 17 */  }

(The variable month is a stepper, current is a most-recent holder and biggestDiff is a most-wanted holder.)

Followers are used a lot with linked data structures to point the data element previous to the one in process.

Icon for the role One-way flag

A one-way flag is a Boolean variable which once changed cannot get its original value anymore. The example program outputs the sum of input numbers given by the user and tells if there were any negative numbers among the inputs. The one-way flag neg watches (on line 11) whether there appears any negative numbers among the inputs and if at least one negative value is found, the variable will never return to value false.

/* 1 */   #include <stdio.h>
/* 2 */   #define TRUE 1
/* 3 */   #define FALSE 0
/* 4 */   int main()
/* 5 */   {
/* 6 */     int number=1, sum=0, neg;
/* 7 */     neg = FALSE;
/* 8 */     while (number) {
/* 9 */       printf("Enter a number, 0 to quit: "); scanf("%d", &number);
/* 10 */      sum += number;
/* 11 */      if (number < 0) neg = TRUE;
/* 12 */    }
/* 13 */    printf("The sum is %d\n", sum);
/* 14 */    if (neg) printf("There was at least one negative number in inputs\n");
/* 15 */    exit(0);
/* 16 */  }

(The variable number is a most-recent holder and sum is a gatherer.)

A one-way flag can also be used, for example, to watch errors occuring in input data so that the program could ask the inputs again.

Icon for the role Temporary

A variable is a temporary if its value is always needed only for a very short period. The example program asks the user for tax-free prices and prints out the total price and the tax for each item. The tax is computed into the temporary tax (on line 10) which is immediately used twice in the next statement (on line 12).

/* 1 */   #include <stdio.h>
/* 2 */   #define TAXRATE 16
/* 3 */   int main()
/* 4 */   {
/* 5 */     float taxfree=1, tax;
/* 6 */     while (taxfree != 0) { 
/* 7 */       printf("Enter tax-free price (0 to quit): ");
/* 8 */       scanf("%f", &taxfree);
/* 9 */       if (taxfree != 0) { 
/* 10*/         tax = taxfree * TAXRATE / 100;
/* 11 */        printf("Total price %.2f that includes tax %.2f\n",
/* 12 */              taxfree+tax, tax);
/* 13 */      }
/* 14 */    } 
/* 15 */    exit(0);
/* 16 */  }

(The variable taxfree is a most-recent holder.)

A temporary is typically used for efficiency reasons (e.g., executing a computation, whose value is needed in several places, only once) or to clarify the program (e.g., computing the result in a variable even though the use of a variable would not be absolutely necessary). A temporary is also often used for swapping two data elements of an organizer.

Icon for the role Organizer

An organizer is a data structure which is used for reorganizing its elements after initialization. The example program asks the user ten characters one by one, assigns them into the organizer word that is implemented as an array, reverses the order of the characters and finally outputs the characters in this reversed order.

/* 1 */   #include <stdio.h>
/* 2 */   int main()
/* 3 */   {
/* 4 */     char word[10], tmp;
/* 5 */     int i;
/* 6 */     printf("Enter ten letters: ");
/* 7 */     for (i = 0; i < 10; i++) scanf("%c", &word[i]);
/* 8 */     for (i = 0; i < 5; i++) {
/* 9 */       tmp = word[i];
/* 10 */      word[i] = word[9-i];
/* 11 */      word[9-i] = tmp;
/* 12 */    }
/* 13 */    for (i = 0; i < 10; i++) printf("%c", word[i]);
/* 14 */    printf("\n");
/* 15 */    exit(0);
/* 16 */  }

(The variable tmp is a temporary and i is a stepper.)

An organizer can be used for sorting or for some other reorganization.

Icon for the role Container

Container is a data structure where elements can be added and removed. The example program implements a stack. The user can add new elements with the command "n" (on lines 11-15) and remove the most recently added element with the command "r" (on lines 16-20). The stack is stored in the array table that acts as a container

/* 1 */   #include <stdio.h>
/* 2 */   #define MAXINDEX 9
/* 3 */   int main()
/* 4 */   {
/* 5 */     int top=-1, table[MAXINDEX];
/* 6 */     int element;
/* 7 */     char command='x';
/* 8 */     while (command != 'q') {
/* 9 */       printf("Enter command: "); scanf(" %c", &command);
/* 10 */      switch (command) {
/* 11 */        case 'n': if (top < MAXINDEX) {
/* 12 */                     printf("Enter element: "); scanf("%d", &element);
/* 13 */                     table[++top] = element;
/* 14 */                  }
/* 15 */                  break;
/* 16 */        case 'r': if (top >= 0) {
/* 17 */                     printf("%d removed.\n", table[top]);
/* 18 */                     top--;
/* 19 */                  }
/* 20 */                  break;
/* 21 */      }
/* 22 */    }
/* 23 */    exit(0);
/* 24 */  }

(The variable top is a walker, element is a most-recent holder and command is a most-recent holder.)

Icon for the role Walker

Walker traverses a data structure. The example program implements a linked list that has a special head node. The user can add new elements with the command "n" (on lines 13-18) and print all the elements in the list with the command "p" (on lines 19-24). The variable p is a walker: it traverses the whole list (on lines 15 and 22).

/* 1 */   #include <stdio.h>
/* 2 */   struct node {int contents; struct node *link;};
/* 3 */   int main()
/* 4 */   {
/* 5 */     struct node *head, *p, *t;
/* 6 */     int element;
/* 7 */     char command='x';
/* 8 */     head = (struct node*) malloc(sizeof(struct node));
/* 9 */     head->link = NULL;
/* 10 */    while (command != 'q') {
/* 11 */      printf("Enter command: "); scanf(" %c", &command);
/* 12 */      switch (command) {
/* 13 */        case 'n': printf("Enter element: "); scanf("%d", &element);
/* 14 */                  p = head;
/* 15 */                  while (p->link) p = p->link;
/* 16 */                  t = (struct node*) malloc(sizeof(struct node));
/* 17 */                  t->contents = element; t->link = NULL; p->link = t;
/* 18 */                  break;
/* 19 */        case 'p': p = head->link;
/* 20 */                  while (p) {
/* 21 */                     printf("%d\n",p->contents);
/* 22 */                     p = p->link;
/* 23 */                  }
/* 24 */                  break;
/* 25 */      }
/* 26 */    }
/* 27 */    exit(0);
/* 28 */  }

(The variable head is a fixed value, t is a temporary, element is a most-recent holder, command is a most-recent holder, the field contents is a fixed value and the field link is a fixed value.)


Last update: 22.1.2006

saja.fi@gmail.com
Marja.Kuittinen@UEF.Fi