How to write an echo program in C?

the output should be something like this:

Enter Character : a Echo : a 

I wrote

 int c; while (c != EOF) { printf("\n Enter input: "); c = getchar(); putchar(c); } 

But I get two input entries after the echo.

+4
source share
5 answers

Two characters are extracted during input. You need to throw away the carriage return.

 int c = 0; int cr; while (c != EOF) { printf("\n Enter input: "); c = getchar(); cr = getchar(); /* Read and discard the carriage return */ putchar(c); } 
+3
source

Homework?

If so, I will not give a complete answer / You probably have a buffered input - the user needs to enter a response before anything is returned to your program. You need to figure out how to disable this.

(it depends on the environment of your program - if you could give more detailed information about the platform and how you run the program, we could give better answers)

+2
source

take fgets, for example:

 char c[2]; if( fgets( c, 2, stdin ) ) putchar( *c ); else puts("EOF"); 

and you have no problem with getchar / scanf (% c) / '\ n' etc.

+1
source

Why don't you use scanf ?

Example:

 char str[50]; printf("\n Enter input: "); scanf("%[^\n]+", str); printf(" Echo : %s", str); return 0; 

Outputs

 Enter input: xx Echo : xx 

scanf link

0
source
 #include <stdio.h> #include <conio.h> main(){ int number; char delimiter; printf("enter a line of positive integers\n"); scanf("%d%c", &number, &delimiter); while(delimiter!='\n'){ printf("%d", number); scanf("%d%c", &number, &delimiter); } printf("\n"); getch(); } 
0
source

All Articles