HP C/iX Library Reference Manual (30026-90004)

Chapter 5 275
HP C/iX Library Function Descriptions
printf
main()
{
double x;
printf("Enter your number: ");
printf("Your number is %g\n", x);
printf("Its square is %g\nIts cube is %g\n", x*x, x*x*x); }
The g conversion character is used so that the decision about using an exponent is
automatic. Note that the item list contains expressions to calculate x squared and x cubed.
Also note that the address of the variable is required in order to read a value for it with
scanf(), but printf() requires the variable name itself.
The following program accepts a decimal integer and prints the integer itself, its square,
and its cube in decimal, octal, and hexadecimal. The program also prints the headings
"Decimal," "Octal," and "Hexadecimal" and prints the data in tabular form.
#include <stdio.h>
main()
{
long n, n2, n3;
/* get value */
printf("Enter your number: ");
/* print headings */
printf("\n\n Decimal Octal Hexadecimal\n");
/* do the computation */
n2=n*n;
n3=n*n*n;
printf("n itself: %7ld %9lo %6lx\n", n, n, n);
printf("n squared: %7ld %9lo %6lx\n", n2, n2, n2);
printf("n cubed: %7ld %9lo %6lx\n", n3, n3, n3);
}
Strings are easy to manipulate using the printf() function. The following program shows
how strings can be inserted in text.
#include <stdio.h>
main()
{
char first[15], last[25];
printf("Enter your first and last names: ");
scanf("%s%s", first, last);
printf("\nWell, hello %s, it's good to meet you!\n", first);
printf("%s, huh? Are you any relation to that famous\n", last);
printf("computer programmer, Mortimer Zigfelder %s?\n", last);
printf("No, sorry, that was my mistake. I was thinking of\n");