printf, and scanfFormatted input and output (I/O) in C is essential for interacting with users and displaying data in a readable format. The printf and scanf functions are used to handle formatted output and input, respectively. These functions enable you to display data in a specific format and to read user input in a controlled manner. Understanding streams and how to use these functions is vital in C programming.
In C, a stream is a sequence of data elements that are made available for processing. Streams are used to represent input and output channels, such as standard input (keyboard), standard output (screen), and files.
stdin: Standard input (usually the keyboard).stdout: Standard output (usually the screen).stderr: Standard error (used for error messages).These streams are automatically opened when the program starts and can be accessed via functions like scanf, printf, and fprintf.
printfThe printf function is used to print formatted output to the standard output stream (stdout). It allows you to display text, variables, and formatted data in a controlled manner using format specifiers.
printf:int printf(const char *format, ...);
format: A string containing text and format specifiers....: The variables to be printed, corresponding to the format specifiers.printf:%d: Integer (decimal)%f: Floating-point number%s: String%c: Character%x: Hexadecimal integer (lowercase letters)%X: Hexadecimal integer (uppercase letters)%o: Octal integer%p: Pointer (address)%u: Unsigned integer#include <stdio.h>
int main() {
int num = 10;
float pi = 3.14159;
char ch = 'A';
char name[] = "John";
printf("Integer: %d\n", num); // Prints an integer
printf("Floating-point: %.2f\n", pi); // Prints a floating-point number with 2 decimal places
printf("Character: %c\n", ch); // Prints a character
printf("String: %s\n", name); // Prints a string
return 0;
}
%d format specifier is used to print the integer num.%.2f specifier prints pi with 2 decimal places.%c specifier prints the character ch.%s specifier prints the string name.Integer: 10
Floating-point: 3.14
Character: A
String: John
You can specify the width of the output and the precision for floating-point numbers using format specifiers.
%5d prints an integer with a minimum width of 5 characters.%.3f prints a floating-point number with 3 digits after the decimal point.#include <stdio.h>
int main() {
float pi = 3.14159;
// Print with a field width and precision
printf("Pi: %8.3f\n", pi); // Prints " 3.142" (width 8, 3 decimal places)
return 0;
}
%8.3f specifier ensures that the number is printed in a field of width 8, with 3 digits after the decimal point.scanfThe scanf function is used to read formatted input from the standard input stream (stdin). It reads data from the user and stores it in the specified variables.
scanf:int scanf(const char *format, ...);
format: A string containing format specifiers that define the type and layout of the expected input....: The variables where the input values will be stored, matching the format specifiers.scanf:%d: Integer input%f: Floating-point input%s: String input%c: Character input%x: Hexadecimal integer input%u: Unsigned integer input#include <stdio.h>
int main() {
int num;
float pi;
char name[20];
// Reading an integer
printf("Enter an integer: ");
scanf("%d", &num);
// Reading a floating-point number
printf("Enter a floating-point number: ");
scanf("%f", &pi);
// Reading a string
printf("Enter your name: ");
scanf("%s", name);
printf("\nYou entered:\n");
printf("Integer: %d\n", num);
printf("Floating-point: %.2f\n", pi);
printf("Name: %s\n", name);
return 0;
}
scanf("%d", &num) reads an integer and stores it in num.scanf("%f", &pi) reads a floating-point number and stores it in pi.scanf("%s", name) reads a string (up to the first space) and stores it in name.Enter an integer: 10
Enter a floating-point number: 3.14159
Enter your name: John
You entered:
Integer: 10
Floating-point: 3.14
Name: John
You can read multiple values in a single scanf call by specifying multiple format specifiers.
#include <stdio.h>
int main() {
int a, b;
float x;
printf("Enter two integers and a float: ");
scanf("%d %d %f", &a, &b, &x);
printf("\nYou entered:\n");
printf("Integer 1: %d\n", a);
printf("Integer 2: %d\n", b);
printf("Float: %.2f\n", x);
return 0;
}
scanf("%d %d %f", &a, &b, &x) reads two integers and one floating-point number.Enter two integers and a float: 10 20 3.1415
You entered:
Integer 1: 10
Integer 2: 20
Float: 3.14
scanfYou can specify field widths for input in scanf just like with printf. This ensures that the input fits into the specified space.
#include <stdio.h>
int main() {
int num;
float pi;
printf("Enter an integer (max 5 digits): ");
scanf("%5d", &num); // Max 5 digits for integer input
printf("Enter a floating-point number (max 8 characters): ");
scanf("%8f", &pi); // Max 8 characters for floating-point input
printf("\nYou entered:\n");
printf("Integer: %d\n", num);
printf("Floating-point: %.2f\n", pi);
return 0;
}
scanf("%5d", &num) ensures that only 5 digits will be read for the integer.scanf("%8f", &pi) ensures that only 8 characters will be read for the floating-point number.scanfWhitespace Handling: In scanf, whitespace characters (spaces, tabs, newlines) are automatically ignored unless explicitly included in the format string (e.g., %c for a character).
Buffer Overflow: Always ensure that the destination variable (such as a character array) is large enough to hold the input, otherwise, a buffer overflow might occur.
stdin for input, stdout for output, and stderr for errors.printf is used for formatted output, allowing you to control the appearance of the data displayed.scanf is used for formatted input, enabling the program to read data in a structured manner.%d, %f, %s, etc., to specify the type of data being processed.By understanding how to use formatted I/O functions effectively, you can handle user input and output in a flexible, readable, and controlled manner.
Open this section to load past papers