printf() and scanf() Functions in C
In C programming, printf()
and scanf()
are the most commonly used functions for performing input and output (I/O) operations. These functions are part of the Standard Input/Output Library (stdio.h
), which needs to be included in your program to use them.
1. printf()
Function
The printf()
function is used to output (print) data to the standard output device, typically the console.
Syntax:
printf(format_string, argument_list);
format_string
: A string containing text and format specifiers.argument_list
: Values to be printed that correspond to the format specifiers.
Common Format Specifiers:
Specifier | Meaning |
---|---|
%d | Integer |
%f | Floating-point number |
%c | Single character |
%s | String (sequence of characters) |
%lf | Double-precision floating-point |
%x , %X | Hexadecimal integer |
%o | Octal integer |
Example:
#include <stdio.h>
int main() {
int students = 100;
float pass_percentage = 75.5;
char grade = 'A';
char founder[] = "Pawan Jaiswal";
printf("Number of students: %d\n", students);// Integer output
printf("Pass percentage: %.2f%%\n", pass_percentage);// Float with two decimal places
printf("Grade: %c\n", grade);// Character output
printf("Founder: %s\n", founder);// String output
return 0;
}
Output:
Number of students: 100
Pass percentage: 75.50%
Grade: A
Founder: Pawan Jaiswal
Explanation:
%d
: Prints an integer.%.2f
: Prints a floating-point number with 2 decimal places.%c
: Prints a single character.%s
: Prints a string.
2. scanf()
Function
The scanf()
function is used to get input from the user and store it in variables.
Syntax:
scanf(format_string, &variable_list);
format_string
: A string containing format specifiers.variable_list
: The addresses of the variables where the input will be stored.
Common Format Specifiers:
Specifier | Meaning |
---|---|
%d | Integer |
%f | Floating-point number |
%c | Single character |
%s | String (no need to use & with strings) |
%lf | Double-precision floating-point |
Example:
#include <stdio.h>
int main() {
int students;
float pass_percentage;
char grade;
char founder[50];
// Input for integer, float, character, and string
printf("Enter the number of students: ");
scanf("%d", &students);// Integer input
printf("Enter the pass percentage: ");
scanf("%f", &pass_percentage);// Float input
printf("Enter the grade: ");
scanf(" %c", &grade);// Character input (note the space before %c)
printf("Enter the founder's name: ");
scanf("%s", founder);// String input (no & needed for string)
// Output the values entered
printf("Number of students: %d\n", students);
printf("Pass percentage: %.2f\n", pass_percentage);
printf("Grade: %c\n", grade);
printf("Founder: %s\n", founder);
return 0;
}
Output (Example of user input):
Enter the number of students: 100
Enter the pass percentage: 85.75
Enter the grade: A
Enter the founder's name: Pawan
Number of students: 100
Pass percentage: 85.75
Grade: A
Founder: Pawan
Explanation:
%d
: Reads an integer.%f
: Reads a floating-point number.%c
: Reads a single character (space before%c
is used to ignore any newline left in the buffer).%s
: Reads a string (does not require the&
operator).
Important Points About printf()
and scanf()
- Format Specifiers:
- Make sure the format specifier matches the type of variable you are using, or the output/input may be incorrect.
- Example: Use
%d
for integers,%f
for floats,%s
for strings, etc.
- Reading Input:
scanf()
requires the address of the variable using the&
operator, except for strings.- Always leave a space before
%c
inscanf()
when reading a character after other inputs to handle newline characters left in the buffer.
- Handling Strings:
- In
scanf()
, string input stops at the first whitespace character (space, tab, or newline). - To read multi-word strings, use
gets()
(deprecated) orfgets()
.
- In
- Floating-Point Precision:
- You can control the number of decimal places displayed using
%.nf
inprintf()
wheren
is the number of decimal places.
- You can control the number of decimal places displayed using
- Escape Sequences:
- Use escape sequences in
printf()
to format the output:\n
: Newline\t
: Tab\\
: Backslash%%
: Prints a percent sign
- Use escape sequences in
Advanced Example Using printf()
and scanf()
#include <stdio.h>
int main() {
int employees;
float revenue;
char grade;
char company_name[50] = "SamagraCS Educational Technology";
char founder_name[30];
// Input section
printf("Enter the number of employees: ");
scanf("%d", &employees);
printf("Enter the revenue in millions: ");
scanf("%f", &revenue);
printf("Enter the grade of the company: ");
scanf(" %c", &grade); // Note the space before %c
printf("Enter the founder's name: ");
scanf("%s", founder_name);
// Output section
printf("\nCompany Name: %s\n", company_name);
printf("Number of employees: %d\n", employees);
printf("Revenue: $%.2f million\n", revenue);
printf("Company Grade: %c\n", grade);
printf("Founder: %s\n", founder_name);
return 0;
}
Output:
Enter the number of employees: 150
Enter the revenue in millions: 12.75
Enter the grade of the company: A
Enter the founder's name: Pawan Jaiswal
Company Name: SamagraCS Educational Technology
Number of employees: 150
Revenue: $12.75 million
Company Grade: A
Founder: Pawan Jaiswal