getchar

Strange behavior using getchar() and -O3

I have these two functions void set_dram_channel_width(int channel_width){ printf("one\n"); getchar(); } void set_dram_transaction_granularity(int cacheline_size){ printf("two\n"); getchar(); } //output: one f //my keyboard input two one f //keyboard input tw...

C : How to simulate an EOF ?

I am currently reading K&R's book and typing in the examples from the first section, and there are a couple of examples such as this: while((c = getchar()) != EOF) { //do something } I am testing these examples on a Windows box and thus running the compiled exe files from the cmd prompt. To test the example above, how do I simula...

problem with getchar() in C

Well am learning C, and this is probably the stupidest question, but here it comes: I'm using "getchar()" to stop the command windows so I can see the exercises am doing but it just don't work. heres a sample: #include <stdio.h> int main() { int value; printf("1. option 1.\n2. option 2.\n3. option 3.\n4. Exit\n\nMake an option...

Problem 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 variable, however, this seems to be problematic as apparently something is stuck in the input b...

How to avoid press enter with any getchar()

Hi, this is a "novato" question for the c programming language: in the next code: #include<stdio.h> int main(void){ int c; while((c=getchar())!= EOF) putchar(c); return 0; } I have to press ENTER to print all the letters I entered with getchar,but I don't want to do this, what I want to do is to press the lett...

C: How do I get a program using getchar to run?

I'm a total C n00b trying to teach myself C off K&R. My question is kind of embarrassingly elementary. OK, here goes: I can't get programs using getchar to give the kind of output I expected. If you happen to have K&R on hand, I'm stuck on exercise 1.13. The question goes, "Write a program to print a histogram of the lengths of words in ...

C - getchar() in a loop?

How I can use getchar() in a loop? Now I have... for (p=0; p<n_players; p++) { ... fflush(stdin); getchar(); } But it doesn't work... if n_players is 3, it execute getchar 2 times only at the end... for (p=0; p<n_players; p++) { blank_start(); ascii_art_title(); printf("%s, tocca a te...\n",player_info[p].play...

getchar() and putchar()

in the example: #include <stdio.h> 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 all the characters out, and where is it remembering all these characters? Thanks. ...

getchar() and counting sentences and words in C

I'm creating a program which follows certain rules to result in a count of the words, syllables, and sentences in a given text file. A sentence is a collection of words separated by whitespace that ends in a . or ! or ? However, this is also a sentence: Greetings, earthlings.. The way I've approached this program is to scan through t...

How can I convert my code to using getchar() putchar() instead of using scanf() and printf() for I/O?

I need to use getchar(), putchar() for I/O. My program works just fine but I cannot use it like it is in CodeWarrior for an embedded system. I also need to get rid of malloc() and just use a stack for pop/push. Can I still use strtol if I am no longer using scanf? #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1...

Confused about getchar() function

I am confused about getchar()'s role in the following code. I mean I know it's helping me see the output window which will only be closed when I press the Enter key. So getchar() is basically waiting for me to press enter and then reads a single character. What is that single character this function is reading? I did not press any key ...

Why does this getchar() loop stop after one character has been entered?

#include <stdio.h> int main() { char read = ' '; while ((read = getchar()) != '\n') { putchar(read); } return 0; } My input is f (followed by an enter, of course). I expect getchar() to ask for input again, but instead the program is terminated. How come? How can I fix this? ...

Is getchar() equivalent to scanf("%c",&a)?

Is getchar() equivalent to scanf("%c",&a);? Is putchar(c) equivalent to printf("%c",a); where a is a char variable? ...

Why is this exception thrown in the visual studio C compiler?

Hello. I am trying to get more adept and my C programming and I was attempting to test out displaying a character from the input stream while inside of the loop that is getting the character. I am using the getchar() method. I am getting an exception thrown at the time that the printf statement in my code is present. (If I comment ou...

getc Vs getchar Vs Scanf for reading a character from stdin

Of the below three functions: getc getchar & scanf which is the best one for reading a character from stdin and why? Are there any known disadvantages or limitations for any of these functions which makes one better than the other? ...

Using getchar() in a while loop

#include <stdio.h> main() { int c ; while ((c = getchar()) != EOF) { int isEOF = (c==EOF); printf("is %c EOF: %d ", c, isEOF); } } Why printf() method is called twice on every input char here? If i give a input 'a', I am getting the result like E:\C_workouts>gcc CharIO.c -o CharIO.exe E:\C_workouts>C...

Why does this C program print weird characters in output?

Dear all, I've the following program: #include <stdio.h> 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 happening and what is that weird char that I see? Stackoverflow is not printing th...

C getchar vs scanf

I am confused by a piece of code found in a function I am studying: char GetCommand( void ) { char command; do { printf( "Enter command (q=quit, n=new, l=list): " ); scanf( "%c", &command ); Flush(); } while ( (command != 'q') && (command != 'n') && (command != 'l') ); printf( "\...

String input using getchar()

The following code uses getchar() to accept a line of input. #include <stdio.h> #include <stdlib.h> int main() { char *rawString = (char *)malloc(200*sizeof(char)); char *rawStringInitial = rawString; char c; c=getchar(); while(c!='\n') { *rawString=c; rawString++; c=getchar(); } *rawString='\0'; printf("\n[%s]\n",rawStr...

Printing a string in C using a function

I'm brand new to C and am trying to learn how to take a string and print it using a function. I see examples everywhere using while(ch = getchar(), ch >= 0), but as soon as I put it into a function (instead of main()), it ceases to work. Right now, it's stuck in an endless loop... why is that? // from main(): // printString("hello"); v...