Input and Output in C

<<Previous

Next >>



Input and Output in C

In C, for input and output, these operations are supplied as functions in standard library. So, we can take the data through input functions and sends results through output functions.


Standard Files

C language treats all its inputs and outputs as files. A file is a place where information come from or can be sent to. These files can either be read or written only. These files are called I/O streams. C language has three I/O files (also called streams). Streams are always open and ready to use. Streams in C are as shown in the below table.

Standard FilesFile PointerMeaning
Standard input stdin used to taking the input from the device such as Keyboard
Standard outputstdoutused to giving output to a device such as monitor or screen
Standard errorstderrused to route the error message to the screen

Therefore, the keyboard and screen are referred to as the standard input and output files. In C, all library functions are covered by the stdio.h header file. For using I/O functionality, the programmer must include those stdio.h header file into their program.


Types of I/O

There are two kinds of I/O functions that are given below.




Formatted I/O Functions

scanf()

  • scanf is the input function which get the formatted input from the file stdin that is keyboard. As mentioned earlier, C treats the keyboard input as file. This is a built-in function that comes with all C compilers. Its header file is stdio.h.
  • 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 needs to pass the address of the variable to scanf function along with other arguments, so that the read values can be assigned to the correct destination.

    Syntax

    
    int scanf(const char *format, arg1, arg2,....);
    
    

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

    Example

    
    int a;
    double b;
    scanf("%d\n", &a);
    scanf("%lf\n", &b);
    
    
  • scanf() reads the characters from standard input and interpret them according to the format string specification. It stops reading when it encounters a space or when some input fails to match with a conversion specification.

printf()

  • The printf() prints all type of data value to the standard output(typically the screen). It requires conversion symbol and variable name to print the data. The printf() is the part of every C compiler. Its header file is stdio.h.

    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.

    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().

    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

    when you compile and execute the above program, you will get the following output.

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

Unformatted I/O Functions

getchar()

The getchar() reads character type data from input file stdin. It reads one character at a time till the user pressses the enter key. The getchar() returns a character type. It reads next character in the input file.

Syntax

      variable-name = getchar();

Example

         char ch;
         ch = getchar();

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

putchar()

The putchar() writes the character type data to the output file stdout(screen). It is used to print one character at a time.

Syntax

      putchar( variable-name);

Example

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

#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()

The gets() is used to read string from standard input file stdin and places it into some buffer which the programmer must provide. The gets() stops reading when an enter key is pressed or EOF is reached.

Syntax


	   char *gets(char *str)

puts()

The puts() is used to send the string to the output file stdout, until it finds a NULL end of string marker. The NULL is not written, instead the newline character is written to the output stdout.

Syntax

	int puts(const char *str)

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

#include<stdio.h>


int main()
{
  char ch[20];
  printf("Enter the text: ");
  gets(ch);
  puts(ch);
  return(0);

}

when you compile and execute the above program, you will get the following output.

Output

Enter the text: welcome
welcome

<< Previous

Next >>







Input and Output in C

<<Previous

Next >>



Input and Output in C

In C, for input and output, these operations are supplied as functions in standard library. So, we can take the data through input functions and sends results through output functions.


Standard Files

C language treats all its inputs and outputs as files. A file is a place where information come from or can be sent to. These files can either be read or written only. These files are called I/O streams. C language has three I/O files (also called streams). Streams are always open and ready to use. Streams in C are as shown in the below table.

Standard FilesFile PointerMeaning
Standard input stdin used to taking the input from the device such as Keyboard
Standard outputstdoutused to giving output to a device such as monitor or screen
Standard errorstderrused to route the error message to the screen

Therefore, the keyboard and screen are referred to as the standard input and output files. In C, all library functions are covered by the stdio.h header file. For using I/O functionality, the programmer must include those stdio.h header file into their program.


Types of I/O

There are two kinds of I/O functions that are given below.




Formatted I/O Functions

scanf()

  • scanf is the input function which get the formatted input from the file stdin that is keyboard. As mentioned earlier, C treats the keyboard input as file. This is a built-in function that comes with all C compilers. Its header file is stdio.h.
  • 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 needs to pass the address of the variable to scanf function along with other arguments, so that the read values can be assigned to the correct destination.

    Syntax

    
    int scanf(const char *format, arg1, arg2,....);
    
    

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

    Example

    
    int a;
    double b;
    scanf("%d\n", &a);
    scanf("%lf\n", &b);
    
    
  • scanf() reads the characters from standard input and interpret them according to the format string specification. It stops reading when it encounters a space or when some input fails to match with a conversion specification.

printf()

  • The printf() prints all type of data value to the standard output(typically the screen). It requires conversion symbol and variable name to print the data. The printf() is the part of every C compiler. Its header file is stdio.h.

    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.

    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().

    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

    when you compile and execute the above program, you will get the following output.

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

Unformatted I/O Functions

getchar()

The getchar() reads character type data from input file stdin. It reads one character at a time till the user pressses the enter key. The getchar() returns a character type. It reads next character in the input file.

Syntax

      variable-name = getchar();

Example

         char ch;
         ch = getchar();

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

putchar()

The putchar() writes the character type data to the output file stdout(screen). It is used to print one character at a time.

Syntax

      putchar( variable-name);

Example

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

#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()

The gets() is used to read string from standard input file stdin and places it into some buffer which the programmer must provide. The gets() stops reading when an enter key is pressed or EOF is reached.

Syntax


	   char *gets(char *str)

puts()

The puts() is used to send the string to the output file stdout, until it finds a NULL end of string marker. The NULL is not written, instead the newline character is written to the output stdout.

Syntax

	int puts(const char *str)

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

#include<stdio.h>


int main()
{
  char ch[20];
  printf("Enter the text: ");
  gets(ch);
  puts(ch);
  return(0);

}

when you compile and execute the above program, you will get the following output.

Output

Enter the text: welcome
welcome

<< Previous

Next >>







Learn about Hashing and Hash table

Learn Expression Tree

OS Kernel Architecture

OS Arcitecture of Windows, Linux and MacOS

Learn OS scheduling Algorithms