C programming - Input/output (I/O)

Input/output (I/O) operations in C programming are used to read data from the user (input) and to display data on the screen or write it to a file (output). The standard input/output functions in C are defined in the stdio.h header file.

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

  • scanf: reads input from the user, typically from the keyboard. It reads data in the format specified by a format string and stores it in variables. For example:
int x;
scanf("%d", &x);
S‮ww:ecruo‬w.theitroad.com
  • printf: displays output on the screen or writes it to a file. It takes a format string and any number of arguments, and produces output based on the format string. For example:
printf("The value of x is %d", x);
  • getchar: reads a single character from the keyboard. It returns an integer representing the character read, or EOF (end-of-file) if the end of input is reached. For example:
char ch;
ch = getchar();
  • putchar: displays a single character on the screen or writes it to a file. It takes an integer representing the character to be displayed. For example:
putchar('A');
  • gets: reads a string of characters from the keyboard. It reads characters until a newline character or end-of-file is encountered, and stores them in a character array. Note that gets has been deprecated in newer versions of C, and is not recommended for use due to potential buffer overflow issues. For example:
char str[100];
gets(str);
  • puts: displays a string of characters on the screen or writes it to a file. It takes a string as an argument, and adds a newline character after it. For example:
char str[] = "Hello, world!";
puts(str);

These are just a few examples of the I/O functions available in C programming. There are many others, including functions for working with files and for formatted input/output.