Problem with program to print "Hello World" without anything in main()

sibu's Avatar, Join Date: Mar 2005
Go4Expert Member
Hi,

could any one help me out with the follwing problem?

#include <stdio.h>

int i = printf("Hello World");

int main()
{}

When I compile this on gcc (it works with VC++) , it gives an error "initializer element is not constant" where as

#include <stdio.h>

int main()
{
int i = printf("Hello World");
}

displays "Hello World".

I would like to know the reason behind this behaviour?

thanking you

Sibu
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
The first example won't compile with any compliant compiler. If you find it compining in VC++, then you have a very old VC++ or you have errors and warnings disabled.

Aside from that, notice that you have no code in "main". "Main" is the entry and exit point for your program. Nothing going on there.

The return value of "printf" is the number of characters printed, or a negative number if an error occurs. I'm not sure what you're planning to do with that. The integer obviously can't be assigned to until the function is run, so you need a valid invocation, not a global kind of declaration.
0
sibu's Avatar, Join Date: Mar 2005
Go4Expert Member
Thanks for that answer.But still there is a problem

I am using an old compiler and let us forget that.

The first example gives a compilation error as "initializer element is not constant". I assume that it is because of the variant return value of printf function. If that is the case, why is it not a compilation error in the second example? The reason behind my assumption is that, if I give

int i =10;
main()
{}

there is no compilation error.

I do agree that we need a valid invocation and not a global declarion. I just came across this code from our forum. I was just trying to find out the answer to this. But quite confusing.......
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You're not paying attention. In the first example, printf is not actually invoked (called; run); The compiler needs to have a value to assign, and it doesn't. In the second example, the compiler recognizes that printf will be called at run time, and a value will be available.

You need to understand the build process that results in an executable file. First, a source file is preprocessed. During this phase include files are copied and pasted directly into the source file.

This new souce file is then submitted to the compiler. Certain syntactical requirements must be met for successful compilation. Not all requirements for an executable need to be present at this time; the compiler need only be assured that they will be present, in correct form, when needed.

Thirdly, the linker takes over. At this time all necessary code must be present, either as code produced by the compiler, or as distinctly specified libraries or other object files.

In some instances (embedded systems or systems without an OS or memory management), there may be a fourth step: the locator. The locator determines the final memory address for executables that aren't dynamically relocatable.

You need to refer to some books or tutorials. A forum is a place to get help with code you're having problems with, a place to expand your learning. It isn't an effective place to learn basics.
0
sibu's Avatar, Join Date: Mar 2005
Go4Expert Member
I think that solves my problem....Thank you very much and a special thanks for your reminder too
0
amjad chitrali's Avatar, Join Date: Feb 2011
Newbie Member
hi.thanks all people those share and solve my problem.