C Printf() And Scanf() Functions

In C programming, the printf() and scanf() functions are the most commonly used. These functions are found in the vast majority of C programs. This tutorial will teach you what these functions are and how to use them in C programs.
In c programs, the scanf() and printf() functions are used for input and output, respectively. Because these functions are defined in the stdio.h header file, you must include it in your program if you use any of them.

Function printf():

The prinf() function displays (or prints) output on the console screen.
Printf() function syntax:

printf("format string",arguments);

A format string (or format specifier) is a string used to format input and output. The format string always begins with the character ‘%’. For instance, the format string for integer is%d, string%s, float%f, and so on.

Function scanf():

The scanf() function is used to read the user’s input.
Scanf() function syntax:

scanf("format string",arguments);

Example 1: To add two input numbers, use the printf() and scanf() functions.

In the following example, we calculate the sum of two numbers entered by the user. Using printf, the program prints a message on the console instructing the user to enter the numbers (). The scanf() function reads the input numbers and stores them in the variables num1 and num2. The result of the sum is displayed on the console using the printf() function after it has been calculated.

int main(){

  int num1,num2,sum;

  printf("Enter first number: ");
  //Reading the first input number
  printf("Enter second number: ");
  //Reading the second input number

  //calculating sum of two entered numbers

  //Printing the sum of input numbers using printf()
  printf("Sum of %d and %d is: %d ",num1, num2, sum);

  return 0;

Example 2: Display the square of the input number.

In this printf() and scanf() function example, we display the square of an input number. Using scanf(), we read a number entered by the user and print the square of the input number using printf ().

  int main(){
  int num;
  printf("Enter a number: ");
  printf("Square of entered number is: %d ",num*num);
  return 0;

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button