scanf() function
The scanf() function is used to read formatted input from the user via the console. It allows the program to pause & wait for the user to enter data. The scanf() function reads the input according to the format specifiers provided & stores the input in the corresponding variables.
Look at the general syntax of the scanf() function:
scanf("format string", &variable1, &variable2, ...);
The format string in scanf() is similar to the one used in printf(). It specifies the type of data to be read. The variables that will store the input values are passed as arguments, each preceded by an ampersand (&) symbol, which denotes the address of the variable.
Example of scanf() function
C
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Enter your age: 25
You are 25 years old.
In this example, the program prompts the user to enter their age using printf(). Then, it uses scanf() with the %d format specifier to read an integer value entered by the user. The input value is stored in the age variable. Finally, the program prints a message displaying the age entered by the user.
It's important to note that when using scanf(), you must provide the memory address of the variable where the input will be stored, hence the use of the & symbol before the variable name.
Program to print cube of given number:
Now let's combine the usage of printf() & scanf() to create a program that calculates & prints the cube of a number entered by the user.
C
#include <stdio.h>
int main() {
int number, cube;
printf("Enter a number: ");
scanf("%d", &number);
cube = number * number * number;
printf("The cube of %d is %d.\n", number, cube);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Enter a number: 5
The cube of 5 is 125.
In this code :
-
We include the stdio.h header file to use printf() & scanf() functions.
-
We declare two integer variables: number to store the user's input & cube to store the calculated cube.
-
We use printf() to prompt the user to enter a number.
-
We use scanf() to read the integer input from the user & store it in the number variable.
-
We calculate the cube of the number by multiplying it by itself three times & store the result in the cube variable.
- Finally, we use printf() to display the original number & its cube.
Program to print sum of 2 numbers:
Let's create another program that takes two numbers as input from the user, calculates their sum, & prints the result.
C
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("The sum of %d and %d is %d.\n", num1, num2, sum);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
Enter two numbers: 10 20
The sum of 10 and 20 is 30.
In this code :
-
We include the stdio.h header file to use printf() & scanf() functions.
-
We declare three integer variables: num1 & num2 to store the user's input, & sum to store the calculated sum.
-
We use printf() to prompt the user to enter two numbers.
-
We use scanf() to read two integer inputs from the user, separated by a space, & store them in the num1 & num2 variables respectively.
-
We calculate the sum of the two numbers by adding num1 & num2 & store the result in the sum variable.
- Finally, we use printf() to display the original numbers & their sum.
Frequently Asked Questions
What is printf & scanf in C?
printf displays output on the screen, while scanf reads input from the user. Both are standard input/output functions in C.
What is the printf function in C?
printf is a standard C function used to format and print output to the console, allowing for various data types to be displayed.
What is called scanf in C?
The scanf is a standard C function used to read formatted input from the user, allowing data to be stored in variables.
Conclusion
In this article, we talked about the printf() & scanf() functions in C programming. We discussed how printf() is used to display formatted output & how scanf() is used to read formatted input from the user. With the help of examples of these functions we learned how programs take input from the user & display output based on that respective input.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.