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

Discussion in 'C' started by sibu, Apr 30, 2007.

  1. sibu

    sibu New Member

    Joined:
    Mar 27, 2005
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  3. sibu

    sibu New Member

    Joined:
    Mar 27, 2005
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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:
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  5. sibu

    sibu New Member

    Joined:
    Mar 27, 2005
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    I think that solves my problem....Thank you very much and a special thanks for your reminder too
     
  6. amjad chitrali

    amjad chitrali New Member

    Joined:
    Feb 10, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi.thanks all people those share and solve my problem.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice