In this article, we provide information about the printf() function in C programming, which is used to display output on the screen.
printf() Function in C Programming
Introduction:
In C, we can read and write characters or data on the screen using the printf() and scanf() functions. However, these functions are not suitable for all situations — for example, when reading individual characters or handling formatted input/output.
Formatted Output in C
Overview:
Reading, processing, and writing data are the three essential operations of any computer program.
Most programs take some input data, process it, and then display the result as output (information).
In C, data can be assigned to variables in two ways:
- Using assignment statements (e.g., x = 5; a = 0;)
- Using input/output functions such as scanf() and printf()
All standard input and output operations in C are handled by library functions, which are part of the Standard I/O Library (stdio.h).
The printf() Function:
The printf() function is used to display text or data on the output screen (monitor).
It is a formatted output function, meaning that it allows the programmer to control the format of the output.
Syntax:
printf(“control string”, arg1, arg2, …, argn);
- control string: A string containing text, escape sequences, and format specifiers (like %d, %f, %c, %s).
- arg1, arg2, …: Variables or values to be printed, corresponding to the format specifiers.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 10;
printf(“Sum of %d and %d is %d”, a, b, a + b);
return 0;
}
Output:
Sum of 5 and 10 is 15
Reading Characters: The getchar() Function
Sometimes, we need to read a single character from the keyboard instead of formatted data.
In such cases, we use the getchar() function.
About getchar():
- It is a built-in function available in the h header file.
- It reads one character at a time from the keyboard and stores it in a variable.
Syntax:
variable_name = getchar();
When this statement is executed, the computer waits for a key to be pressed and stores that character in the specified variable.
Example:
#include <stdio.h>
int main() {
char ch;
printf(“Enter a character: “);
ch = getchar();
printf(“Character entered: “);
putchar(ch);
return 0;
}
Output:
Enter a character: a
Character entered: a
Formatted Input in C
Definition:
Formatted input refers to data that is entered in a specific structure or pattern.
For example:
15.75 123 Rajveer
This line contains three types of data — a float, an integer, and a string — and must be read accordingly.
C provides the scanf() function to read such data in a specified format.
The scanf() Function:
The scanf() function is used to read formatted input from the keyboard.
The term scanf stands for “scan formatted”.
Syntax:
scanf(“control string”, &arg1, &arg2, …, &argn);
- control string: Specifies the format in which the input is expected (e.g., %d, %f, %s, etc.)
- &arg1, &arg2, …: Addresses of the variables where the input values will be stored.
(Note: The ampersand & is used to pass the memory address of the variable.)
Example Program:
#include <stdio.h>
int main() {
int length, breadth, area;
printf(“Enter the length of the rectangle: “);
scanf(“%d”, &length);
printf(“Enter the breadth of the rectangle: “);
scanf(“%d”, &breadth);
area = length * breadth;
printf(“Area of the rectangle: %d”, area);
return 0;
}
Output:
Enter the length of the rectangle: 5
Enter the breadth of the rectangle: 4
Area of the rectangle: 20
Key Points Summary:
| Function | Purpose | Header File | Example |
| printf() | Displays (prints) formatted output on the screen | stdio.h | printf(“%d”, a); |
| scanf() | Reads formatted input from the keyboard | stdio.h | scanf(“%d”, &a); |
| getchar() | Reads a single character from keyboard | stdio.h | ch = getchar(); |
| putchar() | Displays a single character on the screen | stdio.h | putchar(ch); |
Conclusion:
The printf() and scanf() functions are fundamental to C programming.
They allow interaction between the user and the computer by handling input and output efficiently.
For reading individual characters, functions like getchar() and putchar() are used.
Understanding these functions is essential for mastering formatted input/output operations in C.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining