Search Result
Details here...

Advanced Input/Output in C

Previous

Operaotrs Revisited | Precedence and Associativity

Next

Introduction to Array in C


In the Basic Input/Ouput chapter, we learned only the printf() and scanf() functions. In this chapter, we will learn the unformatted I/O function that are required for other advanced topic like, File I/O, Dealing with Structures, etc..

What are the other I/O functions?

  • getchar()
  • putchar()
  • getc()
  • putc()
  • gets()
  • puts()

getchar()

getchar() function can be used to read a single character. This function is alternate to scanf() function. The difference between getchar() and scanf() is that getchar() function does not require any format specifier. The syntax of getchar() function is as follows:
getchar () ;
We will look through the example of getchar() function along with the example of putchar() function shortly.

putchar()

putchar() function can be used to write a single character. This function is alternate to printf() function. The syntax of putchar() function is as follows:
putchar ( character ) ;

Let's have a look at an example

C

#include <stdio.h>
int main () {
char ch;
printf("Enter a character: ");
ch = getchar();
putchar(ch);
return 0;
}

Output

Enter a character: d
d

getc()

The getc() function gets the next character (an unsigned char) from the specified stream, either from the standard input stream or from a file. The syntax of getc() function is as follows:
getc ( FILE *stream ) ;
It can be used to read a single character from the Standard Input Stream(stdin) using stdin as the argument.

putc()

The putc() function writes the specified character (an unsigned char) to the specified stream, either to the standard output stream or to a file. The syntax of putc() function is as follows:
putc ( int ch, FILE *stream ) ;
It can be used to write a single character to the Standard Output Stream(stdout) using stdout as the argument.

Let's have a look at an example

C

#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
ch = getc(stdin);
putc((int) ch, stdout);
return 0;
}

Output

Enter a character: d
d

gets()

The gets() function reads a string from the standard input stream, and stores it in the char array pointed by an address. The syntax of gets() function is as follows:
gets ( char *str ) ;
It stops reading when a newline character is read or, when the user presses the Return key.

Yet to come!

We will learn about string and arrays in the next chapters.

puts()

The puts() function writes a string to the standard output stream. It doesn't include the null character but appends a new line character to the output. The syntax of puts() function is as follows:
puts ( char *str ) ;

Let's have a look at an example

C

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

Output

Enter a string: darpan kattel
darpan kattel

Yet to come!

We will learn about fgets() and fputs(), fgetchar() and fputchar(), fprintf() and fscanf(), etc. in File I/O chapter.

By darpan.kattel
Previous

Operaotrs Revisited | Precedence and Associativity

Next

Introduction to Array in C