Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #18985

Re: avoid newline scanf

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: avoid newline scanf
Date 2012-03-24 05:39 +0000
Organization Aioe.org NNTP Server
Message-ID <20120323223437.651@kylheku.com> (permalink)
References <9472650.1688.1332561289799.JavaMail.geo-discussion-forums@ynlt17>

Show all headers | View raw


On 2012-03-24, Rafael Oliveira <sorrizo_16@yahoo.com.br> wrote:
> Hi, I'm trying to use scanf like this:
>
> scanf("%d%c%d", &a, &op, &b);
> if (op=='+')
>  c=a+b;
> printf("=%d",c);
>
> My input is 2+2, and it prints "=4". Its fine.
>
> The problem is that I dont want a newline after scanf.

That is caused by your interactive input being buffered in your
operating system, and not released into the C program until
you hit Enter.

I.e. it's not scanf that is doing this.

> There is a way of using "=" instead of the default end scanf char "enter"?

On some platforms (anything that looks like Unix) you can reprogram
the tty driver for character-at-a-time input.

I don't know about Windows. There are ways to get characters from the console,
but there is the question of whether that can be done through scanf.
Shrug. In any case, it won't be the same code as for Unix.
You're looking at different source files or #ifdefs for different
platforms.

(I would not even consider using scanf for any sort of interactive
input, whether character-at-a-time or line by line.)

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

avoid newline scanf Rafael Oliveira <sorrizo_16@yahoo.com.br> - 2012-03-23 20:54 -0700
  Re: avoid newline scanf Gene <gene.ressler@gmail.com> - 2012-03-23 21:23 -0700
  Re: avoid newline scanf Kaz Kylheku <kaz@kylheku.com> - 2012-03-24 05:39 +0000
  Re: avoid newline scanf Don Y <this@isnotme.com> - 2012-03-24 00:38 -0700
  Re: avoid newline scanf Barry Schwarz <schwarzb@dqel.com> - 2012-03-24 10:20 -0700
    Re: avoid newline scanf Rafael Oliveira <sorrizo_16@yahoo.com.br> - 2012-03-24 11:57 -0700
    Re: avoid newline scanf Don Y <this@isnotme.com> - 2012-03-24 13:21 -0700

csiph-web