Prev NEXT

The Basics of C Programming

Printf: Reading User Values

The previous program is good, but it would be better if it read in the values 5 and 7 from the user instead of using constants. Try this program instead:

#include <stdio.h>

int main()
{
    int a, b, c;
    printf("Enter the first value:");
    scanf("%d", &a);
    printf("Enter the second value:");
    scanf("%d", &b);
    c = a + b;
    printf("%d + %d = %d\n", a, b, c);
    return 0;
}
©2004 HowStuffWorks

Here's how this program works when you execute it:

Advertisement

Make the changes, then compile and run the program to make sure it works. Note that scanf uses the same sort of format string as printf (type man scanf for more info). Also note the & in front of a and b. This is the address operator in C: It returns the address of the variable (this will not make sense until we discuss pointers). You must use the & operator in scanf on any variable of type char, int, or float, as well as structure types (which we will get to shortly). If you leave out the & operator, you will get an error when you run the program. Try it so that you can see what that sort of run-time error looks like.

Let's look at some variations to understand printf completely. Here is the simplest printf statement:

printf("Hello");

This call to printf has a format string that tells printf to send the word "Hello" to standard out. Contrast it with this:

printf("Hello\n");

The difference between the two is that the second version sends the word "Hello" followed by a carriage return to standard out.

The following line shows how to output the value of a variable using printf.

printf("%d", b);

The %d is a placeholder that will be replaced by the value of the variable b when the printf statement is executed. Often, you will want to embed the value within some other words. One way to accomplish that is like this:

printf("The temperature is ");
printf("%d", b);
printf(" degrees\n");

An easier way is to say this:

printf("The temperature is %d degrees\n", b);

You can also use multiple %d placeholders in one printf statement:

printf("%d + %d = %d\n", a, b, c);

In the printf statement, it is extremely important that the number of operators in the format string corresponds exactly with the number and type of the variables following it. For example, if the format string contains three %d operators, then it must be followed by exactly three parameters and they must have the same types in the same order as those specified by the operators.

You can print all of the normal C types with printf by using different placeholders:

  • int (integer values) uses %d
  • float (floating point values) uses %f
  • char (single character values) uses %c
  • character strings (arrays of characters, discussed later) use %s

You can learn more about the nuances of printf on a UNIX machine by typing man 3 printf. Any other C compiler you are using will probably come with a manual or a help file that contains a description of printf.