Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Problem with program to print "Hello World" without anything in main() (http://www.go4expert.com/forums/program-print-hello-world-main-t4080/)

sibu 30Apr2007 15:53

Problem with program to print "Hello World" without anything in main()
 
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

DaWei 30Apr2007 17:18

Re: Problem with program to print "Hello World" without anything in main()
 
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.

sibu 30Apr2007 17:35

Re: Problem with program to print "Hello World" without anything in main()
 
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....... :confused:

DaWei 30Apr2007 18:22

Re: Problem with program to print "Hello World" without anything in main()
 
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.

sibu 30Apr2007 19:23

Re: Problem with program to print "Hello World" without anything in main()
 
I think that solves my problem....Thank you very much and a special thanks for your reminder too

amjad chitrali 10Feb2011 17:03

Re: Problem with program to print "Hello World" without anything in main()
 
hi.thanks all people those share and solve my problem.


All times are GMT +5.5. The time now is 19:43.