Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pre-defined function called before main

Tags:

c++

c

I don't understand how and when the call to this pre-defined function sqrt() is made, also in case if i define my own function sqrt() it shows a compilation error, so why a pre-defined function call works and call to user-defined function fails although both code resides in the (TEXT) section of my executable.

#include<stdio.h>

int x = sqrt(16); 

int main()
{

     printf(" x =  %d\n",x);
     return 0;
}

OUTPUT:

x = 4;

When i am calling sqrt() function defined by me i am getting following error but the same error does'nt show up when i am using a pre-defined function

ERROR : initializer element is not constant

like image 727
curious Avatar asked Dec 09 '22 09:12

curious


1 Answers

If you define your own sqrt function, it will clash with the one already defined in math.h, ergo the error.

The call is made because globals (or, rather, namespace scope variables) are initialized before entry to main - the initialization of x that is.

like image 95
Luchian Grigore Avatar answered Dec 20 '22 03:12

Luchian Grigore