Skip to main content

Input Output in C

Input Output

  • Input and output refer to the process of reading data from a source, say user input (shell) or file and displaying data to a destination, say computer console (shell) or file.

  • In C, input is usually provided by the user or by an external device, while output is typically displayed on the computer screen or written to a file. There are several functions in C that are commonly used for input and output.

  • Some of the most frequently used ones include:

printf()

This function is used to print data to the console or terminal window. It is used to display messages, numbers, and other values on the screen.

Syntax:

int scanf(const char *format, ...);
  • The first argument is a format string that specifies how to read the input, followed by pointers to variables where the input should be stored.
  • The function returns the number of input items successfully matched and assigned.

scanf()

This function is used to read input data from the user. It is used to capture keyboard input from the user.

Syntax:

int scanf(const char *format, ...);
  • The first argument is a format string that specifies how to read the input, followed by pointers to variables where the input should be stored.
  • The function returns the number of input items successfully matched and assigned.

getchar()

This function is used to read a single character from the user. It is used to capture single keystrokes from the keyboard.

Syntax:

int getchar(void);
  • It waits for the user to input a character and then returns that character as an integer.

putchar()

This function is used to display a single character on the screen. It is used to output single characters to the console.

Syntax:

int putchar(int c);
  • This function is used to write a single character to the console.
  • The character to be printed is specified as an integer.

fread()

This function is used to read data from a file. A file should be opened first before being able to read using fopen() and then close using fclose().

Syntax:

size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
  • The first argument is a pointer to a buffer where the data will be stored.
  • The second argument is the size of each element to be read, the third argument is the number of elements to read, and the fourth argument is a pointer to the file to read from.
  • The function returns the number of elements successfully read.

fwrite()

This function is used to write data to a file. A file should be opened first before being able to read using fopen() and then close using fclose().

Syntax:

size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
  • The first argument is a pointer to the data to be written
  • The second argument is the size of each element to be written
  • The third argument is the number of elements to write
  • The fourth argument is a pointer to the file to write to
  • The function returns the number of elements successfully written.