Thread: printf() --can I duplicate it?

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    printf() --can I duplicate it?

    Code:
    _CRTIMP int __cdecl printf(const char *, ...);
    Ok this is the prototype in the header file stdio.h
    now, notice the ..., i know from use that the '...' allows me to clarify values inputted into the string aka
    Code:
    printf("hello, d = %d\n",d);
    how exactly does this work and can I duplicate it, how would the function definition look?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is kind of crappy in terms of utility but you get the idea.
    Code:
    #include <stdarg.h>
    #include <stdio.h>
    
    int stuff(int number, ...) {
       va_list list;
    
       va_start(list, number);
       for(;number--;)
          printf("%d ", va_arg(list, int));
       va_end(list);
    }
    
    int main(void) {
       stuff(5, 35, 21, 32, 11, 10);
       return 0;
    }
    the first parameter represents the number of ints to follow. Each following parameter is printed to the screen.

    [edit]turned off those damn smilies[/edit]

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    but printf doesn't know how many parameters are passed into it...
    right?

    yeah well I kinda get that now, but if you could fine the actual C definition for printf, that would be cool
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM