curious
curious

Reputation: 91

pre-defined function called before main

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

Upvotes: 4

Views: 376

Answers (6)

Able Johnson
Able Johnson

Reputation: 571

Gcc compiler compiles the source file in a procedural manner.

Use g++ or c++ compiler.The code will work perfectly

I compiled the code using g++ compiler and compilation was successfull.

#include<stdio.h>
int mysqrt(int n)
{
return(n);
}
int a=mysqrt(10);

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

Please ignore the internal logic of mysqrt function.It is just for testing purpose.

From my knowledge it is a problem with gcc compiler.

Clause 6.6, paragraph 3 of the standard says

Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated.

that a constant expression must not contain a function-call that is evaluated.

That is because

A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.

The gcc compiler is able to evaluate a constant express by executing a library function at compile time.But it is unable to evaluate a constant express by executing a user defined function at compile time.But the g++ and c++ compilers are able to do this job.

Upvotes: 1

balaji
balaji

Reputation: 81

Try to define the function in an another file and and include the file here it will.work . Or just simply declare the function before intialization

Upvotes: 0

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27652

In C, an initializer to a variable with static storage duration (which means that it's there during the whole life time of the program, such as your x) must be a constant expression. A call to a function is not a constant expression, and you'll get a compiler error such as (from GCC) error: initializer element is not constant. The standard also says that "An implementation may accept other forms of constant expressions", so it is allowed for the compiler to accept a call to a library function such as sqrt as a constant expression. The compiler knows what sqrt will do, and can even evaluate it during compilation and replace the function call with its result.

C++ has different rules, and there you're allowed to call functions, even your own functions, when initializing x.

Upvotes: 5

jparthj
jparthj

Reputation: 1646

The error you are getting is caused by the reason Luchian explained above.

To be precise with your problem, you need to understand Preprocessor Directives.

Preprocessor directives are lines included in the code of programs preceded by a hash sign (#). These lines are not program statements but directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements.

You can simply define x before entering main function by:

 #define x BLAH

I think this will clear bit of your thoughts. Cheerss!

Upvotes: 0

KARTHIK BHAT
KARTHIK BHAT

Reputation: 1420

It saying that one function by the name sqrt() is defined else already (math.h has its declaration)

when you add the math.h header file it contains the declaration for sqrt() function which is in library which will be added while linking.

when you try to define your own function by the same name compiler gets confused on which function to call (your's or the one already declared in math.h)

Change your function name to my_sqrt() it should work fine when you define and call.

Note: try to learn about comiplation process. what happens at each stage you will get a better picture of what's happpening.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

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.

Upvotes: 8

Related Questions