Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
How come I am able to override printf in C language?

Consider the next piece of code:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

int printf(const char *format, ...)
{
    va_list args;

    return 1;
}

int main()
{
    printf("Hello there");

    return 0;
}

And indeed, printf prints nothing. Meaning, the compiler 'prefer' this function over the one declared in <stdio.h>. But C doesn't allow function overriding, and from my understanding, it should throw an error of conflicting errors.

Can anyone explain this behavior?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The C standard says (7.1.3 Reserved identifiers):

  • All identifiers with external linkage in any of the following subclauses (including the future library directions) and errno are always reserved for use as identifiers with external linkage.
  • Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.
  • If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

So when <stdio.h> is included, printf is reserved as an identifier with file scope in the name space of ordinary identifiers. In addition, printf is always reserved as an identifier with external linkage, even if <stdio.h> is not included.

7.1.4 Use of library functions does not allow to define a function with a reserved name.

In most implementations such definition will silently replace the library function, but the standard does not guarantee anything.

over 4 years ago · Santiago Trujillo Report

0

It fails to compile by MSVC.

https://godbolt.org/z/Y1o14jj39

The answer is that on Linux the linker will try to use your local function first, and only then it will search for the function inside system libraries, and that's not an error for various historical reasons.

And yes, printf can be defined inside several libraries, the last loaded library will take a priority. This is a feature! You can override any system call by setting LD_PRELOAD environment variable to the path of your own shared library with that system call.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!