In C programming, formatted input and output functions are essential for handling data in a structured and readable manner. These functions allow you to specify how data should be input or displayed by using format specifiers, providing flexibility when dealing with different data types. The two main formatted I/O functions in C are printf()
for output and scanf()
for input.
Table of Contents
Toggle1. Formatted Input withscanf()
The scanf()
function is used to read formatted input from the standard input (usually the keyboard). It reads data according to format specifiers, which specify the type of data being input.
1.1 Syntax of scanf()
int scanf(const char *format, ...);
format
: A string that contains format specifiers....
: A list of pointers to variables where the input data will be stored.
1.2 Format Specifiers in scanf()
The format specifiers used in scanf()
are the same as those used in printf()
. However, you must provide the addresses of the variables (pointers) that will hold the input data.
Common format specifiers:
%d
: Reads an integer.%f
: Reads a floating-point number.%c
: Reads a single character.%s
: Reads a string (a sequence of characters).
Note:
- Strings in
scanf()
do not accept spaces by default. To read strings with spaces, consider usingfgets()
or handling the input differently.
1.3 Examples of scanf()
Write a program to show the usage of whitespace characters in the scan() function.
2. Formatted Output withprintf()
The printf()
function is used to print data to the standard output (usually the console) in a formatted manner. It allows you to control the appearance of the output by specifying various format specifiers that correspond to the types of data you want to display.
2.1 Syntax of printf()
int printf(const char *format, ...);
format
: A string that contains literal text and format specifiers....
: A variable number of arguments that provide the values to be printed, corresponding to the format specifiers.
2.2 Common Format Specifiers
Here are some of the most commonly used format specifiers:
Format Specifier | Description | Example |
---|---|---|
%d or %i |
Integer (decimal notation) | 42 |
%f |
Floating-point number | 3.14159 |
%c |
Single character | 'A' |
%s |
String of characters (null-terminated) | "Hello" |
%x |
Hexadecimal integer (lowercase) | 0x2a |
%X |
Hexadecimal integer (uppercase) | 0x2A |
%o |
Octal integer | 052 |
%u |
Unsigned integer | 42 |
%% |
Print a literal percent sign | % |
2.3 Examples of printf()
Write a program to display an integer, a float, and a string value using a single printf() function.
3. Handling Special Cases
3.1 Reading Multiple Values
You can use multiple format specifiers in scanf()
to read several values from a single input.
int day, month, year;
printf("Enter your birth date (dd mm yyyy): ");
scanf("%d %d %d", &day, &month, &year);
printf("Date of Birth: %02d-%02d-%d\n", day, month, year);
3.2 Error Checking with scanf()
The return value scanf()
indicates how many items were successfully read. This can be useful for error handling.
int result, number;
result = scanf("%d", &number);
if (result == 1) {
printf("Input is valid: %d\n", number);
} else {
printf("Invalid input!\n");
}
Summary
Formatted input and output functions such as printf()
and scanf()
are crucial in C programming for interacting with users and displaying data in a structured way. The flexibility provided by format specifiers allows programmers to control how data is input and output, making these functions highly versatile. Understanding and using these formatted functions effectively is essential for writing efficient and readable C programs.