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

difference in the usage of getch() and getchar() for clearing input buffer?

CODE 1:- char ch1, ch2; printf("Input the first character:"); scanf("%c", &ch1); while(getchar()!='\n'); printf("Input the second character:"); ch2 = getchar(); In this case while(getchar()!='\n');, clears the effect of enter-key pressed for…
kevin gomes
  • 1,775
  • 5
  • 22
  • 30
3
votes
1 answer

query regarding getch() input

CODE:- char ch,ch1; ch=getchar(); ch1=getch(); printf("%c\n%c",ch,ch1); When I enter a character during ch=getchar(), I have to press enter key, which remains in input buffer. That enter key is not read by the ch1=getch(). Why ch1=getch() is not…
kevin gomes
  • 1,775
  • 5
  • 22
  • 30
3
votes
5 answers

About storing getchar() returned value inside a char-variable

I know the following code is broken --getchar() returns an int not a char-- #include int main(int argc, char* argv[]) { char single_byte = getchar(); while (single_byte != EOF) { single_byte = getchar(); printf("getchar() !=…
Giuseppe Crinò
  • 486
  • 3
  • 15
3
votes
1 answer

How to skip getchar() if there is nothing to read?

I have written a simple, interactive, program that expects inputs from the user. After it has read a specific input it takes a specific action, but first checks if the user has entered too many commands by reading from the stream any left over…
AYR
  • 1,139
  • 3
  • 14
  • 24
3
votes
3 answers

Are getchar() and putchar() functions or macros?

I referred to two reliable sources for the information and both seems to have different definitions of the same thing: http://www.cplusplus.com/reference/clibr%E2%80%A6 http://www.ocf.berkeley.edu/~pad/tigcc/doc/html/stdio_fputchar.html The first…
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
3
votes
1 answer

EOF exercise 1-6 K&R The C programming language

This is taken directly from the K&R book: The precedence of != is higher than that of =, which means that in the absence of parentheses the relational test != would be done before the assignment =. So the statement c = getchar() != EOF is…
Prafiate
  • 33
  • 4
3
votes
1 answer

k and r line counting 1.5.3 not working?

I'm having trouble with k&r 1.5.3. Obviously I'm a complete beginner. Below is the code exactly from the book and exactly as I typed it. It compiled fine and runs. It returns characters but just never prints the line count. I'm using ssh into a…
3
votes
3 answers

Why is getchar() reading '\n' after a printf statement?

I'm prompting the user to enter the length of an array, initializing a char[] array with this input, and then prompting the user to type a message to enter into the char[] array. I'm reading the first character of the user's message with…
Ian Campbell
  • 2,678
  • 10
  • 56
  • 104
3
votes
1 answer

A interesting practice about getchar() function

When I do practice on K&R,I found a very interesting question: code as follows: include main() { …
kursk.ye
  • 389
  • 1
  • 3
  • 12
3
votes
2 answers

Getting capital O from getchar when pressing home or end

I'm using this in my C code: system("stty -echo -icanon"); This is part of a homework assignment, but this particular part is something I'm working on beyond the requirements of the assignment. We're implementing a shell, and we've been given a…
beatgammit
  • 19,817
  • 19
  • 86
  • 129
3
votes
3 answers

What are these additional characters that precede the null character?

I am following Kernighan and Ritchie's C book but I'm having some difficulties working with strings. In the following code, it appears that my strings, while inputted from the user via getchar() contain additional, what I would say are junk…
EMiller
  • 2,792
  • 4
  • 34
  • 55
3
votes
5 answers

Cancelling getchar()

I've got a small program that does a large amount of processing. The progress of which you can get a print of by hitting the enter key. The way I've implemented this is by having the processing done in the main thread whilst I have a pthread…
rhlee
  • 3,857
  • 5
  • 33
  • 38
3
votes
1 answer

getting numbers from stdin to an array in C

I'm trying to get numbers from stdin to an array. the first number in stdin is the number of elements in the array (the number can be any int). I did this to get the first number: while(c=getchar()!=' '){ n*=10; n+=atoi(c); } And then created an…
MinaHany
  • 1,015
  • 4
  • 16
  • 32
2
votes
1 answer

C input - getchar()

It's a basic question.. but had to ask. For a program like this, if the use case is 123^Z, the program doesnt terminate, even though i put an EOF at the end (Ctrl+Z). Why is that so? It's only when I put an EOF after a CR that it works. Any anwers…
Iceman
  • 4,202
  • 7
  • 26
  • 39
2
votes
5 answers

Terminate a while loop using `getchar()`

For my homework assignment, I need to implement Horners Algorithm for converting between bases. I have been told to use getchar() for this assignment. But I am having a problem where when I hit enter, the program doesn't terminate and just takes in…
Snow_Mac
  • 5,727
  • 17
  • 54
  • 80