Why is it legal to declare a built-in library function as an identifier name?

This is the declaration of the printf variable:

int printf=90;

This is done without any errors, but if I try to print the identifier value with the given name printf, then it will give an error, so why, although the compiler allows printf to be used as the identifier, we cannot print its value afterwards.

+4
source share
1 answer

Your question is a bit confusing.

C has "scope rules" that determine how a name (for example printf) is resolved from a given point in the code. It is possible and legal in accordance with the rules of the scope to obscure the name from the outer scope.

printf extern -declared <stdio.h>, .

, . , . , , printf , C . ++ ::printf, printf. C .

, printf , :

{
  int (*theprintf)(const char *fmt, ...) = printf;
  int printf = 90;
  theprintf("now printf has the value %d\n", printf);
}

, , extern, , :

{
  extern int printf(const char * fmt, ...);
  printf("hello?\n");
}
+6

Source: https://habr.com/ru/post/1628532/


All Articles