How does the particular C function work?

后端 未结 3 1487
南旧
南旧 2021-02-03 12:03

I am trying to learn C and am very confused already.

In the OOP languages i have used there exists the ability to perform method overloading, where the same function cou

3条回答
  •  北海茫月
    2021-02-03 12:46

    Internally, printf will (at least usually) use some macros from stdarg.h. The general idea is (a greatly expanded version of) something like this:

    #include 
    #include 
    #include 
    
    int my_vfprintf(FILE *file, char const *fmt, va_list arg) {
    
        int int_temp;
        char char_temp;
        char *string_temp;
        char ch;
        int length = 0;
    
        char buffer[512];
    
        while ( ch = *fmt++) {
            if ( '%' == ch ) {
                switch (ch = *fmt++) {
                    /* %% - print out a single %    */
                    case '%':
                        fputc('%', file);
                        length++;
                        break;
    
                    /* %c: print out a character    */
                    case 'c':
                        char_temp = va_arg(arg, int);
                        fputc(char_temp, file);
                        length++;
                        break;
    
                    /* %s: print out a string       */
                    case 's':
                        string_temp = va_arg(arg, char *);
                        fputs(string_temp, file);
                        length += strlen(string_temp);
                        break;
    
                    /* %d: print out an int         */
                    case 'd':
                        int_temp = va_arg(arg, int);
                        itoa(int_temp, buffer, 10);
                        fputs(buffer, file);
                        length += strlen(buffer);
                        break;
    
                    /* %x: print out an int in hex  */
                    case 'x':
                        int_temp = va_arg(arg, int);
                        itoa(int_temp, buffer, 16);
                        fputs(buffer, file);
                        length += strlen(buffer);
                        break;
                }
            }
            else {
                putc(ch, file);
                length++;
            }
        }
        return length;
    }
    
    int my_printf(char const *fmt, ...) {
        va_list arg;
        int length;
    
        va_start(arg, fmt);
        length = my_vfprintf(stdout, fmt, arg);
        va_end(arg);
        return length;
    }
    
    int my_fprintf(FILE *file, char const *fmt, ...) {
        va_list arg;
        int length;
    
        va_start(arg, fmt);
        length = my_vfprintf(file, fmt, arg);
        va_end(arg);
        return length;
    }
    
    
    #ifdef TEST 
    
    int main() {
        my_printf("%s", "Some string");
        return 0;
    }
    
    #endif
    

    Fleshing it out does involve quite a bit of work -- dealing with field width, precision, more conversions, etc. This is enough, however, to at least give a flavor of how you retrieve varying arguments of varying types inside your function.

提交回复
热议问题