1

In C#, we learned that function overloading occurs when more than one function have the same identifier but different signature.

Although the concept of function overloading is specific to object-oriented languages, is it applicable to the C language as well on the basis of the following observation?

printf("%d", 3);

printf("%d + %d = %d", 1 , 2 , 3 );

The first printf passes only TWO arguments. The second printf passes FOUR arguments.

Does that mean printf is overloaded?

Lundin
  • 195,001
  • 40
  • 254
  • 396
ColorDeColor
  • 181
  • 2
  • 12
  • http://www.cprogramming.com/tutorial/c/lesson17.html – J... Feb 11 '14 at 12:08
  • http://www.geeksforgeeks.org/does-c-support-function-overloading/ – Sudhakar Tillapudi Feb 11 '14 at 12:08
  • 3
    FYI, function overloading is not specific to object oriented languages. Maybe you are thinking of overriding? C++ has function overloading: `void foo(int)` and `void foo(char)` may both exist, and which one gets called is dependent on the type of the argument. This is independent of C++'s OOP facilities, and could have existed in C as well :) – Magnus Hoff Feb 11 '14 at 12:12
  • Please change your title to something more precise. Your question is about overloading and not about OO langues, no? – Jens Gustedt Feb 11 '14 at 12:52
  • C functions can be declared to accept any number of arguments. `printf*` is one of those functions. its declaration will look something like `printf( const char *format, ...)` google C `va_list` macro and `stdarg.h` – Elias Van Ootegem Feb 11 '14 at 14:22

5 Answers5

6

No printf is not overloaded. There is no function overloading or any other object oriented goodness in C. The way printf works is by using argument list. Take a look at this article http://www.cprogramming.com/tutorial/c/lesson17.html

Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59
  • 1
    Function overloading isn't an OO feature. I believe some languages that completely lack OO support have function overloading (Ada?). Furthermore, you are incorrect in stating that there is no OO features in C. C supports several aspects of OO design, such as private encapsulation. See the [C FAQ](http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c) of SO for more information. – Lundin Feb 11 '14 at 12:19
  • 1
    The main mistake people always do when discussing OO is failing to understand that OO is a _way of designing programs_, completely separate from the chosen programming language. It is perfectly possible to write non-OO programs in Java, C++ etc. – Lundin Feb 11 '14 at 12:21
3

The printf family of functions does not use overloading, but rather take a variable number of arguments.

Function overloading and overriding (i.e. virtual methods in C#) are not supported in C, which doesn't even have member functions.

However they can be imitated by means of function pointers. This is the approach taken for instance in the implementation of the X Window System.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • Strictly speaking, C does have something equivalent to member functions, although it is far from elegant. If you implement your "class" as a struct of incomplete (opaque) type, you can have a module with functions that take a pointer to such an incomplete type, and you will have something working in the same manner as public member functions. To emulate private member functions is even easier: merely declare them in the same module as the public functions, but make them static. It is crude, but it has all the properties of an OO class. – Lundin Feb 11 '14 at 12:28
  • We used to call those Abstract Data Types, but I see your point. – Nicola Musatti Feb 11 '14 at 13:01
  • Indeed, but an ADT may or may not be implemented using object-oriented design: it is a broader, more vague term. – Lundin Feb 11 '14 at 13:31
2

Read about variadic functions.

printf and scanf family of functions are variadic functions.

Useful Links:

  1. Cprogramming.com

  2. Writing Variadic Functions

To complete the answer: C does not support Function Overloading.

0xF1
  • 6,046
  • 2
  • 27
  • 50
2

No printf is not an example for function overloading as others already stated, it uses the features from stdarg.h. But seemingly unknown to many, C has some sorts of function overloading.

  • since C99 C has "type generic mathematical functions" in tgmath.h that have you e.g compute float or double sin depending on the argument that you are passing
  • since C11 has _Generic, a feature that is even more powerful than function overloading, and that can be used, among other things, to write function-like macros that implement overloading features

And, all of this has nothing to do with OO programming.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

Function overloading is language feature completely separate from object-oriented design. Just because it tends to exist in languages that have OO features, doesn't make it an OO feature as well.

The corner stones of OO design are: autonomous classes with limited dependences towards the outside world ("loose coupling"), private encapsulation of data/methods and inheritance/polymorphism.

Every other feature that doesn't fit in with the above is just some extra fluff. Function overloading and operator overloading are two such fluffy things - there are many OO languages that don't support those 2 features.

As for printf, it uses the icky variable argument feature of the C language. I wouldn't call that function overloading, but rather some old ad hoc crap that was introduced in the 70s just so that C could brag about having variable number of arguments. A feature which is mildy useful, to say the least. It isn't used in any sane, production-quality code.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 2
    So, to clarify, any use of `printf`, `fprintf` or `snprintf` renders any codebase insane and not production quality? – Magnus Hoff Feb 11 '14 at 12:22
  • @MagnusHoff What I meant is that a code base using user-defined functions with variable arguments isn't sane. It is a completely superfluous feature of the language. As for using stdio functions in production code, I personally think it is acceptable practice as long as you know what you are doing. Not everyone agrees though... to cite a widely-recognized C programming authority: "The input/output library shall not be used in production code." MISRA-C:2004 20.9. – Lundin Feb 11 '14 at 12:34
  • Thank you for elaborating on that :) – Magnus Hoff Feb 11 '14 at 12:38