I'm focusing now on this I/O thing, even writing a guide by looking some sources. I want to cover it all i can about stdin i/o and if it looks good in the end i'd love to share with you. But that's still OT.

I can't get rid of the line feed code char. x is a char, and when i write let's say "web" and hit enter, it prints out w, e, b and also a line feed.
I have tried with ((x = getchar()) && x != '\n') and also ((x = getchar()) && (int) x != 10).

What am i missing?

#include <stdio.h>

int main()
{
    while(1)
{
        char x = 0;
        printf("\nInput: ");
        while ((x = getchar()) && x != '\n'){
            printf("\nchar x -> %c \nint x --> %i\n", x, x);
        }
    }
    return 0;
}

Thank you

What am i missing?

Hmm.

Input: web

char x -> w
int x --> 119

char x -> e
int x --> 101

char x -> b
int x --> 98

Input: foo

char x -> f
int x --> 102

char x -> o
int x --> 111

char x -> o
int x --> 111

But x should be an int , as that is what getchar returns.

Edited 7 Years Ago by Dave Sinkula: n/a

Hmm.
But x should be an int , as that is what getchar returns.

Thank you Dave, it was a code::blocks problem, it was compiling old code over and over dunno why :\ so yes it actually works perfectly.

The getchar function returns the next character from the input stream pointed to by stdin.

And now i've realized that

int getchar(void);

return an...int :P

Thank you!

This article has been dead for over six months. Start a new discussion instead.