next up previous contents index
Next: Arrays Up: Variables Previous: Variables   Contents   Index


Scalar Variables

In C, simple (scalar) variables have the same basic naming conventions as used in Perl, but without a `$' sign prepended. Variables used in your program must be declared, and this also includes their type. Some common types recognized include

Declaration of variables can also include an initial assignment:

  int count = 0;
  float pi;
  char ans = 'y';

When using a printf statement to print out variables, the format appropriate for the variable should be included:

  printf("Count is now at %d", count);
  printf("Pi has a value of %f", pi);
Some common format strings used in a printf statement are The precision to which real numbers should be printed out as can be specified as, for example, %.5f, to print out 5 decimal places.