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

Is getchar() equivalent to scanf("%c") and putchar() equivalent to printf("%c")?

Is a = getchar() equivalent to scanf("%c",&a);? Is putchar(a) equivalent to printf("%c",a); where a is a char variable?
Gautam Kumar
  • 1,162
  • 3
  • 14
  • 29
7
votes
9 answers

Putchar and Getchar in C

I'm reading K&R's The C Programming Language and have become confused on putchar and getchar. I made a program where you enter 10 chars and the program prints them back out to the screen. #include int main() { int i; int ch; …
CS Student
  • 1,613
  • 6
  • 24
  • 40
6
votes
1 answer

Is there difference between scanf("%c",&x) and x=getchar()?

I'm writing to understand as much as I can of the following program that I tried to compute, because I thought it had some possible application for everyday life. The program goes like this (it's a cypher): #include void cifrario(int k)…
jacopoburelli
  • 257
  • 2
  • 9
6
votes
3 answers

Why does this C program print weird characters in output?

I've the following program: #include int main() { int ch; while( ch = getchar() != '\n') { printf("Read %c\n",ch); } return 0; } No matter what I enter I get: Read Why is this…
user437821
  • 71
  • 1
  • 6
6
votes
2 answers

Why Ctrl-Z does not trigger EOF?

Why Ctrl+Z does not trigger the loop to finish on the following small program? #include main() { int c; while ((c = getchar()) != EOF) { //nothing } return 0; } If I enter: test^ZEnter, it does not get out…
Alex
  • 5,510
  • 8
  • 35
  • 54
6
votes
4 answers

getchar() and putchar()

in the example: #include main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } I don't quite understand it. putchar() would put the character out, but why is it that after EOF it puts…
user379229
  • 95
  • 1
  • 5
6
votes
4 answers

Cannot figure out how to use getchar(); in C

#include int main(void) { char F,C; printf("Do you have a Fever? y/n\n"); F = getchar(); printf("Do you have a runny nose or cough? y/n\n"); C = getchar(); printf("Here are the results you input:\n"); …
user2824931
  • 83
  • 1
  • 1
  • 3
6
votes
7 answers

ProbIem with EOF in C

I'm writing a program which is supposed to read two strings that can contain line breaks and various other characters. Therefore, I'm using EOF (Ctrl-Z or Ctrl-D) to end the string. This works fine with the first variable, but with the second…
user196316
6
votes
4 answers

Clarification needed regarding getchar() and newline

I have a doubt regarding using getchar() to read a character input from the user. char char1, char2; char1 = getchar(); char2 = getchar(); I need to get 2 chars as inputs from the user. In this case, if the user enters the character 'A' followed by…
Raj
  • 4,342
  • 9
  • 40
  • 45
5
votes
2 answers

error: A label can only be part of a statement

I'm writing a brainfuck interpreter in C, and I'm having a little bit of trouble with the use of somethings I'm not used to. In brainfuck, a comma ( ,) is essentially getchar(). So I have the following code: //This is just ptr static char…
Joshua Hedges
  • 255
  • 1
  • 3
  • 13
5
votes
5 answers

Please Explain this Example C Code

This code comes from K&R. I have read it several times, but it still seems to escape my grasp. #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; int getch(void) { return(bufp>0)?buf[--bufp]:getchar(); } int ungetch(int c) { …
user485498
5
votes
6 answers

getchar and putchar

My C code: int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } Why does this program react like this on inputting hello? hello hello and not like: hheelloo
user494461
5
votes
2 answers

Get a single character in Python as input without having to press Enter (Similar to getch in C++)

First of all, I am totally new in Python, having just started to learn it. I know a lot of stuff about C++ however and I am just trying to implement some of those in Python. I have done quite a search on it but I couldn't find any solution that fits…
Sakib Khan
  • 305
  • 2
  • 13
5
votes
1 answer

k&R,how getchar read EOF

while reading from k&r i came across the following example #include int main() { int c; while((c=getchar())!=EOF) { putchar(c); } printf("hello"); } doubt 1:when i am typing the character ctrl+z(EOF on my sys) . o/p is hello but when i…
Tarun
  • 3,162
  • 3
  • 29
  • 45
5
votes
1 answer

using getchar() in C; does it move to the next char every time I use it? Including within assignment operations?

I am using getchar() while writing a program in C (scanf is not allowed yet at this point in the course.) I was wondering if every single time I call it if it moves to the next one; including during assignment operations. For example; I am trying to…
MaddieSun
  • 101
  • 8
1 2
3
61 62