Detailed Explanation of printf() and scanf() Functions in C Language

printf() and scanf() functions are used for input and output operations in C language. These two functions are built-in library functions defined in stdio.h (header file).

printf() Function

printf() function is used for output operations. It prints the given statement to the console.

The syntax of the printf() function is as follows:

printf("format string", argument_list);  
The format string can be %d (integer), %c (character), %s (string), %f (float), etc.

scanf() Function

scanf() function is used for input operations. It reads input data from the console.

scanf("format string", argument_list);  

Program to Print the Cube of a Given Number

Below is a simple C language example that takes input from the user and prints the cube of the given number.

#include<stdio.h>    int main(){        int number;        printf("Please enter a number:");        scanf("%d",&number);        printf("The cube of the number is: %d ", number*number*number);        return 0;  } 

Output:

Please enter a number: 5The cube of the number is: 125

👇 Click to Claim👇

👉 C Language Knowledge Resource Collection
  • The statement scanf(“%d”,&number) reads an integer from the console and stores the given value in the number variable.

  • The statement printf(“The cube of the number is: %d “, number) prints the cube of the number to the console.

Program to Print the Sum of Two Numbers

Below is a simple C language example for input and output operations, printing the sum of two numbers.

#include<stdio.h>    int main(){        int x=0, y=0, result=0;        printf("Please enter the first number:");      scanf("%d", &x);      printf("Please enter the second number:");      scanf("%d", &y);        result = x + y;      printf("The sum of the two numbers is: %d ", result);        return 0;  } 

Output:

Please enter the first number: 9Please enter the second number: 9The sum of the two numbers is: 18
Programmer Technical Exchange Group

Scan the code to join the group and remember to note: city, nickname, and technical direction.



Leave a Comment