printf() and scanf() in C

printf() and scanf() are two commonly used functions in the C programming language.

printf() is used to output formatted data to the standard output stream, which is typically the console. It allows the programmer to specify the data to be output and the formatting of that data. Here’s a simple example:

int num = 42;
printf("The answer is %d\n", num);

This will output the following to the console:

The answer is 42

The %d in the format string tells printf() to expect an integer argument, which is provided by the variable num.

scanf() is used to read formatted data from the standard input stream, which is typically the keyboard. It allows the programmer to specify the format of the input data and the variables that will store that data. Here’s a simple example:

int num;
scanf("%d", &num);

This will wait for the user to input an integer value and store it in the variable num. The %d in the format string tells scanf() to expect an integer input.

It’s important to note that scanf() reads data from the input stream in a blocking fashion, meaning that the program will wait for the user to input data before continuing. This can lead to issues if the user inputs unexpected data or the input stream becomes corrupted. To handle these issues, it’s important to check the return value of scanf() to ensure that the input was read correctly.

Program to print cube of given number:

Here’s a simple C program that reads a number from the user and prints its cube:

#include <stdio.h>

int main() {
   int num;
   printf("Enter a number: ");
   scanf("%d", &num);

   int cube = num * num * num;

   printf("The cube of %d is %d", num, cube);
   return 0;
}

Explanation:

  1. We include the standard input-output header file stdio.h.
  2. In the main() function, we declare an integer variable num.
  3. We prompt the user to enter a number using printf() and read the input using scanf().
  4. We calculate the cube of the number and store it in an integer variable cube.
  5. Finally, we print the cube using printf() with the format specifier %d.

Sample Output:

Enter a number: 5
The cube of 5 is 125

Note: In this program, we assume that the user enters a valid integer input. If the user enters invalid input, it may lead to undefined behavior. It’s always a good practice to add input validation to handle unexpected user input.

Program to print sum of 2 numbers:

Here’s a simple C program that reads two numbers from the user and prints their sum:

#include <stdio.h>

int main() {
   int num1, num2;
   printf("Enter two numbers: ");
   scanf("%d %d", &num1, &num2);

   int sum = num1 + num2;

   printf("The sum of %d and %d is %d", num1, num2, sum);
   return 0;
}

Explanation:

  1. We include the standard input-output header file stdio.h.
  2. In the main() function, we declare two integer variables num1 and num2.
  3. We prompt the user to enter two numbers using printf() and read the inputs using scanf().
  4. We calculate the sum of the two numbers and store it in an integer variable sum.
  5. Finally, we print the sum using printf() with the format specifier %d.

Sample Output:

Enter two numbers: 10 20
The sum of 10 and 20 is 30

Note: In this program, we assume that the user enters valid integer inputs. If the user enters invalid input, it may lead to undefined behavior. It’s always a good practice to add input validation to handle unexpected user input.