Panel For Example Panel For Example Panel For Example

C Input and Output Functions

Author : Adrian January 06, 2026

Overview

Programs are written to have computers perform calculations or process information. Whether performing calculations or processing information, a program must obtain input data, process or compute that data, and then output the results to the user.

For example, when writing a program to compute the area of a circle, the constant PI can be declared and does not require user input, while the circle's radius varies and must come from user input. After computing the area, the program should output the result to the user.

Input and output functions implement the input and output capabilities. A function is a block of prewritten C code that performs a specific task. A function can be treated as a black box: callers only need to know the function's purpose and how to invoke it, not its internal implementation. A function has inputs and outputs; other statements in the program can call the function, passing parameters (inputs) and receiving return values (outputs).

Further details on functions are covered in later chapters.

The C language places provided functions in libraries. When a program needs to call a library function, the library's header file must be included in the source file. C's input and output functions are part of the standard library; their header is "stdio.h". To use input/output functions in a source file, add the following directive near the top of the file:

#include <stdio.h>

This directive informs the compiler that the source file uses C standard library input/output functions and ensures the compiler handles them correctly.

 

Output functions

The standard C output function is printf(), which outputs content to the console according to a specified format. The console is a command-line window that primarily supports text-based programs.

The general form of printf() is:

printf("format string", variable list)

printf() accepts two arguments: the format string and the variable list.

The first argument specifies how to format the output; it is a string literal. The format string can contain multiple placeholders positioned as needed. Each placeholder in the format string corresponds in order to a variable in the variable list. The number of placeholders must match the number of variables provided.

For example:

double dTemp = 129.6789603;printf("%s:%.2fn", "This is the data output by the printf function",dTemp);

In printf() the first argument "%s:%.2f" is the format string. "%s" is a format specifier that outputs a string taken from the variable list following the format string. "%.2f" is a format specifier for a floating-point number, displayed with 2 digits after the decimal point. Changing the number after the decimal point adjusts the displayed precision. The corresponding values are taken from the variable list after the format string.

The variable list follows the format string and is comma-separated. Since the format string "%s:%.2f" contains two specifiers, two variables must be provided and their order must match the order of specifiers: the string literal "This is the data output by the printf function" corresponds to %s, and dTemp corresponds to %.2f.

The example shows that printf can control the number of decimal places printed for floating-point values.

Common printf format specifiers include:

% %c single character %d decimal integer %f floating-point number %o octal number %s string %u unsigned decimal %x hexadecimal %% percent sign%

Common printf format controls:

%0m.n format characters

Explanation of components:

% is the required start of the format specifier.

0 indicates that empty spaces should be filled with zeros; omitting it leaves empty spaces blank.

m.n: m is the field width, the number of character positions the output should occupy. n is the precision, used to set the number of digits after the decimal point for floating-point numbers. If n is omitted, the default precision is 6. Examples: "%.2f" prints a floating number with 2 decimal places; "%9.2f" prints in a field width of 9 with 2 decimal places; "%09.2f" prints in a field width of 9 with leading zeros to fill the field and 2 decimal places.

Example:

char chTemp = 'a';double dTemp = 129.6789603;printf("%s:%09.2fn", "Output floating-point variables", dTemp);printf("%s:%c:ndecimalism:%d:nhexadecimal:%xn", "Output character variables", chTemp, (int) chTemp, (int) chTemp);

This example applies %c, %d and %x to output a character variable as a character, as a decimal numeric value, and as a hexadecimal numeric value, respectively. The casts to (int) force the character to be converted to its numeric value.

The format string in the examples includes the newline escape sequence "\n". The backslash "\" introduces an escape sequence, where the character following "\" has a special meaning. Common escape sequences are listed below.

Escape sequences

Escape sequence Description
\' single quote
\" double quote
\? question mark
\\ backslash
\a bell
\b backspace
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab

 

Input functions

When a program needs to receive input from the keyboard, it can use scanf() and related functions. When scanf() is used, the console waits for user input until the user presses Enter, which ends the single input operation.

The general form of scanf() is:

scanf("format string", variable address list)

The format string in the first argument is similar to printf() and can contain one or more conversion specifiers starting with %. These conversion specifiers indicate how input should be parsed and converted. The second argument is a list of variable addresses, written by prefixing variable names with the & operator to obtain their addresses. The function stores the converted user input into the variables at those addresses.

Common conversion specifiers for scanf()

Conversion specifier Variable type Input type
%d int decimal integer
%ld long long decimal integer
%f float decimal number, may include a fractional part
%lf double decimal number, may include a fractional part

Example:

int n; double x;float y; scanf("%d %lf %f",&n,&x,&y);

In this example, an int n, a double x, and a float y are declared to receive user input. scanf converts the user-entered data according to the specified types and assigns the converted values to the three variables in order. If the input data does not match the expected format, input fails. Values should be separated by spaces, Enter, or Tab.

 

Example: Input integer and floating-point and then output

Source file: sample.c

#includeint main(){ // Declare an integer variable to receive the input integer int nTemp; // Declare a double-precision floating-point variable to receive the input decimal double dTemp; // Receive user input scanf("%d %lf",&nTemp,&dTemp); // Output the values of nTemp and dTemp printf("The input integer is :%dn The input decimal is:%.2f",nTemp,dTemp); return 0;}