Graphics Programs Reference
In-Depth Information
The final line just shows the address of the variable A , using the unary
address operator to dereference the variable. This value is displayed as eight
hexadecimal digits, padded by zeros.
As these examples show, you should use %d for decimal, %u for unsigned,
and %x for hexadecimal values. Minimum field widths can be set by putting a
number right after the percent sign, and if the field width begins with 0, it
will be padded with zeros. The %s parameter can be used to print strings and
should be passed the address of the string. So far, so good.
Format strings are used by an entire family of standard I/O functions,
including scanf() , which basically works like printf() but is used for input
instead of output. One key difference is that the scanf() function expects all
of its arguments to be pointers, so the arguments must actually be variable
addresses—not the variables themselves. This can be done using pointer
variables or by using the unary address operator to retrieve the address of the
normal variables. The input.c program and execution should help explain.
input.c
#include <stdio.h>
#include <string.h>
int main() {
char message[10];
int count, i;
strcpy(message, "Hello, world!");
printf("Repeat how many times? ");
scanf("%d", &count);
for(i=0; i < count; i++)
printf("%3d - %s\n", i, message);
}
In input.c, the scanf() function is used to set the count variable. The output
below demonstrates its use.
reader@hacking:~/booksrc $ gcc -o input input.c
reader@hacking:~/booksrc $ ./input
Repeat how many times? 3
0 - Hello, world!
1 - Hello, world!
2 - Hello, world!
reader@hacking:~/booksrc $ ./input
Repeat how many times? 12
0 - Hello, world!
1 - Hello, world!
2 - Hello, world!
3 - Hello, world!
4 - Hello, world!
5 - Hello, world!
6 - Hello, world!
Search WWH ::




Custom Search