INPUT AND OUTPUT FUNCTION

There are a number of I/O standard functions in c, based on the data types. The I/O function are classified into two types (a). formatted functions (b).unformatted functions. various functions of these categories are listed

Types of Input and Output Functions in C

Formatted

Unformatted

The unformatted input-output functions further have two categories:

  • The character functions
  • The string functions

Formatted Functions:

INPUT FUNCTION :

scanf() in C

  • The scanf() reads all types of data values given by the user and these values are assigned to the variables. It requires the conversion specification (such as %s and %d ) to identify the type of data to be read during the program execution.
  • You need to pass the address of the variable to the scanf function along with other arguments so that the read values can be assigned to the correct destination.

scanf() Syntax

  • The first argument is the format string (conversion specification) and followed by a list of variable arguments of pointer types ie., the address of variables that need to be passed.

scanf() Example

double b;

scanf(“%d\n”, &a);

scanf(“%lf\n”, &b);

  • scanf() reads the characters from standard input and interprets them according to the format string specification. It stops reading when it encounters a space or when some input fails to match with the conversion specification.

OUTPUT FUNCTION:

printf() in C programming

printf() Syntax:

  • int printf( const char *format, arg1, agr2,….);
  • The first argument is the format string which is composed of ordinary characters and conversion specification. This is followed by zero or more optional arguments.

printf() Example:

printf(“welcome”);

printf(“The result is %d “,a);

  • Usually the format string is enclosed with the double quotation marks in both scanf() and printf().
  • scanf printf Example
  • Let see the example program using scanf() and printf() functions.

#include<stdio.h>

int main()

{

int a;

printf(“Enter the value of a = “);

scanf(“%d”,&a);

printf(“\nThe value of a = %d”,a);

return(0);

}

Output:

Enter the value of a = 10 The value of a = 10

Unformatted I/O Functions

getchar() in C Programming

getchar() — Syntax

variable-name = getchar();

getchar() — Example

char ch;
ch = getchar();

This places the next character into the variable ch. It is a low level function.

putchar() in C Programming

putchar() — Syntax

putchar( variable-name);

putchar() — Example

char ch = 'C';
putgachar(ch);

getchar putchar example

#include<stdio.h>
int main()
{
char a;
a = getchar();
putchar(a);
return(0);
}

The most important point is that the both getchar() and putchar() could be implemented as macros, rather than function. This means that it might not be possible to use functions as parameters inside them:

putchar( function() );

gets() in C Programming

gets — Syntax

char *gets(char *str)

puts() in C Programming

puts — Syntax

int puts(const char *str)

puts() returns an integer value, whose value is only guaranteed if there is an error.

gets puts Example C Program

#include<stdio.h>
int main()
{
char ch[20];
printf("Enter the text: ");
gets(ch);
puts(ch);
return(0);
}

Output

Enter the text: welcome
welcome

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store