C Data Input and Data Output

In most programs you create, you will need to display information on the screen or read information from the keyboard. Many of the programs presented in earlier chapters performed these tasks, but you might not have understood exactly how. Today you will learn

The basics of C's input and output statements

How to display information on-screen with the printf() and puts() library functions

How to format the information that is displayed on-screen

How to read data from the keyboard with the scanf() library function

This chapter isn't intended to be a complete treatment of these topics, but it provides enough information so that you can start writing real programs.

Displaying Information On-Screen

You will want most of your programs to display information on-screen. The two most frequently used ways to do this are with C's library functions printf() and puts().

The printf() Function

The printf() function, part of the standard C library, is perhaps the most versatile way for a program to display data on-screen. You've already seen printf() used in many of the examples in this book. Now you will to see how printf() works.

Printing a text message on-screen is simple. Call the printf() function, passing the desired message enclosed in double quotation marks. For example, to display An error has occurred! on-screen, you write

printf("An error has occurred!");

In addition to text messages, however, you frequently need to display the value of program variables. This is a little more complicated than displaying only a message. For example, suppose you want to display the value of the numeric variable x on-screen, along with some identifying text. Furthermore, you want the information to start at the beginning of a new line. You could use the printf() function as follows:

printf("\nThe value of x is %d", x);

The resulting screen display, assuming that the value of x is 12, would be

The value of x is 12

In this example, two arguments are passed to printf(). The first argument is enclosed in double quotation marks and is called the format string. The second argument is the name of the variable (x) containing the value to be printed.

The printf() Format Strings

A printf() format string specifies how the output is formatted. Here are the three possible components of a format string:

Literal text is displayed exactly as entered in the format string. In the preceding example, the characters starting with the T (in The) and up to, but not including, the % comprise a literal string.

An escape sequence provides special formatting control. An escape sequence consists of a backslash (\) followed by a single character. In the preceding example, \n is an escape sequence. It is called the newline character, and it means "move to the start of the next line." Escape sequences are also used to print certain characters. Escape sequences are listed in Table 7.1.

A conversion specifier consists of the percent sign (%) followed by a single character. In the example, the conversion specifier is %d. A conversion specifier tells printf() how to interpret the variable(s) being printed. The %d tells printf() to interpret the variable x as a signed decimal integer.

The most frequently used escape sequences.Refer Appendix.

Table:The most commonly needed conversion specifiers.

Specifier 	Meaning 						Types 
%c 			Single character 				char
%d 			Signed decimal 					integer,int, short
%ld 		Signed long decimal 			long
%f 			Decimal floating-point 			float, double
%s 			Character string 				char arrays
%u 			Unsigned decimal integer 		unsigned int, unsigned short
%lu 		Unsigned long decimal integer 	unsigned long

The literal text of a format specifier is anything that doesn't qualify as either an escape sequence or a conversion specifier. Literal text is simply printed as is, including all spaces.

Example : Using printf() to display numerical values.

/* Demonstration using printf() to display numerical values. */
#include <stdio.h>
int a = 2, b = 10, c = 50;
float f = 1.05, g = 25.5, h = -0.1;
	printf("\nDecimal values without tabs: %d %d %d", a, b, c);
	printf("\nDecimal values with tabs: \t%d \t%d \t%d", a, b, c);
	printf("\nThree floats on 1 line: \t%f\t%f\t%f", f, g, h);
	printf("\nThree floats on 3 lines: \n\t%f\n\t%f\n\t%f", f, g, h);
	printf("\nThe rate is %f%%", f);
	printf("\nThe result of %f/%f = %f\n", g, f, g / f);
	return 0;

Sample Output:

Decimal values without tabs: 2 10 50

Decimal values with tabs:    2    10   50

Three floats on 1 line:     1.050000    25.500000    -0.100000

Three floats on 3 lines:




The rate is 1.050000%

The result of 25.500000/1.050000 = 24.285715

Program Analysis:

Listing example prints six lines of information. Lines 10 and 11 each print three decimals: a, b, and c. Line 10 prints them without tabs, and line 11 prints them with tabs. Lines 13 and 14 each print three float variables: f, g, and h. Line 13 prints them on one line, and line 14 prints them on three lines. Line 16 prints a float variable, f, followed by a percent sign. Because a percent sign is normally a message to print a variable, you must place two in a row to print a single percent sign. This is exactly like the backslash escape character. Line 17 shows one final concept. When printing values in conversion specifiers, you don't have to use variables. You can also use expressions such as g / f, or even constants

The printf() Function

#include <stdio.h>

printf( format-string[,arguments,...]);

printf() is a function that accepts a series of arguments, each applying to a conversion specifier in the given format string. printf() prints the formatted information to the standard output device, usually the display screen. When using printf(), you need to include the standard input/output header file, STDIO.H.

The format-string is required; however, arguments are optional. For each argument, there must be a conversion specifier. Table lists the most commonly used conversion specifiers.

The following are examples of calls to printf() and their output:

printf() Example 1:

#include <stdio.h>
  printf("This is an example of something printed!");
  return 0;

Example 1 Output
This is an example of something printed!

printf() Example 2:

printf("This prints a character, %c\na number, %d\na floating \

point, %f", `z', 123, 456.789 );

Example 2 Output

This prints a character, z

a number, 123

a floating point, 456.789

Displaying Messages with puts()

The puts() function can also be used to display text messages on-screen, but it can't display numeric variables. puts() takes a single string as its argument and displays it, automatically adding a newline at the end. For example, the statement

puts("Hello, world.");

performs the same action as

printf("Hello, world.\n");

The puts() Function

#include <stdio.h>

puts( string );

puts() is a function that copies a string to the standard output device, usually the display screen. When you use puts(), include the standard input/output header file (STDIO.H). puts() also appends a newline character to the end of the string that is printed. The for- mat string can contain escape sequences. The following are examples of calls to puts() and their output:

puts Example 1:

puts("This is printed with the puts() function!");

Example 1 Output

This is printed with the puts() function!

puts Example 2:

puts("This prints on the first line. \nThis prints on the second line.");

puts("This prints on the third line.");

puts("If these were printf()s, all four lines would be on two lines!");

Example 2 Output

This prints on the first line.

This prints on the second line.

This prints on the third line.

If these were printf()s, all four lines would be on two lines!

The scanf() Function

#include <stdio.h>

scanf( format-string[,arguments,...]);

scanf() is a function that uses a conversion specifier in a given format-string to place values into variable arguments. The arguments should be the addresses of the variables rather than the actual variables themselves. For numeric variables, you can pass the address by putting the address-of operator (&) at the beginning of the variable name. When using scanf(), you should include the STDIO.H header file.

scanf() reads input fields from the standard input stream, usually the keyboard. It places each of these read fields into an argument. When it places the information, it converts it to the format of the corresponding specifier in the format string. For each argument, there must be a conversion specifier. Table lists the most commonly needed conversion specifiers.

scanf () Example 1

int x, y, z;

scanf( "%d %d %d", &x, &y, &z);

scanf () Example 2

#include <stdio.h>

main() {

  float y;

  int x;

  puts( "Enter a float, then an int" );

  scanf( "%f %d", &y, &x);

  printf( "\nYou entered %f and %d ", y, x );

  return 0;



Enter a float, then an int 10.67 100

You entered 10.67 and 100


Top Pages