for a basic ascii checker code of less than 10 lines , i want to print out stuff immediately , instead of after a newline.
i understand that stdout is a bufferedstream , and so it will by default wait for a '\n'. i was looking at this page for some solutions , but so far aint working like me wants...

this is the code :

#include<stdio.h>
int main(){
    char c;
    while((c=getchar())!=EOF){
        if(c == '\n') continue;
        fprintf(stderr , " : %d \n" , c);
    }
}

i want it to printout like :
A : 65

any help would be highly appreciated.

regards
somjit.

When people talk about stdout being line-buffered, that means that if you do printf("Hello ");, then wait for some seconds and then printf("world!\n");, the output will appear all at once as "Hello world!" when the couple of seconds are over. If you used stderr instead (or you flushed stdout after the first printf), you'd see "Hello" right away and then a couple of seconds later the rest of the message would appear.

Since your format string ends with a \n anyway, that's not your problem at all. Even using stdout, your message will appear as soon as you print it, so using stderr instead changes nothing (except that it will mess with output redirection and such).

Your problem is that getchar won't return until the user entered a full line - i.e. it won't return right after the user entered the first character. That's not something you can change using only the functionality provided by the C standard. Depending on which platform you're targetting, there are platform-specific functions to do what you want though.

commented: thanks :) +3

when you say about things being line buffered , the S.O. page had this

Standard output is line buffered if it can be detected to refer to an interactive device, otherwise it's fully buffered.

what does that mean ?

It means if stdout is writing normally to the screen, it will automatically be flushed when you print a newline. But when it's writing to, say, a file or a pipe, the file won't be actually written to until the buffer is full, you explicitly flush it or the program ends.

good to know.

so in the program above, can you suggest some way (since getchar() wont do it) that i get output to ascii when i press any character ?

You want some variant of getch(). However, note that raw input is not portable, so you'll be using something specific either to your compiler or operating system.

This may help, since I don't particularly feel like writing a raw input example for the umpteenth time. ;)

thanks for the link deceptikon :) problem solved :)

just for reading , can you give some links to read about portability in c ? its been a long time since iv been with c , and forgotten a lot of stuff i learned back then.

Honestly, I don't have any links anymore since it's all been internalized. However, I would be shocked if you couldn't find some good articles with the search engine of your preference. Alternatively, if you ask a specific question (ideally in a new thread) I can probably answer it. If not, I can do a good job faking an answer. ;)

:P nah iv been in daniweb long enough to "internalize" a few good practices , will create a new thread when some new question comes up in my mind. :)

thanks for the help. :)

The stdout stream is buffered. It doesn't get written to output until either some time has passed, or the buffer is full. To ensure that output to stdout is printed immediately, then you need to call the fflush(stdout) function after the fprintf(stdout,...), or printf() function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.