Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   scanf calling (http://www.go4expert.com/forums/scanf-calling-t4322/)

fsakalos 15May2007 22:50

scanf calling
Hi all, I have simple question. Do you know how to call character written during scanf back?

DaWei 16May2007 00:14

Re: scanf calling
I'm not sure I understand your question. Scanf doesn't write, it reads. Once you have read a character with scanf, you cannot put it back into the stream in any portable way. If you are using buffered input and if you understand how your particular implementation works, you can write your own functions to peep at the next character before taking it, or to put the last character taken back into the buffer.

Generally speaking, this is how C input works: The operating system collects and manipulates keyboard input and places the characters into an OS buffer. This is not a buffer manipulable by the programmer.

When the programmer calls an input function, the language makes a blocking call for input using a low-level read. This call does not return until an end of line or end of file is found. When that condition is detected, the collected characters are put into the C input stream. If the stream is not buffered, some data may be lost. It depends on how the programmer uses the various input functions.

Functions that are not provided by the language may be written by the programmer. This act requires a knowledge of the platform and OS in use. For instance, one may do just about anything by writing functions that use the Win Console API. Similarly, but not the same, for Unix variants.

fsakalos 16May2007 00:29

Re: scanf calling
OK, I'll change my question. I have a variable named "number". Until some point it doesn't have any value but I want to assign a value for it. User will set that value. This should be program for power of 2 but not only. I want to have a chart like he writes 5 so it will be Pof2 for 5, 6, 7, etc. Problem is, that it always starts from 0 and not from user's value.

fsakalos 24May2007 00:09

Re: scanf calling
Please answer something

DaWei 24May2007 03:09

Re: scanf calling
Man scanf and read the documentation carefully. You also need to consider how to provide information with your question. You don't even mention the language. This is for C. C++ has overloaded pow functions.

#include <stdio.h>
#include <math.h>

int main()
    double base = 2.0;
    double exp;

    while (1)
        printf ("Enter the exponent: ");
        if (scanf ("%lf", &exp) == 1)
            printf ("2 raised to the power of %f is %f\n", exp, pow (base, exp));
            printf ("Invalid input.  ");
    return 0;


Originally Posted by Output
Enter the exponent: 4
2 raised to the power of 4.000000 is 16.000000
Enter the exponent: 16
2 raised to the power of 16.000000 is 65536.000000
Enter the exponent:

All times are GMT +5.5. The time now is 05:52.