The % Format Specifiers
The % specifiers that you can use in ANSI C are:
Usual variable type Display
%c char single character
%d (%i) int signed integer
%e (%E) float or double exponential format
%f float or double signed decimal
%g (%G) float or double use %f or %e as required
%o int unsigned octal value
%p pointer address stored in pointer
%s array of char sequence of characters
%u int unsigned decimal
%x (%X) int unsigned hex value
_______________________________________________________________
flag meaning
- left justify
+ always display sign
space display space if there is no sign
0 pad with leading zeros
# use alternate form of specifier
_______________________________________________________________
For example:
printf("%s,Hello")
will print Hello,
printf("%25s ,Hello")
will print 25 characters with Hello right justified and
printf("%25.3s,Hello")
will print Hello right justified in a group of 25 spaces.
Also notice that it is fine to pass a constant value to printf as in printf("%s,Hello").
Finally there are the control codes:
\b backspace
\f formfeed
\n new line
\r carriage return
\t horizontal tab
\' single quote
\0 null
________________________________________________________________
There are five basic data types associated with variables:
* int - integer: a whole number.
* float - floating point value: ie a number with a fractional part.
* double - a double-precision floating point value.
* char - a single character.
* void - valueless special purpose type which we will examine closely in later sections.
One of the confusing things about the C language is that the range of values and the amount of storage that each of these types takes is not defined. This is because in each case the 'natural' choice is made for each type of machine. You can call variables what you like, although it helps if you give them sensible names that give you a hint of what they're being used for - names like sum, total, average and so on. If you are translating a formula then use variable names that reflect the elements used in the formula. For example, 2pr (that should read as "2 pi r" but that depends upon how your browser has been set-up) would give local variables names of pi and r. Remember, C programmers tend to prefer short names!
Note: all C's variables must begin with a letter or a "_" (underscore) character.