Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

POINTERS AND STRINGS

#include
#include
#include
void main()
{
clrscr();
char *str1="uvce";
char str2[]="uvce";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
printf("
%u",str2);
getch();
}
I'M USING TURBO C++ COMPILER.WHY IS THE OUTPUT COMING OUT TO BE
2 5 5
PLEASE HELP ME OUT.

Comments

  • LundinLundin Posts: 3,711Member
    In the first case you are taking the size of a pointer, not an array. Pointers are 16-bit on Turbo C, since it is an ancient compiler. In the other cases you are taking the size of strings (arrays). All strings surrounded by "" are automatically null-terminated, containing a null character at the end, which gives the size 5.
  • bilderbikkelbilderbikkel Posts: 754Member
    : void main()

    main is of return type int. See references 1-5.

    References

    * 1) C++. International Standard. ISO/IEC 14882. Second edition. Paragraph 3.6.1.2

    * 2) From http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3 : main() must return int. Not void, not bool, not float. int. Just int, nothing but int, only int. Some compilers accept void main(), but that is non-standard and shouldn't be used. Instead use int main().

    * 3) Herb Sutter. Exceptional C++. ISBN: 0-201-61562-2. Item 21: void main() is nonstandard and nonportable.

    * 4) From Bjarne Stroustrup's homepage (http://www.research.att.com/~bs/bs_faq2.html#void-main) :

    The definition

    void main() { /* ... */ }

    is not and never has been C++, nor has it even been C.

    * 5) From the The alt.comp.lang.learn.c-c++ FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html#q3.4: 3.4 Why does everyone make so much fuss about "void main()"?. Because the return type of the main() function must be int in both C and C++. Anything else is undefined. Bottom line - don't try to start a thread about this in alt.comp.lang.learn.c-c++ as it has already been discussed many, many times and generates more flamage than any other topic.
  • LundinLundin Posts: 3,711Member
    Hehe, well he is writing in C. The C standard states that main() shall return int if the program is running in a "hosted environment", such as on Windows / Linux / Mac. If the program is running without a "host OS", then it doesn't make much sense to return anything, and in that case the C standard allows it.

    Personally I always use int main() even if I'm writing low-level hardware stuff, because if I do I can test the code on a Windows compiler. In such programs, the program counter will never reach the end of main, so it really doesn't matter if main returns anything or not.
  • bilderbikkelbilderbikkel Posts: 754Member
    : Hehe, well he is writing in C. The C standard states that main()
    : shall return int if the program is running in a "hosted
    : environment", such as on Windows / Linux / Mac. If the program is
    : running without a "host OS", then it doesn't make much sense to
    : return anything, and in that case the C standard allows it.
    :
    : Personally I always use int main() even if I'm writing low-level
    : hardware stuff, because if I do I can test the code on a Windows
    : compiler. In such programs, the program counter will never reach the
    : end of main, so it really doesn't matter if main returns anything or
    : not.

    Lundin, PLEASE don't argue on this. Are five references to the literature not enough for you? Or do you want to support newbies to incorrectly use void main()?


    bilderbikkel
  • MT2002MT2002 Posts: 1,444Member
    : Lundin, PLEASE don't argue on this. Are five references to the
    : literature not enough for you? Or do you want to support newbies to
    : incorrectly use void main()?

    Lundin is referring to the C standard, not the C++ standard. In any case, C99 section 5.1.2.2.1 displays only two forms on a hosted envirement:

    [code]int main ();
    int main (int argc, char* argv[]);[/code]
    If the program is not running in a hosted envirement (section 5.1.2.1), the entry point is implementation defined, so do not need to follow the above forms (Nor even use main() ). I think this is what Lundin was referring to.

    In any case, the OP is running on a hosted envirement (As correctly pointed out), so he should be using one of the two forms shown above.

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • LundinLundin Posts: 3,711Member
    Correct, I am referring to C not C++.


    ISO 9899:1999 "Programming Languages - C"

    5.1.2.1 Freestanding environment

    "1 In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.

    2 The effect of program termination in a freestanding environment is implementation-defined."

    5.1.2.2 Hosted environment

    "1 A hosted environment need not be provided, but shall conform to the following specifications if present."

    5.1.2.2.1 Program startup

    "1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

    int main(void) { /* ... */ }

    or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

    int main(int argc, char *argv[]) { /* ... */ }

    or equivalent;9) or in some other implementation-defined manner."

    "9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on."


    Annex J, J2 Undefined behavior

    "A program in a hosted environment does not define a function named main using one of the specified forms (5.1.2.2.1)."
  • bilderbikkelbilderbikkel Posts: 754Member
    Hi Lundin,

    I like people quoting the C (or C++) Standard. It shows people take programming serious.

    : "A program in a hosted environment does not define a function named
    : main using one of the specified forms (5.1.2.2.1)."

    But what I still disagree is posting that 'main' can have return type void. Yes, it can, in a hosted environment. But 'int main' is always correct. Please support people teaching newbies to use 'int main'.

    I hope you can contribute to our newbies writing the correct-in-100%-of-the-cases 'int main', instead of defending them writing also-valid-in-1%-of-the-cases 'void main'.

    See ya, Bilderbikkel



  • LundinLundin Posts: 3,711Member
    Embedded systems is probably 90% of the computers in the world... but point taken since this is the beginner forum :-)

    If you are a newbie, you should probably skip this post, always write int main() and don't ponder on why you do it. It is a rather advanced and somewhat pointless topic ;-)
Sign In or Register to comment.