Using getchar() function to read a character from standard input and puts() function to write a string to stdout output


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Reading data from the standard input using getchar() and write it to the standard output using puts() C function

To show: How to use the C functions getchar() and puts() to read from standard input and write to standard output respectively




// Using getchar()/getwchar() and puts()/_putws()

#include <stdio.h>


int main(void)


char c, sentence[80];

int i = 0;


printf("Using getchar() and puts()\n");


puts("Enter a line of text: ");


// while iteration/loop

while (( c = getchar()) != '\n')

sentence[i++] = c;


// insert NULL at the end of string

sentence[i] = '\0';

puts("\nThe line of text entered was: ");


return 0;



Output example:


Using getchar() and puts()


Enter a line of text:

This is a line of text

The line of text entered was:

This is a line of text

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index