Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use main, printf,scanf for naming identifiers?

Tags:

c

int main()
{
  int main=5;
  printf("%d",main);
  return 0;
}

In this case there is no error and the gcc compiler prints 5. But if I write

 int main()
    {
        int printf=5;
        printf("%d",printf);
        return 0;
    }

the compiler shows an error..why ?

like image 766
Parikshita Avatar asked Dec 09 '22 13:12

Parikshita


1 Answers

In your 1st example you "hide" the main function, replacing it with an int object.

In your 2nd example you "hide" the printf function, replacing it with an int object. Trying to call an int is illegal.

5("foo"); /* illegal to call "function" 5 */
like image 82
pmg Avatar answered Dec 15 '22 00:12

pmg