C Input and Output - scanf, printf, gets, puts, getchar, putchar

<<Previous

Next >>



Input and Output in C

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


Standard Files in C

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

Standard FilesFile PointerMeaning
Standard input stdin used to take the input from the device such as Keyboard
Standard outputstdoutused to send 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 the stdio.h header file into the program.


Types of Input Output

There are 2 kinds of I/O functions as given below.




Formatted Input Output Functions

scanf() in C

  • scanf is the input function that gets the formatted input from the file stdin that is the keyboard. As mentioned earlier, C treats the keyboard input as a file. scanf 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 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

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

  • 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

    
    int a;
    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.

printf() in C programming

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

    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

    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() in C Programming

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.

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

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

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

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.

gets - Syntax

	   char *gets(char *str)

puts() in C Programming

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.

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);

}

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

Output

Enter the text: welcome
welcome

<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














C Input and Output - scanf, printf, gets, puts, getchar, putchar

<<Previous

Next >>



Input and Output in C

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


Standard Files in C

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

Standard FilesFile PointerMeaning
Standard input stdin used to take the input from the device such as Keyboard
Standard outputstdoutused to send 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 the stdio.h header file into the program.


Types of Input Output

There are 2 kinds of I/O functions as given below.




Formatted Input Output Functions

scanf() in C

  • scanf is the input function that gets the formatted input from the file stdin that is the keyboard. As mentioned earlier, C treats the keyboard input as a file. scanf 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 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

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

  • 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

    
    int a;
    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.

printf() in C programming

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

    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

    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() in C Programming

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.

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

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

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

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.

gets - Syntax

	   char *gets(char *str)

puts() in C Programming

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.

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);

}

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

Output

Enter the text: welcome
welcome

<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy