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
- int: an integer
- float: a floating point number
- double: a double-precision float
- char: a single character
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
- %d: a signed integer
- %f: a float
- %e: a float in scientific notation
- %c: a single character
- %s: a string of characters
The precision to which real numbers should be printed out as
can be specified as, for example, %.5f, to print out
5 decimal places.