C Input Output: printf() and scanf()

C Input Output: printf() and scanf()

3 mins read978 Views Comment
Updated on Feb 7, 2023 09:35 IST

In C programming, printf() and scanf() are two of the most commonly used standard input/output functions.

2023_02_MicrosoftTeams-image-276.jpg

In this article, we will discuss how these two functions (printf() and scanf()) are used for I/O in C, along with examples.  

Before we begin exploring, printf() and scanf() functions, let’s explore the list of topics that we will be covering in this article: 

So, without further ado, let’s get started! 

You can also explore: C Programming Online Courses & Certifications 

C Output: printf() 

The printf() function in the C programming language is used to print text to the standard output. It is defined in the standard library stdio.h.  

You can also explore:Understanding Logical Operators in C

The syntax for printf() is as follows: 

 
printf("format string", list of arguments);
Copy code

The format string is a string that defines the format of the output. Placeholders (also known as conversion specifiers) in the format string represent the values of the arguments passed to the function. The most commonly used conversion specifiers are: 

  • %d for printing an integer 
  • %f for printing a floating-point number 
  • %s for printing a string 
  • %c for printing a character 

Let’s look at a few examples: 

Example 1: 

 
#include <stdio.h>
int main() {
int age = 30;
float height = 5.7;
char name[] = "John Doe";
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
return 0;
}
Copy code

Output 1: 

2023_02_image-24.jpg

Example 2: 

 
#include <stdio.h>
int main() {
int number = 12345;
double floating_point = 3.14159;
char character = 'A';
char string[] = "Hello World";
printf("The number is: %d\n", number);
printf("The floating point number is: %.4f\n", floating_point);
printf("The character is: %c\n", character);
printf("The string is: %s\n", string);
return 0;
}
Copy code

Output 2: 

2023_02_image-24.jpg

Example 3: 

 
#include <stdio.h>
int main() {
int year = 2022;
char name[] = "Sam";
float salary = 50000.00;
printf("%s's salary in the year %d is $%.2f\n", name, year, salary);
printf("%s's salary in the year M is $%8.2f\n", name, year, salary);
return 0;
}
Copy code

Output 3: 

2023_02_image-25.jpg

In this example, printf() is used to print a formatted string to the standard output, including a person’s name, year, and salary. The %s placeholder is used to insert the string value of name, the %d placeholder is used to insert the integer value of year, and the %f placeholder is used to insert the floating-point value of salary. The .2 and 8 within the format specifier specify the number of digits after the decimal point and the minimum width of the output field, respectively. 

C Input: scanf() 

The scanf() function in the C programming language is used to read data from the standard input (usually the keyboard). It is also defined in the standard library stdio.h.  

You can also explore: Tokens in C Programming

The syntax for scanf() is as follows: 

 
scanf("format string", list of pointers to arguments);
Copy code

The format string is a string that defines the format of the input. Placeholders (also known as conversion specifiers) in the format string represent the type of data to be read. The most commonly used conversion specifiers are: 

  • %d for reading an integer 
  • %f for reading a floating-point number 
  • %s for reading a string 
  • %c for reading a character 

scanf() returns the number of items successfully read, or EOF if an error occurs. 

Let’s look at a few examples: 

Example 1: 

 
#include <stdio.h>
int main() {
int age;
float height;
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your height: ");
scanf("%f", &height);
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
return 0;
}
Copy code

Output 1: 

2023_02_image-29.jpg

Example 2: 

 
#include <stdio.h>
int main() {
int day, month, year;
printf("Enter the date (dd mm yyyy): ");
scanf("%d %d %d", &day, &month, &year);
printf("The date is: d/d/d\n", day, month, year);
return 0;
}
Copy code

Output 2: 

2023_02_image-27.jpg

In this code, scanf() is used to read three integers (day, month, and year) from the user and store them in the variables day, month, and year. The d format specifier is used to ensure that the day and month are always displayed with two digits, with leading zeros if necessary. The d format specifier is used to ensure that the year is always displayed with four digits. 

Example 3: 

 
#include <stdio.h>
int main() {
int x, y;
printf("Enter two integers: ");
scanf("%d %d", &x, &y);
printf("The sum of %d and %d is %d\n", x, y, x + y);
printf("The difference of %d and %d is %d\n", x, y, x - y);
printf("The product of %d and %d is %d\n", x, y, x * y);
printf("The quotient of %d and %d is %d\n", x, y, x / y);
printf("The remainder of %d and %d is %d\n", x, y, x % y);
return 0;
}
Copy code

Output 3: 

2023_02_image-30.jpg

In this code, scanf() is used to read two integers from the user and store them in the variables x and y. The code then uses printf() to perform various arithmetic operations on x and y and print the results to the standard output. 

You must explore: Understanding the Difference Between Structure and Union in C

Format Specifiers for I/O in C 

The following table demonstrates commonly used C data types and their format specifiers: 

Data Type  Format Specifier 
int  %d or %i 
char  %c 
float  %f 
double  %lf 
short  %hd 
long  %ld 
long long  %lld 
unsigned int  %u 
unsigned short  %hu 
unsigned long  %lu 
unsigned long long  %llu 
string  %s 

Endnotes 

I hope that this article was useful in helping you learn how to use printf() and scanf() functions in C programming. Explore our C articles to find out more about the language and consolidate your knowledge of the fundamentals. 

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio