Skip to main content

Scanf and Printf in C

Scanf and Printf

  • scanf() and printf() are two C library functions that are used for input and output, respectively.

  • Both scanf() and printf() functions are part of the stdio.h header file, which is included at the beginning of the program.

Scandf()

  • The scanf() function is used to read input data from stdin (standard input) based on a specified format string, and store the input data in variables.

  • The scanf() function takes two arguments: a format string and a variable list of pointers to the variables in which the input data will be stored.

  • The format string specifies the format of the input data, and can include placeholders such as %d for integers, %f for floating-point numbers, %c for characters, %s for strings, and so on.

printf()

  • The printf() function is used to write formatted output to stdout (standard output).

  • The printf() function takes a format string as its first argument, followed by a variable list of arguments that will be inserted into the formatted output.

  • The format string specifies the format of the output, and can include placeholders such as %d for integers, %f for floating-point numbers, %c for characters, %s for strings, and so on.

Syntax for scanf() and printf() are as follows:

int scanf(const char *format, ...);
int printf(const char *format, ...);

Example

Here's an example C program that uses scanf() and printf() functions to read input from the user and display formatted output to the console:

#include <stdio.h>

int main() {
int age;
char name[20];

printf("Enter your name: ");
scanf("%s", name);

printf("Enter your age: ");
scanf("%d", &age);

printf("Hello, %s! You are %d years old.\n", name, age);

return 0;
}

Explanation:

  • We first declare two variables: age of type int and name of type char with a length of 20.
  • We then use the printf() function to print a message to the console asking the user to enter their name, and then use the scanf() function to read a string from stdin and store it in the name array.
  • We repeat the process to get the user's age, using %d as the format specifier and passing the address of the age variable as the second argument to scanf().
  • Finally, we use printf() to display a formatted message to the console, using the input values entered by the user. Note that we use %s as the format specifier for the name variable, and %d for the age variable.

Output

When this program is run, the user is prompted to enter their name and age. Once they enter the values and press Enter, the program reads the input values and displays a formatted message back to the console.

Here's the output of the program:

Enter your name: John
Enter your age: 25
Hello, John! You are 25 years old.
Error handling in scanf() and printf()
  • While scanf() and printf() are powerful input and output functions in C, they can also introduce a number of problems if used incorrectly.

  • One of the most common issues is the potential for buffer overflow, where input data overflows the allocated space for a variable, causing undefined behavior.

  • To mitigate this risk, it's important to always use field width specifiers and avoid using %s to read input data into a fixed-size array without a length modifier.

  • Additionally, it's important to check the return value of these functions to ensure that they successfully read or wrote the expected amount of data.

  • With careful usage and error checking, scanf() and printf() can be very useful tools for input and output operations in C.