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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by BEN10 View Post
    every function is called by another function.but main is a special case, the OS calls it and in turn expects it to return some value and that value is 0 if the program has successfully terminated and a non-zero value signal abnormal termination.since 0 is taken as int that's why int main(void) is used.
    That is simply not true. There are no defined values for success or error and the OS usually doesn't even care what it returns. It's simply a user-defined value that you return.
    Now, other programs can pick up this value that you return and interpret it in some way. That's what its purpose usually is. What the numbers means is up to the implementor, the coder - ie YOU.
    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.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    @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.

  3. #3
    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.

  4. #4
    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?

  5. #5
    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