Thread: Can someone help explain these basic consepts to me please?

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by BEN10 View Post
    plz be definite.is return 1 according to the standards or not? i just wanna know it although i'll use return 0.
    no - return value is application defined. your Helloworld program could decide just to return return value of printf

    in this case 0 will be failure, while non-negative value indicating success...


    But more general situation is when program indicates successful end with 0 and error with negative value... in most cases it will be specified in the man pages for tools where the return value could be of interest for the caller
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    @Elysia
    it means we can use any value as return value.for eg there's no problem in return 1,return 2 etc but it has to be int or it can be float,char also.
    @matsp
    can you give me some link where it is explained in detail.
    I don't know of any document that describes the "pre-main" startup, but I have worked at [debugged and modified] the "before main" section of code more than once, so I have some idea of how it works. You could try looking at the source code for your C library startup code, if you like - run in the debugger and check out the call-stack at the start of main, and check where that came from.

    The return type from main is an int because the C standard says so. If you use something else, the C runtime code that calls main is STILL going to look at the place where integers are returned and expect to find an integer. If you happen to pass a float, then it's likely that your actual value is a "random" number (either e.g. 0x3F800000 instead of 1.0 if int and float are passed in the same register, or whatever happens to be in the register used to return integers, so completely unpredictable from reading the code, but not reliably random in the sense you could use it for random number generation) - same applies to void. If it's char, then you are likely to have random content in the bits covering the upper bits beyond the size of char - of course, that DOES depend on how the value was loaded into the return value register. In some architectures, there is no way to load a byte into a register, in which case it may appear to work. If you then change to another processor architecture, that code will break! That's the annoying bit about "undefined behaviour" - it sometimes appear to work perfectly fine, only to fail when something changes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by matsp View Post
    I don't know of any document that describes the "pre-main" startup, but I have worked at [debugged and modified] the "before main" section of code more than once, so I have some idea of how it works. You could try looking at the source code for your C library startup code, if you like - run in the debugger and check out the call-stack at the start of main, and check where that came from.

    Mats
    i'm using visual studio 2005.how can i look at the source code for C library startup code in that?

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Set a breakpoint in main, then use the callstack to go the startup code and place a breakpoint there. Now restart the debugger.
    Of course, if you don't want to step through it, then you don't need the final breakpoint or restart the debugger.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    i'm using visual studio 2005.how can i look at the source code for C library startup code in that?
    Depending on which distribution of Visual Studio it is, you may not have the source code for the C library startup code. If it's a commercial version (not Express Edition), then it probably does have the C library source code. If so, set a breakpoint at the start of main in a project, run in the debugger, and just look at the call-stack (bottom right corner of the screen in the debugger if you haven't moved your debug windows around).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Basic 3D gfx tutorial?
    By ichijoji in forum Game Programming
    Replies: 6
    Last Post: 05-02-2003, 08:21 PM
  3. Basic windows programming
    By Remedy in forum C Programming
    Replies: 3
    Last Post: 04-22-2003, 11:04 PM
  4. Visual Basic
    By The15th in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-08-2001, 01:50 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM