1

Possible Duplicate:
Does C support overloading ?

I would like to know if printf supports function overloading in C, since same printf function may be used in more than one way in one C program.

Community
  • 1
  • 1
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199

4 Answers4

7

If you mean the difference between

printf("something: %d\n", number);
printf("something else: %d, %s\n", number, string);

this has nothing to do with overloading. It's just a variadic function

insumity
  • 5,311
  • 8
  • 36
  • 64
3

printf() in C is a variadic function which can be thought of as a form of overloading. Unlike overloaded functions in languages like C++, Java, C# etc., a variadic function is not type-safe which is why they are somewhat frowned upon.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    I _nearly_ want to downvote, variadic-functions and function-overloading are very different things (there is only one printf function, not different implementations depending on parameters). You're skating close to the edge with your "can be thought of as" :) – Binary Worrier Apr 27 '11 at 10:12
  • 2
    @Binary All sorts of things can be thought of as overloading in its most general form. If you want to take overloading as being strictly meaning C++ overloaded functions, then no its different. But just as polymorpism can mean many things, so can overloading. – David Heffernan Apr 27 '11 at 10:14
  • 1
    Agreed, however I can think of a cats and dogs as "Animals" in their most general form, but It won't help my Irish Wolfhound catch mice :) Just saying I think _this case is more specific than general_ . . . and unfortunately no, I don't own an Irish Wolfhound. – Binary Worrier Apr 27 '11 at 10:58
2

If you're asking how printf takes different numbers and types of arguements, then no, it isn't function overloading. Functions can be defined to take variable argument lists.

Look at stdarg.h for working with variable arguement lists

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
1

C doesn't have signature based polymorphism so you can't do this. There are a few ways you can sort of do this listed here.

The more important question is why you would want to do this? Why is printf not generic enough for you?

Community
  • 1
  • 1
Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169