Reading from the C standard input using getchar() and write to the standard output using putchar() C functions

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

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: none

To do: Reading from the C standard input using getchar() and write to the standard output using putchar() C functions

To show: How to read from standard input using getchar() and write to standard output using putchar() C functions

 

 

// Program read from standard input, keyboard to standard output, console

// using predefined functions getchar() and putchar() which are defined in stdio.h header file

#include <stdio.h>

 

int main(void)

{

int count;

 

// gives some prompt...

printf("Enter a line of text:\n");

printf("Press \'S\' to stop.\n");

 

// get character from standard input store in variable count

count = getchar();

 

// while the S is not encountered...

while(count != 'S')

{

// put character on the standard output

putchar(count);

// carry on getting character from the standard input

count = getchar();

}

printf("\n");

 

return 0;

}

 

Output example:

 

Enter a line of text:

Press 'S' to stop.

This is a sample line of text

This is a sample line of text

Another line of text...

Another line of text...

Get from standard input and then put it

Get from standard input and then put it

On standard output...

On standard output...

S

Press any key to continue . . .

 

 

 

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