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
12
votes
3 answers

C getchar vs scanf

I am confused by a piece of code found in a function I am studying: char GetCommand( void ) { char command; do { printf( "Enter command (q=quit, n=new, l=list): " ); scanf( "%c", &command ); Flush(); } while…
startuprob
  • 1,917
  • 5
  • 30
  • 45
12
votes
9 answers

(Exercise 1.6 K&R) How to verfiy that getchar() != EOF IS 0 OR 1?

I have just started to learn programming (C) as a hobby, by myself. I'm using K&R. main() { int c; while ((c = getchar()) != EOF) putchar(c); } Verify that getchar() != EOF IS 0 OR 1 I think I understand what is happening: c is assigned the…
BBedit
  • 7,037
  • 7
  • 37
  • 50
12
votes
2 answers

setvbuf not able to make stdin unbuffered

My main intention was to make getchar return as soon as it gets a character instead of waiting for the ENTER key. I tried this int main() { setvbuf(stdin,NULL,_IONBF,0); getchar(); return 0; } Comparing this with the prototype of…
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
11
votes
1 answer

How to unit test c functions involving IO?

I am facing problems in writing unit tests to C functions which involve IO operation. For example, below is the code I wrote to get an input string from the user from console. I do not know as to how to automate testing user input using getchar()…
ramkumarhn
  • 121
  • 1
  • 6
10
votes
5 answers

read char from console

I write console application which performs several scanf for int And after it ,I performs getchar : int x,y; char c; printf("x:\n"); scanf("%d",&x); printf("y:\n"); scanf("%d",&y); c = getchar(); as a result of this I get c = '\n',despite the …
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
10
votes
4 answers

putchar() weird output, why is this happening?

If I type the words "Hello World" into the standard input stream, this program will print out weird box symbols instead of the expected "Hello World" back into standard output. #include int main(void) { // print out all characters…
zxgear
  • 1,168
  • 14
  • 30
9
votes
5 answers

Using getchar() on c gets the 'Enter' after input

I'm trying to write a simple program that asks a user to choose from a menu in a loop. I use getchar() to get the input, however i've noticed that when I enter a char and press 'Enter' the program makes two loops (as if i pressed twice) one the char…
Ari M
  • 1,386
  • 3
  • 18
  • 33
9
votes
3 answers

Theory Behind getchar() and putchar() Functions

I'm working through "The C Programming Language" by K&R and example 1.5 has stumped me: #include /* copy input to output; 1st version */ int main(int argc, char *argv[]) { int c; while ((c = getchar()) != EOF) …
Raeven
  • 613
  • 2
  • 8
  • 18
9
votes
5 answers

getchar() returns the same value (27) for up and down arrow keys

So for the up key on the keyboard, I get 27, surprisingly for the down key I also get 27. I need my program to behave differently on the up and down key, and I can't seem to figure it out. I am using Linux, and need it to work for Linux. #include…
NoNameY0
  • 612
  • 2
  • 8
  • 18
8
votes
2 answers

Add a Timeout for getchar()

I need to add a timeout function for getchar() in my program. What do I do so that when my program reaches the instruction getchar(), it will only wait for a certain amount of time for the user to make a keystroke and if the user does not make a…
brain56
  • 2,659
  • 9
  • 38
  • 70
8
votes
3 answers

Why doesn't getchar() read characters such as backspace?

This is a very basic C question, coming from page 18 of Kernighan and Ritchie. I've compiled this very simple code for counting characters input from the keyboard: #include /* count characters in input; 1st version */ main() { long…
Philip King
  • 91
  • 1
  • 4
8
votes
8 answers

The C Programming Language, Ch.1 Exercise 1.10 (Getchar and Putchar)

I've been working on this for 2 hours and I am stuck... I found the answer online, but that isn't going to help me learn the concept that I'm obviously missing. Prompt: Write a program to copy its input to its output, replacing each tab by \t , each…
user3505236
  • 83
  • 1
  • 6
8
votes
5 answers

getchar does not stop when using scanf

I have a difficulty understanding getchar(). In the following program getchar works as expected: #include int main() { printf("Type Enter to continue..."); getchar(); return 0; } However, in the following program, getchar…
oz123
  • 27,559
  • 27
  • 125
  • 187
7
votes
1 answer

printf prints additional * character

I have a very simple code to convert Upper case to lower case: #include int main() { char c; int i=0; for (i=0;i<10;i++){ c=getchar(); c=c-'A'+'a'; printf("%c\n",c ); } return 0; } But running this simple code always I…
doubleE
  • 1,027
  • 1
  • 12
  • 32
7
votes
2 answers

Read all data from stdin C

I've wrote this small function to read all the data from stdin. I need to know if this function is POSIX compatible (by this, I mean it will work under Unix and Unix-like systems) at least it works on Windows... char* getLine() { int i = 0, c; …
user3600250
1
2
3
61 62