Follow

Console Input/Output || chapter 11

Console Input/Output (I/O) in C is used for communication between a program and the user through the console, which is the text-based interface used by command-line programs.

Types of I/O:

There are two types of console I/O in C: formatted and unformatted.

Formatted Console I/O Functions:

Formatted console I/O functions are used to read or write data in a specific format. The most commonly used formatted console I/O functions are printf() and scanf().

c
printf("The value of x is %d\n", x);
 scanf("%d", &x);

sprintf( ) and sscanf( ) Functions:

The sprintf() function is used to format a string and store it in a buffer, while the sscanf() function is used to read formatted input from a string and store it in variables.

c
char str[50]; 
int x = 10, y = 20
sprintf(str, "The value of x is %d and y is %d", x, y); 
sscanf(str, "The value of x is %d and y is %d", &x, &y);

Unformatted Console I/O Functions:

Unformatted console I/O functions are used to read or write data as raw bytes. The most commonly used unformatted console I/O functions are getchar() and putchar().

c
int ch; 
ch = getchar(); 
putchar(ch);

Summary:

Console Input/Output in C is used for communication between a program and the user through the console. There are two types of console I/O in C: formatted and unformatted. Formatted console I/O functions are used to read or write data in a specific format, and the most commonly used formatted console I/O functions are printf() and scanf(). The sprintf() and sscanf() functions are used to format strings and read formatted input from strings, respectively. Unformatted console I/O functions are used to read or write data as raw bytes, and the most commonly used unformatted console I/O functions are getchar() and putchar().

No comments:

Post a Comment

Tell us how you like it.