anybody help me "how to write our own (by user) scanf function so that it will works exactly as scanf function defined in liberary, what will be its declaration and definition ".

How exactly are we talking here, because scanf() is a rather complex function. Ignoring for a moment that there are actually 6 functions in the scanf() family that have a slightly different interface each, the declaration of scanf() itself is like so:

int scanf(const char *fmt, ...);

The definition will probably defer to one of the vscanf() functions:

int scanf(const char * restrict fmt, ...)
{
    va_list args;
    int rv;

    va_start(args, fmt);
    rv = vfscanf(stdin, fmt, args);
    va_end(args);

    return rv;
}

Which in turn will likely call into an internal function for the actual work of parsing the format string and doing string to type conversions. Let's say you wanted to write your own scanf() that only supports %d and with no real error checking as characters are extracted from the stream. It might look like this:

int myscanf(const char *fmt, ...)
{
    int converted = 0;
    va_list args;

    va_start(args, fmt);

    while (*fmt) {
        if (*fmt++ == '%') {
            if (*fmt == 'd') {
                /* Parse and convert an integer */
                char buf[BUFSIZ];
                int i = 0, ch;
                char *end;

                /* Read up to whitespace */
                while ((ch = getchar()) != EOF && !isspace(ch))
                    buf[i++] = (char)ch;

                if (i == 0)
                    break; /* No valid characters */

                /* Attempt the conversion */
                *va_arg(args, int*) = (int)strtol(buf, &end, 0);

                if (*end)
                    break; /* A complete conversion failed */

                ++converted;
            }
            else {
                break;
            }
        }
    }

    va_end(args);

    return converted ? converted : EOF;
}

To support the full range of functionality and specifiers though, it's more work and would look something like this (which you'll note is not self-contained. There are several supporting libraries handling some of the trickier format parsing and conversions). Somehow I doubt that your assignment is to implement a complete and ISO conforming implementation, because that's well out of scope for even a graduate course in computer science.

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.