Title: printf() and scanf() in C
Table of Contents
- Introduction to printf() and scanf()
- The printf() Function
- The scanf() Function
- Printf() and Scanf() together
- Conclusion
Introduction to printf() and scanf()
In C programming, printf() and scanf() are two essential functions for handling input and output. They are part of the standard input/output library (stdio.h) and are used for printing formatted output and reading input from the user, respectively.
The printf() Function
The printf() function is used to display formatted output on the screen or to a file. It accepts a format string and optional arguments that will be inserted into the format string according to the specified format specifiers.
Format Specifiers
| Format Specifier | Description |
|---|---|
| %d | Print an integer |
| %f | Print a floating-point number |
| %c | Print a character |
| %s | Print a string |
| %p | Print a pointer address |
| %% | Print a percent sign |
Escape Sequences
| Escape Sequence | Description |
|---|---|
| \n | Newline (move to the next line) |
| \t | Horizontal tab (insert tab space) |
| \r | Carriage return (move to the beginning) |
| \\ | Backslash (print a backslash) |
| \" | Double quote (print a double quote) |
| \' | Single quote (print a single quote) |
Examples
#include <stdio.h>
int main() {
int num = 42;
float pi = 3.14159;
char letter = 'A';
char name[] = "John";
printf("Integer: %d\n", num);
printf("Float: %.2f\n", pi);
printf("Character: %c\n", letter);
printf("String: %s\n", name);
return 0;
}
Output:
Integer: 42
Float: 3.14
Character: A
String: John
The scanf() Function
The scanf() function is used to read input from the user or from a file. It requires format specifiers to match the input data type and stores the input values into the specified variables.
Format Specifiers for Input
| Format Specifier | Description |
|---|---|
| %d | Read an integer |
| %f | Read a floating-point number |
| %c | Read a character |
| %s | Read a string (ends with a space or newline) |
Examples
#include <stdio.h>
int main() {
int age;
float weight;
char initial;
char name[50];
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your weight: ");
scanf("%f", &weight);
printf("Enter your initial: ");
scanf(" %c", &initial);
printf("Enter your name: ");
scanf("%s", name);
printf("Age: %d\n", age);
printf("Weight: %.2f\n", weight);
printf("Initial: %c\n", initial);
printf("Name: %s\n", name);
return 0;
}
Input:
Enter your age: 25
Enter your weight: 68.5
Enter your initial: J
Enter your name: Alice
Output:
Age: 25
Weight: 68.50
Initial: J
Name: Alice
Printf() and Scanf() together
You can use both printf() and scanf() together to create interactive programs. For example:
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("The sum is: %d\n", sum);
return 0;
}
Input:
Enter the first number: 10
Enter the second number: 20
Output:
The sum is: 30
Conclusion
In this post, we discussed the printf() and scanf() functions in C programming. The printf() function is used to print formatted output, while the scanf() function is used to read input from the user. Understanding these functions is crucial for creating interactive programs that can display output and gather input from users effectively. With this knowledge, you can now build more sophisticated C programs that involve input and output operations.
Happy coding!
0 Comments