Questions tagged [getchar]

Anything related to C or C++ standard library functions `getchar` (C) or `std::getchar` (C++). These functions are used to read a single character from the standard input stream `stdin`.

Anything related to C or C++ standard library functions getchar (defined in <stdio.h> C standard header) or std::getchar (defined in <cstdio> C++ standard header). These functions are used to read a single character from the standard input stream stdin.

See CPPreference.com:

926 questions
94
votes
14 answers

How to avoid pressing Enter with getchar() for reading a single character only?

In the next code: #include int main(void) { int c; while ((c=getchar())!= EOF) putchar(c); return 0; } I have to press Enter to print all the letters I entered with getchar, but I don't want to do this, what I want…
javier
  • 1,705
  • 2
  • 18
  • 24
85
votes
6 answers

What is the equivalent to getch() & getche() in Linux?

I am not able to find the equivalent header file for conio.h in Linux. Is there any option for getch() & getche() function in Linux? I want to make a switch case base menu where the user will give his option just by pressing one key & process should…
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
59
votes
5 answers

How to simulate an EOF?

I am currently reading K&R's book and typing in the examples from the first section, and there are a couple of examples such as this: while((c = getchar()) != EOF) { //do something } I am testing these examples on a Windows box and thus running…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
51
votes
5 answers

`getchar()` gives the same output as the input string

I am confused by a program mentioned in K&R that uses getchar(). It gives the same output as the input string: #include main(){ int c; c = getchar(); while(c != EOF){ putchar(c); c = getchar(); } } Why…
Shubham
  • 21,300
  • 18
  • 66
  • 89
42
votes
9 answers

I'm trying to understand getchar() != EOF

I'm reading The C Programming Language and have understood everything so far. However when I came across the getchar() and putchar(), I failed to understand what is their use, and more specifically, what the following code does. main() { int c; …
Paolo Bernasconi
  • 2,010
  • 11
  • 35
  • 54
41
votes
6 answers

function similar to getchar

Is there a Go function similar to C's getchar able to handle tab press in console? I want to make some sort of completion in my console app.
demi
  • 5,384
  • 6
  • 37
  • 57
28
votes
2 answers

GetKeyState() vs. GetAsyncKeyState() vs. getch()?

What's the difference between getting a key press with: GetKeyState() GetAsyncKeyState() getch()? When should I use one over the other?
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
26
votes
6 answers

int c = getchar()?

ok so im reading this book: The C Programming Language - By Kernighan and Ritchie (second Edition) and one of the examples im having trouble understanding how things are working. #include #define MAXLINE 1000 int getline(char line[], int…
Rhexis
  • 2,414
  • 4
  • 28
  • 40
24
votes
2 answers

getc Vs getchar Vs Scanf for reading a character from stdin

Of the below three functions: getc getchar & scanf which is the best one for reading a character from stdin and why? Are there any known disadvantages or limitations for any of these functions which makes one better than the other?
Jay
  • 24,173
  • 25
  • 93
  • 141
22
votes
10 answers

Why doesn't getchar() wait for me to press enter after scanf()?

I am learning C and I'm using "getchar()" to stop the command windows so I can see the exercises am doing but it just doesn't work. heres a sample: #include int main() { int value; printf("1. option 1.\n2. option 2.\n3. option…
user158057
18
votes
5 answers

How to clear stdin before getting new input?

I have read about 5-10 different advices how to clear stdin, but none of them suits my needs. The thing is that fflush(stdin) worked perfectly at my computer, but unfortunately it doesn't seem to work everywhere, so I need something with the same…
Tom
  • 181
  • 1
  • 1
  • 4
18
votes
5 answers

putchar() vs printf() - Is there a difference?

I am currently in chapter 1.5.1 File copying and made a program like so: #include /* copy input to output; 1st version */ main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); …
user3649506
18
votes
3 answers

Why do I need to type Ctrl-D twice to mark end-of-file?

char **query; query = (char**) malloc ( sizeof(char*) ); int f=0; int i=0,j=0,c; while((c=getchar())!=EOF) { if(!isalpha(c)) continue; if(f==1) query=(char**) realloc(query,(i+1)*sizeof(char*)); …
mualloc
  • 495
  • 9
  • 24
16
votes
6 answers

Confused about getchar() function

I am confused about getchar()'s role in the following code. I mean I know it's helping me see the output window which will only be closed when I press the Enter key. So getchar() is basically waiting for me to press enter and then reads a single…
Serenity
  • 4,968
  • 19
  • 65
  • 104
12
votes
5 answers

Is there a built-in function in Python 3 like getchar() in C++?

I want to do user input in python which is similar to getchar() function used in c++. c++ code: #include using namespace std; int main() { char ch; while(1){ ch=getchar(); if(ch==' ') break; cout<
miltonbhowmick
  • 324
  • 1
  • 5
  • 17
1
2 3
61 62