printf() and scanf() functions for Input and Output in C

In this tutorial, we will see about the printf() and scanf() functions. These functions are available in stdio.h header file of C. These functions provide a convenient way to display data on the screen and accept user input, respectively. In this blog, we will explore the usage and features of printf() and scanf() functions in detail.

Introduction to print()

The printf() function is used to display output on the screen or console. You can format and print data in a specified manner as per your requirement.

Here’s a basic example:

#include <stdio.h>
int main() {
    printf("Hello, World!\n");
    return 0;
}

Explanation of above program

  • The printf() function accepts a string as its argument, enclosed in double quotation marks.
  • The ‘\n’ character is used to insert a new line after printing the text.

Formatting Specifiers of Printf()

  • Printf() provides formatting specifiers to display variables of different data types.
  • For example, %d is used for integers, %f for floats, %c for characters, and %s for strings.

Displaying Variables

  • Using printf() with formatting specifiers to display the values of variables.

Example:

int num = 10; 
printf("The value of num is: %d\n", num);

Formatting Width and Precision

  • Specifying the width and precision of displayed values using formatting specifiers.

Example:

float num = 3.14159; 
printf("Pi value with 2 decimal places: %.2f\n", num);

Escape Sequences

  • Using escape sequences to include special characters in printf() output.

Example:

printf("This is a tab \t and a backslash \\.\n");

Introduction to scanf()

The scanf() function is used for reading input from the user. It allows you to accept values and store them in variables for further processing.

Here’s a basic example:

#include <stdio.h>
int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Your age is: %d\n", age);
    return 0;
}

Accepting User Input:

  • The scanf() function takes a formatting specifier as its first argument to specify the data type of the input.
  • The & operator is used to pass the address of the variable where the input should be stored.

Working with Multiple Inputs:

  • Accepting multiple inputs using multiple scanf() statements.

Example:

scanf("%d %f", &age, &height);

Handling Strings:

  • Reading strings using the %s formatting specifier.

Example:

char name[20]; scanf("%s", name);

Handling Input Errors:

  • Validating user input and handling potential errors or unexpected input.
  • Example: Checking the return value of scanf() to ensure successful input.

Conclusion:

The printf() and scanf() functions are very important Components of C programming. This enables you to take inputs from the user and display output to the user. By understanding the formatting specifiers, escape sequences, and techniques for reading user input, you can create more interactive and user-friendly programs. Experiment with different examples, practice using various formatting options, and continue exploring the vast possibilities these functions offer. Happy coding!