The printf and scanf functions in C are part of the C Standard Library and are used for output and input, respectively. They are essential for reading data from the user and displaying information on the screen.

printf Function

The printf function is used to print formatted output to the standard output (usually the console).

Syntax

int printf(const char *format, ...);
  • format: A string that specifies how to format the output. It can include format specifiers (like %d, %f, etc.) that dictate how subsequent arguments should be formatted.
  • ...: A variable number of arguments corresponding to the format specifiers.

Common Format Specifiers

  • %d: Print an integer in decimal format.
  • %f: Print a floating-point number.
  • %c: Print a single character.
  • %s: Print a string.
  • %u: Print an unsigned integer.
  • %x: Print an integer in hexadecimal format.
  • %p: Print a pointer address.

Example

Here’s an example of how to use printf:

#include <stdio.h> int main() { int age = 25; float height = 5.9; char initial = 'A'; char name[] = "Alice"; printf("Name: %s\n", name); printf("Initial: %c\n", initial); printf("Age: %d\n", age); printf("Height: %.1f feet\n", height); return 0; }

scanf Function

The scanf function is used to read formatted input from the standard input (usually the keyboard).

Syntax

int scanf(const char *format, ...);
  • format: A string that specifies the expected format of the input. It can include format specifiers that dictate how to read subsequent arguments.
  • ...: A variable number of pointers to variables where the read values will be stored.

Common Format Specifiers

  • %d: Read an integer in decimal format.
  • %f: Read a floating-point number.
  • %c: Read a single character.
  • %s: Read a string (until whitespace).
  • %u: Read an unsigned integer.
  • %x: Read an integer in hexadecimal format.

Example

Here’s an example of how to use scanf:

#include <stdio.h> int main() { int age; float height; char name[50]; printf("Enter your name: "); scanf("%s", name); // Read a string input printf("Enter your age: "); scanf("%d", &age); // Read an integer input printf("Enter your height (in feet): "); scanf("%f", &height); // Read a floating-point input printf("Name: %s\n", name); printf("Age: %d\n", age); printf("Height: %.1f feet\n", height); return 0; }

Important Notes

  1. Address-of Operator: When using scanf, you need to use the address-of operator (&) before the variable name for non-string types (like int, float, etc.) to provide the memory address where the input will be stored. For strings, you can directly use the variable name as it already represents the address of the first element.

  2. Input Validation: It’s a good practice to validate the input after using scanf. This can prevent incorrect data from being processed.

  3. Buffer Overflow: When using %s with scanf, it’s crucial to specify a width limit to avoid buffer overflow. You can do this by using a format specifier like %49s if you have an array of size 50.

  4. Return Value: Both printf and scanf return a value. printf returns the total number of characters printed, and scanf returns the number of input items successfully matched and assigned.

Summary

  • printf is used for formatted output, while scanf is used for formatted input.
  • Both functions support various format specifiers to handle different data types.
  • Care should be taken with input validation and memory management when using these functions to ensure robustness and security in C programs.