Hardware Reference
In-Depth Information
The standard C printf statement is, however, more versatile than its equivalent
in BASIC as it allows a wide variety of formatting variations. They include:
\ h
for backspace
\ f
for form feed
\ n
for new line
\ t
for tab
The following example prints the message 'Warning!' immediately preceded
and immediately followed by two blank lines:
printf(" \ n \ nWarning! \ n \ n");
Variables can be included within the formatted print statement, as the following
example shows:
printf("Tank number %d temperature %d \ n", tankno, temp);
The current values of tankno and temp are printed as integer decimal numbers
within the string. Thus if tankno and temp currently had the values 4 and 56,
the resulting output generated would be:
Tank number 4 temperature 56
C allows a wide range of conversion characters to be included within formatted
print strings. These usually include:
%c for single character
%d for signed decimal
%o for unsigned octal
%s for string
%u for unsigned decimal
%x for lower-case hexadecimal
%X for upper-case hexadecimal.
The following example shows how conversion specifiers can be used to print
the decimal, hexadecimal, and octal value of the same number:
printf("Decimal %d, hexadecimal %X, octal %o", num, num, num);
It is important to note that each conversion specifier must correspond to an
argument within the list. The following code fragment prints the hexadecimal
and octal equivalents of the decimal number, 191:
/* bconv1.c */
main()
{
int num;
num = 191;
printf("Decimal %d, Hex %X, octal %o", num, num, num);
}
Loops
Loops can be easily implemented in C programs. The program that follows
(written in Borland C ++ 4.5 - see Figure 7.5) prints the ASCII character
set and uses the %d , %X , and %c conversion specifiers to provide the decimal,
hexadecimal, and ASCII representation of the loop index ( byte ). The loop
is executed for byte values in the range 32 to 127 and the variable byte is
Search WWH ::




Custom Search