948 questions
1
vote
0
answers
73
views
C program in CLion not producing expected output, possibly related to I/O buffering
I'm learning C and wrote a simple program in CLion that echoes '1' for every character read until EOF. However, the actual output doesn't match expectations.
Code:
#include <stdio.h>
int main() ...
-1
votes
3
answers
192
views
Is there a reason why I don't see much of getchar() used in c? [closed]
When I see code for getting input I see scanf() used most of the time. I see some from the link taking-string-input-space-c-3-different-methods and I also see it on this website.
Is there something ...
0
votes
0
answers
57
views
Simplest timer in C [duplicate]
I am trying to make the simplest timer program in C. My attempt is as follows:
#include <stdio.h>
#include <time.h>
int main()
{
char c;
time_t start, end;
long int day, ...
1
vote
2
answers
141
views
getchar() keeps waiting after input - continues only after pressing ENTER again
I am having a problem with my program getting stuck on waiting for input after using getchar(). I used a loop to clear the buffer and the same functionality appears four times in my program but only ...
1
vote
1
answer
97
views
k&r: Exercise 1-18. Program takes input but doesnt produce any output?
i'm currently going through K&R. right now i'm at exercise 1-18: "Write a program to remove trailing blanks and tabs from each line of input,
and to delete entirely blank lines.".
...
3
votes
3
answers
170
views
Issue with getchar() Skipping Input in a C Program
A number guessing C program; After the 4th iteration which prompts “well, is it 5 then?”, the character \n (enter) still remains in the buffer which then gets read by the first getchar() in the while ...
4
votes
5
answers
171
views
getting a stream of -1 when I pass EOF in an infinite getchar printf loop
I have just started the K&R book. It introduces a loop using getchar() and putchar() to copy characters from input to output until EOF is reached:
main()
{
int c;
while ((c = getchar()) !=...
3
votes
2
answers
190
views
Linux: Catching Ctrl+C breaks stdin
I'm writing a test application for a console text input library. The idea of that part of the library is to just treat any input as a regular text input, even Ctrl+[letter] combinations. And for the ...
0
votes
0
answers
32
views
Why can't I get an output on the printf statement in C language right after the scan statement, the run command skips it completely [duplicate]
I am new to C language coding, I was trying to learn the various inputs that I can take from the user and display the same but I keep getting a blank in the place of the printf statement that takes ...
2
votes
1
answer
129
views
How to detect the Enter key when pasting multi-line text (containing `\r`)?
While developing a line editor in C++, I'm using getch() to capture each key input, and see \r(13 in ASCII) as the Enter key. However, when pasting text that contains multiple lines, the program ...
0
votes
1
answer
128
views
Ungetc() Not Ungetting Character
My code is extremely simple, but I figure that's best to figure out what's going on in my program. For some reason, I can't seem to get ungetc() to work properly, nor can I figure out it's purpose. ...
-1
votes
2
answers
137
views
How can i make a C program exit when key Q is pressed?
i am making a counting program, and i want to add a feature that quits the program, but i don't really know how to do it. i have seen examples on how to do it, but all of them are for windows. please ...
0
votes
0
answers
66
views
Why does my printf function not work? Is my while loop not ending? [duplicate]
I am just trying to copy and paste the text input. After which the code must show done.
#include <stdio.h>
/*code to copy all input characters and paste it one by one*/
int main()
{
int c;
...
-1
votes
1
answer
108
views
getchar() taking stuff which I print using printf()
#include <stdio.h>
int main() {
int c;
for (int i = 0; i < 10; i++) {
c = (getchar() != EOF);
printf("%d\n", c);
}
printf("As we can see, the value of c is ...
3
votes
1
answer
100
views
Strange printf not working after a getchar and getchar skipping
I'm getting crazy with C.
I'm programming with JavaScript, Ruby, Python, PHP, Lua, even Java...
Recently, I tried to program a simple Read-Eval-Print-Loop in C.
And I have a very strange behavior with ...
0
votes
2
answers
104
views
getchar() and its buffer
I wrote this code to get familiar with getchar(). I took line
while(ch != '\n' && ch != EOF)
ch = getchar();
from a book. What is a mystery to me is that in the second while loop using ...
0
votes
3
answers
158
views
getchar() and space-character
I am experimenting with the getchar() function. Can someone please explain why every time I finish an input line with a space and then press Enter, the input window skips a line and then I have to ...
1
vote
2
answers
137
views
Why does this python program hang the terminal when it exits on Linux or WSL?
From what I can tell, this program confuses the terminal emulation on both Linux and WSL, on both python 2 and 3.
Once the program exits, characters are no longer echoed and normal terminal function ...
1
vote
1
answer
113
views
Does getchar read ctrl+z or return EOF?
I have this code:
#include <stdio.h>
int main(void) {
int d=0;
d = getchar();
printf("%d\n", d);
}
Output:
^Z
-1
From what I understand there are two things that could be ...
1
vote
0
answers
57
views
getchar is able to read ctrl+z but not scanf?
What I want to do is find out if scanf can read ctrl+z as EOF? I know that getchar and scanf return EOF when the end of the file is reached. This I have no problem with. What I want to do is have them ...
0
votes
2
answers
99
views
Why does getchar in C accept many characters in a loop but not outside one
In the following program:
#include <stdio.h>
int main() {
int c;
c = getchar();
putchar(c);
}
Even if If write many characters in the input and press enter, it only prints the ...
3
votes
5
answers
174
views
Does a character have to be casted to unsigned char before being compared to getc family returns?
I am not sure about whether I have to cast a character to an unsigned char before being compared to the return of a getc family function.
The functions I consider getc family, are getc, fgetc and ...
1
vote
0
answers
42
views
why getchar() doesn't read the input? [duplicate]
#include<stdio.h>
int main(void){
int character;
double balance,amount;
printf("Enter the balance: ");
scanf("%lf",&balance);
printf("Enter ...
-1
votes
1
answer
97
views
what is the use of getchar() in following program [duplicate]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NUM_DAYS_IN_WEEK 7
typedef struct
{
char *acDayName;
int iDate;
...
1
vote
1
answer
62
views
Why am I doing wrong with getchar()? (initializer element is not constant)
The code:
#include <stdio.h>
#include <stdbool.h>
typedef enum{
counting1,
counting2,
searching,
possiblecom,
possibleend,
} read_state;
bool issep(char a){
return (a == ' ' |...
0
votes
1
answer
145
views
Why does while((c = getchar()) != EOF) repeat 2 times in c programming?
When I enter a number from keyboard, while loop goes 2 times, and I didn't understand why it is doing that. I want to run this loop for one time after each entering number. Is there anyone who can ...
0
votes
2
answers
70
views
getchar() prompting for input when typing space before Enter
A part of the code I'm writing has to read a sequence of integers separated by a space from input into an array.
It works fine when I input, for example, 3 numbers, and then press Enter. But if after ...
3
votes
3
answers
217
views
Loop skips over getchar in C?
I am working on a simple program in C that asks the user to give an input in a loop until they enter the letter Q when prompted. When I run the program however, it immediately skips the prompt to ...
0
votes
1
answer
56
views
Problem with reading/writing on the terminal line
I am trying to write a function that reads an input from the terminal using getchar() and stores it in a string and then display it in 2 different formats.
The read line should be stored in the ...
0
votes
1
answer
76
views
Why does my 'do while' loop first asks for getchar before executing printf?
I'm new to programing and trying to learn C on my own. However I've encounetred a problem I don't know how to solve. This is my simple program. Sorry for grammar mistakes english is my second language....
0
votes
3
answers
86
views
C switch, how to fix multiple default prints?
If i give my program this:
abcdefghijklmnopqrstuvwxyz
It answered with this?!
Invalid command a
Invalid command c
Invalid command e
Invalid command g
Invalid command i
Invalid command k
Invalid ...
0
votes
1
answer
169
views
The use of EOF (Ctrl + D) in getchar() in C
I'm going through section 1.9 in K&R, I don't understand the use of EOF in the code implemented below
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int ...
1
vote
2
answers
200
views
Problem: using getchar() for Umlauts/Umlaute (Ö, Ä, Ü, ß)
I want to run a simple program:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c;
printf("Please enter a letter: ");
while ((c = getchar()) !='.')
printf(&...
0
votes
1
answer
145
views
counting of characters in c programming language using getchar() to get input character
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num=0,ch='9';
while((ch=getchar())!=EOF){
++num;
}
printf(&...
-1
votes
2
answers
117
views
Fgets skip first character
When I take user’s name as input and use that information to do another task, the output does not contains first character of the first student name.
int main() {
// Task1 - Enter student number
...
0
votes
1
answer
54
views
How to create a program that requires number inputs that exits after typing a non-numeric character, using getchar() and infinite loop in C
An assignment asks us to create a program in C that asks for user input of only numbers, using getchar() and infinite loop with while(1), that stops after typing a non-numeric character.
This is my ...
0
votes
0
answers
43
views
getchar() does not read EOF when not following ENTER [duplicate]
I need to read the following format of text:
text
textEOF
Where EOF is a single CTRL+D and the rest are just the characters (\n is important).
The only way it does work is by adding another EOF after ...
0
votes
1
answer
109
views
using getchar() function to pause program but something's wrong with it
I tried to using getchar() function to pause program when user enters 'm' or 'M'.
The main problem is I don't know why I can't use :
while (check != 'm' || check != 'M' );
Instead of:
while (check != ...
0
votes
0
answers
60
views
How can I print input piped in from a file, but not reprint input entered on the command line, in a simple shell program in C?
I have a simple shell program in C that needs to be able to run as a command line and take in input from the user and also take in input from a file piped in as < commands.txt. for the purposes of ...
-2
votes
1
answer
104
views
Function that gets char instantly without pressing enter
I'm new to programming, and I try to program a menu in C++.
I want it to be in a while loop, breaking when ESC is pressed, but I want the character to be read instantly without having to press Enter.
...
0
votes
1
answer
81
views
How to fix conditional jump?
I have a problem with, i think, function INPUT. But i can't understand how to fix it
[functions code]
typedef struct Item {
int data;
struct Item* next;
} Item;
typedef struct Queue{
...
0
votes
0
answers
154
views
C how to get stdin from pipe and get user input in the same program (or clear stdin during execution?)
I have a C program for linux where the user is intended to pipe information into the program (echo "hello\nworld" | cprogram), and then interactively search through that information. This ...
1
vote
1
answer
141
views
Clearing the buffer not working as expected?
I have the following code.
int x1, x2;
char check[3];
do{
//input should be (x1,x2)
successfully_inputed = scanf("(%d,%d%2s", ...
0
votes
1
answer
102
views
Read header of PPM file only using getchar()
I need to read a PPM file but I'm limited to only using getchar() but I'm running into trouble ignoring whitespaces.
I'm using num=num*10+(ch-48); to read the height and width but don't know how to ...
0
votes
2
answers
82
views
Why are two getchar() functions used?
I'm trying to write a program which task is to get rid of extra whitespace in a text stream.
#include <stdio.h>
int main()
{
int c;
while( (c = getchar()) != EOF)
{
if(...
-1
votes
2
answers
71
views
While loop in C prints the same line more than once
char ch;
int nr=0;
printf("\n: ");
ch = getchar();
while(ch != 'q' && ch != 'Q'){
ch = tolower(ch);
if(ch == 'a' || ch == 'e' || ch == 'o' || ch == 'i' || ch == 'u')
...
0
votes
1
answer
2k
views
ESP32: fgets() to read from Serial input does not wait for input
Using ESP-IDF and a ESP32S2 microcontroller, I want my program to wait for input using fgets(), then after pressing Enter printing my input using printf().
void app_main(void)
{
char command[64] = ...
1
vote
1
answer
182
views
Passing character array to a function returns warning
Currently completing my C homework for the week and i'm messing about with pointers for the first time properly.
We are told to create a main function to deal with our readLine function thats premade ...
0
votes
5
answers
1k
views
Checking if input is a single character and meets validation in C
I'm new to the C language and have to write a program that asks the user to type the letter "t" in uppercase or lowercase and then press enter. The program should inform you if you entered ...
1
vote
4
answers
172
views
get string in input and print it folded in C
I'm trying to write a program which gets one or more input lines, and if one line is too long, it gets folded at a maximum number of chars. My approach would be to write the input chars in a first ...