Skip to content

Input Output

In C, input/output (I/O) refers to the process of reading data from an input source (such as a keyboard or file) and writing data to an output destination (such as a screen or file). C provides a set of functions for performing input and output operations, known as the standard I/O library.

Here are some of the most commonly used I/O functions in C:

  • printf: used to print a formatted string of characters to the screen
  • scanf: used to read a formatted string of characters from the keyboard
  • getchar: used to read a single character from the keyboard
  • putchar: used to write a single character to the screen
  • fprintf: used to write a formatted string of characters to a file
  • fscanf: used to read a formatted string of characters from a file Here is an example of how to use the printf and scanf functions to perform basic input and output in C:
#include <stdio.h>

int main(void) {
  int num;
  printf("Enter a number: ");
  scanf("%d", &num);
  printf("You entered the number %d\n", num);

1. printf in C

In C, printf is a function used to print a formatted string of characters to the screen (i.e., the standard output device). It is part of the standard I/O library and is commonly used for producing output in C programs.

The printf function takes a format string and a set of variables as arguments and returns the number of characters printed. The format string specifies how the variables should be formatted and includes placeholders for the variables as well as special characters for controlling the output.

Here is an example of how to use the printf function in C:

#include <stdio.h>

int main(void) {
  int num = 5;
  float pi = 3.14;
  printf("The value of num is %d and the value of pi is %.2f\n", num, pi);
  return 0;
}

In this example, the printf function is used to print a string of characters that includes two placeholders for the variables num and pi. The %d placeholder is used for the int variable num, and the %.2f placeholder is used for the float variable pi. The .2 specifies that the value of pi should be printed with two decimal places of precision.

The printf function can be used to print a wide range of data types, including integers, floating-point numbers, characters, and strings, using a variety of formatting options.

Here is the general syntax for using the printf function in C:

printf(format_string, arg1, arg2, ...);
The format_string argument is a string of characters that specifies how the remaining arguments should be formatted. It can include placeholders for the arguments as well as special characters for controlling the output.

The arg1, arg2, ... arguments are the values to be printed. They must be specified in the same order as the placeholders in the format_string.

Here is an example of how to use the printf function to print a variety of data types in C:

#include <stdio.h>

int main(void) {
  int num = 5;
  float pi = 3.14;
  char letter = 'A';
  char *word = "Hello";
  printf("The value of num is %d\n", num);
  printf("The value of pi is %.2f\n", pi);
  printf("The value of letter is %c\n", letter);
  printf("The value of word is %s\n", word);
  return 0;
}
In this example, the printf function is used to print four different variables: an int, a float, a char, and a string of characters. Each variable is printed using a different placeholder in the format_string argument. The %d placeholder is used for the int variable num, the %.2f placeholder is used for the float variable pi, the %c placeholder is used for the char variable letter, and the %s placeholder is used for the string of characters word.

2. scanf in C

In C, scanf is a function used to read a formatted string of characters from the keyboard (i.e., the standard input device). It is part of the standard I/O library and is commonly used for receiving input in C programs.

The scanf function takes a format string and a set of variables as arguments and returns the number of variables successfully read. The format string specifies how the characters should be interpreted and includes placeholders for the variables as well as special characters for controlling the input.

Here is an example of how to use the scanf function in C:

#include <stdio.h>

int main(void) {
  int num;
  float pi;
  char letter;
  printf("Enter an integer, a floating-point number, and a character: ");
  scanf("%d %f %c", &num, &pi, &letter);
  printf("You entered the integer %d, the floating-point number %.2f, and the character %c\n", num, pi, letter);
  return 0;
}

In this example, the scanf function is used to read three different variables from the keyboard: an int, a float, and a char. Each variable is read using a different placeholder in the format_string argument. The %d placeholder is used for the int variable num, the %f placeholder is used

Last modified on: 2023-01-10 00:45:31