Prev:
Standard Input and Output
Next:
More on Format Specifiers
At last! Here's that function I keep using in my examples. It gives you the power to print output onto the screen, and is relatively simple to use.
The number of arguments required varies, but the first argument you pass should be a STRING - think of a string as a sequence of characters for now.
Recall that a string must be surrounded by double quote marks.
Here's printf in action once again:
printf("Hello World!\n");
Notice how the string, "Hello World!\n" is enclosed in double quote marks. I've said it at least once, but I'll say it again: the \n is the NEWLINE character and acts like a line break!
In the past, I've provided examples where a value of some variable was printed out.
To do so, requires you to embed a format specifier in your text string, and to pass extra arguments to the printf function. An example:
printf("x equals %d \n", x);
This statement prints the value of x out. I had to pass the value of x into the printf function. When you pass arguments to
functions, you separate each one with a comma - here,
"x equals %d \n" is an argument, so is x.
There are several format specifiers - the one you use should depend on the type of the variable you wish to print out. Here are the common ones:
|
Format Specifier |
Type |
|---|---|
|
%d (or %i) |
int |
|
%c |
char |
|
%f |
float |
|
%lf |
double |
|
%s |
string |
| %x | hexadecimal |
To display a number in scientific notation, use %e. To display a percent sign, use %%. Don't worry about the hexadecimal one yet - we'll get to it much later!
%d is essentially the same as %i but I used %d from the very first day. I guess it's just down to personal preference. In case you're wondering, LF stands for "long float".
Don't try to display a decimal number using the integer format specifier, %d, as this displays unexpected values! Similarly, don't use %f for displaying integers. Mixing %d with char variables, or %c with int variables is all right, as shown in this example:
#include <stdio.h>
int main() {
int a = 72;
char b = 'A';
printf("a equals %d \n", a);
printf("a equals %c \n", a);
printf("b equals %d \n", b);
printf("b equals %c \n", b);
}
a equals 72 |
The reason why this works is because a character constant is just an integer from 0 to 255.
You could use as many format specifiers as you want with printf - just as long as you pass the correct number of arguments.
The ordering of the arguments matters. The first one should correspond to the first format specifier in the string and so on. Take this example:
printf("a=%d, b=%d, c=%d\n", a,b,c);
If a, b and c were integers, this statement will print the values in the correct order. Rewriting the statement as...
printf("a=%d, b=%d, c=%d\n", c,a,b);
... would still cause the program to compile OK, but the values of a, b and c would be displayed in the wrong order!
I've still yet to use this function in my examples. It's a lot more flexible to use than the getchar and getc functions, simply because you can input numbers and strings, as well as characters.
Have a look at this:
printf("Enter a number ");
printf(" and press Enter: ");
scanf("%d", &a);
Before I move on, I'll mention briefly that the & is known as the ADDRESS-OF operator - you'll be using it a lot more when you meet POINTERS at a later date!
Back to the example. scanf takes at least two arguments.
The first one is a string that can consist of format specifiers. The rest of the arguments should be variable names preceded with the address-of operator. Try to picture the previous scanf statement by this: "Read in an integer from the input string, then go to the address of the variable called a and put the value there". Remember that a variable is like a container in your computer's memory - each one has a different address.
Like with printf, the number of arguments after the string argument should match the number of format specifiers contained in that string.
Similarly, the type of the format specifier should match the type of the corresponding variable. The ordering of the variables also matters.
If you have multiple format specifiers within the string argument of scanf, you can input multiple values. All you need to do is to separate each format specifier with a DELIMITER - a string that separates variables. For convenience, the delimiter should be one character that's a punctuation mark, like a comma or a space. As a default, scanf stops reading in a value when space, tab or Enter is pressed.
Consider scanf("%d %d", &x, &y);
(Assume that x and y have been declared beforehand!).
If I entered: 1 2 and pressed Enter, 1 would get assigned to x, and 2 would get assigned to y.
But if I entered 1, 2 and pressed Enter, x would equal 1, but y won't get assigned 2 because scanf was not expecting a comma in the input string.
Now consider:
scanf("%d, %d, %d", &x,&y,&z);
If I entered 1 2 3 and pressed enter 1 would get assigned to x but 2 and 3 won't get assigned to y or z, simply because I didn't separate the numbers with commas.
Entering 1,2,3 works, but why does 1, 2, 3 also work? scanf ignores spaces, tabs and carriage returns immediately after the delimiters.
Just don't put a space, tab or carriage return before the delimiter! 1 ,2, 3 won't work.
If you want the user to press return after each number, try something along the lines as:
scanf("%d\n%d\n%d", &x,&y,&z);
Note that you shouldn't put a delimiter after the last format specifier!
This is probably past the novice stage, but if you wanted to enter a value for a double variable using scanf, you need to use %lf as the format specifier - it's the specifier for a long float variable.
I think we've covered enough material for you to apply your knowledge in a few mini exercises!
No answers will be given (there is no single correct way of doing things). I found, through my own personal experience, that if you weren't given the answer, you'll spend more time trying to work things out, thus improving your learning. Take things step by step!
1a. Write a program that declares four variables in total (one of each data type).
1b. Initialize each variable, then use 4 printfs to display each value.
1c. Make sure there's a line break after each line.
1d. Now modify the program so that you use scanf to fetch input for each variable. Once stored, print them out (tip: use %lf for doubles).
2. Recall an earlier example:
#include <stdio.h>
int main() {
printf("%c%c%c", 105, 111, 116);
printf("%c%c%c", 97, 45, 115);
printf("%c%c%c", 105, 120, 46);
printf("%c%c%c", 99, 111, 46);
printf("%c%c%c", 117, 107, 10);
return 0;
}
Write a similar program that displays your name.
3a. Write a program that requests input for 2 integers a and b.
3b. Using printf, display the sum of a and b.
3c. Using another printf statement, display the product of a and b.
3d. Then display the remainder of a divided by b.
3e. Comment out the printf statement that displays the product (deleting the printf statement is not acceptable!)
Prev:
Standard Input and Output
Next:
More on Format Specifiers
www.iota-six.co.uk
Copyright © 2001-2003
Unauthorized copying not permitted
Designed and Developed Using Macromedia Studio MX