Valter Silva
Valter Silva

Reputation: 16656

C: Ways to use scanf

Can I use scanf(...) as argument to a function ? Like this:

printInteger(scanf(....));

Can I use scanf to attribute the value that I read to some variable ? Like this:

n = scanf(...);

p.s.: Here I'm explaining why I'm asking this.

This question can be a little weird I know, but I'm working in a project, which is developing a compiler that takes some language as input and then compile to C.

For example, this is my language, let's call 'stackoverflow' ;)

proc printInteger(integer k)
    integer i;
        begin
            for i = 1 to k do
                print i;
        end

proc main()
    integer n, i;
    boolean ok;
    begin
        printInteger(getInteger);
        n = getInteger;
        ok = true;
        while i < n do
            begin
                print i;
                i = i + 1;
            end
        if ok then print 1; else print 0;
    end

I won't get deeper in the language, but notice that getInteger means that I would like to do a scanf(...), what I mean is, when appears getInteger I would like to compile as scanf(...), so that's why I would like to know some ways to use scanf(...).

Upvotes: 1

Views: 2393

Answers (2)

pb2q
pb2q

Reputation: 59637

Can I use scanf(...) as argument to a function ? Like this:

printInteger(scanf(....));

Can I use scanf to attribute the value that I read to some variable ? Like this:

n = scanf(...);

You can use scanf as an argument to a function, but the real answer to both questions is no: scanf doesn't return any data scanned, it returns the number of items successfully scanned - or EOF if the end-of-input is reached before any successful scanning. You only get access to the items scanned using the pointers that you pass as scanf arguments to receive the values. So while you can pass scanf as an argument to a function, it won't do what you seem to want.

If you want to implement the getInteger operation in your language, in C, it's hard to make suggestions since only you know how this language/operation should work. Just using scanf, the implementation would look something like this:

int nextInt;
int numScanned = scanf("%d", &nextInt);

if (numScanned < 1)
    handleError();
return nextInt;

But if you're doing general parsing for your language, then using scanf is a bad idea: you'll soon run into problems with the limitations of scanf, and you're not going to be able to anticipate all of the input types unless your language is really simple, simpler than the example that you've included.

To do this properly, find a good lex library for C. This will prevent a lot of headaches. Otherwise, if you must do the lexing yourself, start looking over fgets, get a line at a time from your input, and do the tokenizing yourself.

Upvotes: 5

Jonathan Leffler
Jonathan Leffler

Reputation: 754920

You ask:

Can I use scanf(...) as an argument to a function like this?

printInteger(scanf(....));

The answer to the first question is "Yes, but ...".

Can I use scanf to attribute the value that I read to some variable like this?

n = scanf(...);

The answer to the second is "No, because ...".

The "but" is mostly 'but it does not do what you expect so you would very seldom, if ever, do so'.

In the first example, scanf() returns either the (integer) number of successful conversions, or EOF if it reached EOF. In no case does it return the value that it just read (not least because, in general, it reads multiple values and most of them are not integers). So, if you want to print the number of values that was converted, you could use the printInteger() function to do so, but it is not what you'd normally want to do.

Similarly, in the second case, you can certainly assign the result of scanf() to an integer n as shown (and it is often sensible to do so if you're going to need to report an error). However, that is not the value that was read (assuming you had a %d conversion specification); it is the number of successful conversions.

Upvotes: 1

Related Questions