ADVERTISEMENT
ADVERTISEMENT

Input and Output Statements in C

In C programming, input and output (I/O) statements are used to read data from the input stream and write data to the output stream. These statements allow a program to interact with the user, receive data from external sources, and send data to external destinations.

In C programming, formatted and unformatted statements refer to the way data is input and output.

Formatted Statements

Formatted Statements use placeholders, called format specifiers, to specify the type and layout of input and output. The most common formatted statements in C are printf() and scanf(). Here is an example of a formatted input statement using scanf():

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

In this example, %d is a format specifier that tells scanf() to expect an integer as input. The & operator is used to pass the address of the age variable to scanf(), so that the input can be stored in the age variable.

Unformatted Statements

Unformatted statements do not use format specifiers and do not perform any formatting on the input or output. The most common unformatted statement in C is getchar(), which reads a single character from the input stream. Here is an example of an unformatted input statement using getchar():

char c;
printf("Enter a character: ");
c = getchar();

In this example, getchar() reads a single character from the input stream and stores it in the c variable. There is no formatting of the input, and no format specifiers are used.

putchar() function is used to output a single character to the standard output stream. It takes a single character as an argument. For Example

#include <stdio.h>

int main()

{

char c = 'A';

putchar(c);      // Outputs the character 'A'

return 0;

}

There are several different I/O statements available in C, including both formatted and unformatted statements. Some of the most commonly used I/O statements are:

  • printf(): This is a formatted output statement that is used to display data to the standard output stream (usually the terminal or console window). It takes a format string and a list of values as arguments, and uses format specifiers in the format string to control the layout and type of the output.

  • scanf(): This is a formatted input statement that is used to read data from the standard input stream (usually the keyboard). It takes a format string and a list of variables as arguments, and uses format specifiers in the format string to control the type and layout of the input.

  • putchar(): This is an unformatted output statement that is used to write a single character to the standard output stream. It takes a single character as an argument.

  • getchar(): This is an unformatted input statement that is used to read a single character from the standard input stream.

Here is an example of using printf() and scanf() to perform formatted input and output:

#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.", name, age);

  return 0;
}

 This program prompts the user to enter their name and age, reads the input using scanf(), and then outputs a greeting using printf(). The format specifiers %s and %d in the format strings control the layout and type of the input and output.


ADVERTISEMENT

ADVERTISEMENT