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
5
votes
2 answers

getchar() and buffer order

A beginner's C book I'm reading has me confused with respect to getchar() and buffer order (especially as it relates to newline). It says, Getting rid of the Enter keypress is a problem that all beginning C programmers must face. Consider the…
yroc
  • 886
  • 1
  • 10
  • 16
5
votes
3 answers

bubble sorting and getchar in c

I am working with microsoft visual studio 2012 and trying to make a bubble sort. Here is my code: #include "stdafx.h" #include "String.h" #include #include using namespace std; int main() { int array[100], n, c, d, swap; …
Koray Durudogan
  • 624
  • 4
  • 12
  • 31
5
votes
1 answer

Why is this statement printed twice in while loop?

I wrote this simple program for practise: #include #include #include #define CLASSES 3 #define STUDENTS 4 int grades[CLASSES][STUDENTS]; int main(void) { int i = 1; char t,k; while(i == 1) { …
gixxer
  • 814
  • 1
  • 10
  • 25
5
votes
1 answer

getchar() != EOF

I am running the following program from the C Programming Language book: #include main() { int c; while((c=getchar()) != EOF) putchar(); } Or #include int main(){ int c = getchar(); while(c != EOF){ …
Nishi
  • 53
  • 4
5
votes
2 answers

How to print a variable continuously in a loop and terminate with a hit of escape key in C under linux?

Please find the section of code below. I want to print the current value of the variable continuously in a loop. And the loop has to be terminated once I hit escape key. Here the problem is that the execution stops at the getchar function. But I…
SilentCat
  • 75
  • 1
  • 7
5
votes
1 answer

What is the standard input buffer?

#include int main(void) { int c; c = getchar(); putchar(c); c = getchar(); putchar(c); c = getchar(); putchar(c); return 0; } I want to understand why the function that is called three times working with a…
YaR_
  • 53
  • 1
  • 4
4
votes
3 answers

Ctrl-C eaten by getchar()

I've been searching for a solution to my problem for a long time now that's why i'm turning to you: Consider this piece of code: static char done = 0; static void sigHandler(void) { done = 1; } int user_input() { return (getchar() == 'q') ?…
julien.d
  • 63
  • 1
  • 5
4
votes
5 answers

Why does a loop containing getchar() exit when '\n' is entered?

I was working with K&R, and it extensively uses getchar() for input in basics. But the problem is I am unable to fully understand its behavior. Below is a piece of code: #include int main() { char c,i; char line[10000]; i =…
mr.loop
  • 818
  • 6
  • 20
4
votes
4 answers

Evaluate a simple mathematical expression with limited tools, no arrays, and no library functions

Here's a question from the last year's first "Intro to programming" exam at my uni: Using the getchar() function read an input sequence consisting of numbers, + and - signs. The output should be the result of those arithmetical operations. For…
David Ilic
  • 165
  • 1
  • 7
4
votes
2 answers

C, K&R Exercise 1-6, stuck, confused

I am a noob teaching myself to program in C using The C Programming Language, Second Edition (by K&R). In Chapter 1 Section 1.5.1 File Copying, the authors touch very briefly on operational precedence when making comparison between values,…
H3G3moKnight
  • 123
  • 1
  • 9
4
votes
1 answer

How to stop enter character getting stuck in buffer? C

So in the following bit of code I'm reading an option from the user to then decide whether to perform a given action: printf("Do you want to execute %s: ", exe); char input = getchar(); if (input == 'y') { return execute(exe); } return 0; The…
transiti0nary
  • 493
  • 6
  • 25
4
votes
4 answers

C code, scanf and getchar

I'm just asking what does the getchar do in this code and how does it work? I don't understand why the getchar affects the code, to me it seems as if its just getting the value but nothing is being done with the value. int c=0; while (c>=0) { …
james
  • 101
  • 1
  • 8
4
votes
2 answers

Getting a single character without pressing enter

I'm trying to get a single character input from the user without the user having to press enter. I've tried this: #include int main() { int input; for (;;) { input = getchar(); printf("%d\n", input); } } But…
Juicy
  • 11,840
  • 35
  • 123
  • 212
4
votes
3 answers

getchar() loops over EOF when STDIN provided through a pipe

I'm in front of something I can't explain. There is no better way of explaining than giving an example : #include int main () { char c; while (1) { c = getchar(); printf("%x\n", c); } return(0); } If I…
pixis
  • 2,761
  • 1
  • 11
  • 26
4
votes
1 answer

C, can not read input

#include #include int main() { int i,j;//count int c;//for EOF test int menu; unsigned int firstSize = 12811; unsigned int lastSize; char * text=malloc(firstSize); if(text == NULL){ printf("\n…
CursedChico
  • 571
  • 4
  • 7
  • 17