Thread: printf

  1. #1
    Registered User
    Join Date
    Jul 2003


    I was going through this program that used printf function when I realized that it (printf) could take unlimited number of arguments. Using overloading we can specify different number of values to be passed, but how does overlaoding happen in printf function (as we cannot define it unlimited number of times)?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    There is no such thing as overoading in C. That is a C++ feature. Printf uses a set of macros to handle this.

    Look up va_args, va_list, etc.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Yes.This is known as Variable argument lists functions.Please go through Lesson 17 in the tutorial section of this site for further clarifications.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >how does overlaoding happen in printf function (as we cannot define it unlimited number of times)?
    "Overloading" is a poor choice of words to describe a variable number of arguments. But, it is accomplished through macro black magic. Here are the macros for MSVC++ 6:
    #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
    #define va_start(ap,v)  ( ap = (va_list)&v + _INTSIZEOF(v) )
    #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
    #define va_end(ap)      ( ap = (va_list)0 )
    Of course, writing functions that use these macros doesn't require intimate knowledge of the black arts, you can just follow the tutorial here.
    My best code is written with the delete key.

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Huh.... I didn't know that...

    It all makes sense now...

    There is no such thing as overoading in C.
    There's no such thing as 'overoading' anywhere.

    About variable length argument functions...

    1) every parameter is promoted upwards the highest it can.
    2) The type information of these arguments must be provided manually by the programmer of the function. (In other words, the compiler can't tell an int from a char, or a void* from a hole in the ground.)
    3) printf uses symbols like '%c' and '%d' to let you specify what type the variable is. You could have more symbols than values in the function, and the program wouldn't know it. (You'd get garbage, most likely.)
    4) special functions like ioctl use the '...' part of the function, the variable argument part, for the sole purpose of ignoring types.
    5) To understand the reasons behind #1-#4, you need to understand that local variables are passed to other functions in stacks (usually). Without type information, only the function can tell when the function's argument list ends. Variable length argument functions take each 4-byte (or more) value, and they cast them based on what the function says they are.

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, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05: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, 08:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM
Website Security Test