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
1 answer

Can't read anything after sending EOF?

#include int main() { char c = getchar(); //EOF (ctrl + d ) while( ( c = getchar() ) != '?' ) { printf( "%d\n", c == EOF );//infinite loop printing 1 } } What happens here? It is as if EOF completely blocks reading…
mdjukan
  • 41
  • 3
3
votes
1 answer

Are both these two cleaning buffer methods equivalent?

I was wondering: do they do exactly the same thing? calling c = getchar on expression is the same as doing it with a do...while loop? void clrbuf(void) { int c; while ((c = getchar()) != '\n' && c != EOF); } void clrbuf(void) { int c; …
3
votes
2 answers

why does the getchar() function not work here?

I'm trying to write C code where it takes in integer inputs, and prints them, unless the newline character ('\n') is entered. But it never returns the value that I enter. If I enter 6, I expect it to print 6 but it gives me 54. In fact whatever…
Aquila
  • 29
  • 4
3
votes
1 answer

C - Switch-case prints case twice

I have written the following switch-case: char input; int run = 1; while(run){ printf("Would you like to update the student's name? Enter Y or N (Y=yes, N=no)\n"); input = getchar(); switch (input) { …
3
votes
3 answers

Copying the input from `getchar()` to another variable

In the following code example from K&R's book, if I replace putchar(c) with printf("%c", c) the code works the same. But if I replace it with printf("%d", c) it gives gibberish output. #include int main() { int c; c = getchar(); …
3
votes
1 answer

Random characters printed after printing 'result'

I'm getting random characters after the actual output in printf("%s",result);. Why are these characters being printed? And how can I remove them? #include char *replacechar(char[]); int main() { char str[25]; char *result; int…
3
votes
2 answers

Understanding "while (getchar() != '\n')"

I'd like to gain a little better understanding of how things work in the background in regards to this command. I started off with trying to prevent my application from failing when scanf() received a char instead of the int it was expecting. The…
DDT
  • 33
  • 4
3
votes
3 answers

What will be EOF here in the below code?

I was reading the C programming language book and in the given below code I am not quite getting how will we be getting an EOF here? And also I don't exactly get what is an EOF? is it a character, integer or a condition? If it is a condition then…
3
votes
2 answers

Which keyboard characters can be read with getchar() on Linux

I have a C++ application running on Ubuntu that waits for a key press using getchar() function. The input is not used, I just end my program with any keypress. I noticed I could type basically any printable character, but also some unprintable…
Marthe Veldhuis
  • 316
  • 2
  • 16
3
votes
1 answer

getchar() and reading line by line

For one of my exercises, we're required to read line by line and outputting using ONLY getchar and printf. I'm following K&R and one of the examples shows using getchar and putchar. From what I read, getchar() reads one char at a time until EOF.…
franchise842
  • 35
  • 1
  • 1
  • 4
3
votes
1 answer

Getchar() infinite loop

I’m starting to learn from "The C Programing Language" and one of the codes in the book is not working for me. This code suppose to count the number of characters using getchar(). Here is my code: #include int main() { long nc; nc =…
3
votes
2 answers

flushall() doesn't work - in C

I have to get many chars one by one with getchar() function. I have to clean the buffer after using the function, but flushall() doesn't do it. After the second iteration of the function it gets '\n' as input. I tried using fflush(), _flushall(),…
Asher
  • 57
  • 1
  • 7
3
votes
3 answers

How to flush the console buffer?

i have some code that run repetedly : printf("do you want to continue? Y/N: \n"); keepplaying = getchar(); in the next my code is running it doesnt wait for input. i found out that getchar in the seconed time use '\n' as the charcter. im gussing…
DoronS
  • 347
  • 1
  • 6
  • 12
3
votes
1 answer

How to convert ASCII code to corresponding int using getchar?

I basically want to know what to put in the last printf statement for the first %d, because from my understanding getchar converts the inputted character to ASCII code. So how do I display the inputted character? #include int main(void)…
Michael
  • 139
  • 1
  • 12
3
votes
4 answers

Can't figure out why getchar() is picking up newline for first occurence in C

I am taking a training course on "C" and running into a problem. It's hard to explain so I'll post the code. This is training syntax, so don't ask me why it's done the way it is. When both of these segment blocks are run in a main(), the second…
DNadler
  • 33
  • 1
  • 4