C + + උදව්වක්

ruwantheekshana

Well-known member
  • Mar 10, 2008
    5,871
    1,317
    113
    C++ වල

    #include
    <iostream>
    int main()

    යන codes වලින් අදහස් කරන්නේ කුමක්ද? ඇයි ඒවා අප හදන සෑම ප්‍රෝග්‍රෑම් එක කම තියෙන්නේ?
     

    sanzilla jackcat

    Well-known member
  • Oct 3, 2008
    6,759
    3,344
    113
    ෆ්ලුජා නගරය
    iostream means the you including the iostream library.

    int main() <= This is the entry point of your application. When you writing a DLL file
    you don't have this entry point. In Windows applications you have WinMain as the
    main.


    You can also change name of the entry point using compiler options.
     

    sanzilla jackcat

    Well-known member
  • Oct 3, 2008
    6,759
    3,344
    113
    ෆ්ලුජා නගරය
    if you asking how the main() is called?And how it works , I can also
    give answers for that.

    There is a C/C++ runtime library called crt0 , typically implemented as a static
    library, and invoked by your compiler when linking and building the
    final executable unless you exceptionally specified[when you
    writing operating system or binary image you could say the compiler
    to specifically not to use it]. So it have a undefined reference to _main [note that
    underscore comes from the naming conventions].
     

    nismok

    Well-known member
  • Jun 27, 2008
    9,377
    672
    113
    #include <iostream> යනු preprocessor directive එකකි. මෙහිදී කියනුයේ iostream නමැති library එක (basic standard input-output library in C++) බාවිතා කරන බවය.

    Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.
    Source file inclusion

    මේ ආකාරයට ඔබට වෙනත් ඕනෑම C++ file එකක් include කරගත හැක.

    Syntax

    #include <filename> (1)
    or
    #include "filename" (2)

    Includes source file, identified by filename into the current source file at the line immediately after the directive.
    The first version of the directive searches only standard include directories. The standard C++ library, as well as standard C library, is implicitly included in standard include directories. The standard include directories can be controlled by the user through compiler options.
    The second version firstly searches the directory where the current file resides and, only if the file is not found, searches the standard include directories.
    In the case the file is not found, program is ill-formed.
     
    Last edited:

    ruwantheekshana

    Well-known member
  • Mar 10, 2008
    5,871
    1,317
    113
    iostream means the you including the iostream library.

    int main() <= This is the entry point of your application. When you writing a DLL file
    you don't have this entry point. In Windows applications you have WinMain as the
    main.


    You can also change name of the entry point using compiler options.

    if you asking how the main() is called?And how it works , I can also
    give answers for that.

    There is a C/C++ runtime library called crt0 , typically implemented as a static
    library, and invoked by your compiler when linking and building the
    final executable unless you exceptionally specified[when you
    writing operating system or binary image you could say the compiler
    to specifically not to use it]. So it have a undefined reference to _main [note that
    underscore comes from the naming conventions].


    ඒ කියන්නේ අපි මෙවා ඕනෑම ප්‍රෝග්‍රෑම් එකක් ලියනකොට මතක තබා ගත යුතු දේවල්? මේවා අපි මතකයේ තියාගත යුතුයි නේද?

     

    ruwantheekshana

    Well-known member
  • Mar 10, 2008
    5,871
    1,317
    113
    #include <iostream>
    using namespace std;
    int main(){
    cout<<"whats ur favrt number"<<endl;

    int iNumber;
    cin>>iNumber;

    cout<<"ur favrt no is"<<iNumber<<endl;
    cin.get();
    return 0;

    }

    දැන් බලපන් මචන් මේ return 0; ඒක මේ ප්‍රෝග්‍රෑම් එකේ තිබ්බත් නැතත් කිසිම වෙනසක් වෙන්නේ නෑ නේ? :eek::eek:
     

    ~v3n0m~

    Member
    Aug 28, 2008
    6,773
    100
    0
    #include <iostream>
    using namespace std;
    int main(){
    cout<<"whats ur favrt number"<<endl;

    int iNumber;
    cin>>iNumber;

    cout<<"ur favrt no is"<<iNumber<<endl;
    cin.get();
    return 0;

    }


    දැන් බලපන් මචන් මේ return 0; ඒක මේ ප්‍රෝග්‍රෑම් එකේ තිබ්බත් නැතත් කිසිම වෙනසක් වෙන්නේ නෑ නේ? :eek::eek:

    thats the exit code. its being returned to the os. to show that the program has been successfully executed. I think you can replace it with EXIT_SUCCESS as well. someone correct me if im wrong.
     

    sanzilla jackcat

    Well-known member
  • Oct 3, 2008
    6,759
    3,344
    113
    ෆ්ලුජා නගරය
    ඒ කියන්නේ අපි මෙවා ඕනෑම ප්‍රෝග්‍රෑම් එකක් ලියනකොට මතක තබා ගත යුතු දේවල්? මේවා අපි මතකයේ තියාගත යුතුයි නේද?


    machan
    Code:
    #include <iostream>

    this means you are including the header file of the iostream library.
    so you can use std::cin and std::cout objects. There are other libraries too
    you could include their header files when you using them.

    Code:
    #include <unistd.h>   // this is a unix header file //
    #include <stdio.h>   // this is the old "C" library. In C++ also you'll need this
    #include <glut.h> // when doing OpenGL programming you will need this.
    
    // and more

    what a header file does is it have a extern declarations to some functions
    or variables. It's just a declaration not a definition. The definition is at a
    binary file [ we call these libraries ,more specifically static libraries].


    And about the main thing , yes it should be keep on your mind.

    There is something called C++ standard. You could have these following two
    forms of the main method.

    Code:
    int main(void);
    
    int main( int argc, char **argv);

    and C++ standard also tells that you can't overload or re-declare
    or use both at once. It's not like the first case , because it's specified
    in the C++ specification.


    If not clear , feel free to ask again.


    --thanks in advance--
     

    sanzilla jackcat

    Well-known member
  • Oct 3, 2008
    6,759
    3,344
    113
    ෆ්ලුජා නගරය
    thats the exit code. its being returned to the os. to show that the program has been successfully executed. I think you can replace it with EXIT_SUCCESS as well. someone correct me if im wrong.

    The C++ specification is that you main method should have a int return type.
    C++ is more strict than the C , so you need to return it. It's an not oky to
    ignore it. But some C++ compilers are even only giving a warning , then it's
    actually violating the C++ standard, which means you better keep in mind
    that return type int should be there and it should be returned. These simple
    things will make the whole codebase into broken mumbo jumbo when it
    grows. So don't practice, if it become a habit , it's not easy to remove it from
    your blood.
     

    Epic Madness

    Banned
    Jan 2, 2012
    1,139
    124
    0
    #include මේකෙන් කරන්නේ header files program එකට import කරගන්න එකයි
    C++ කියන්නේ C මත හදලා තියෙන language එකක් නිසා C වලින් තමයි මේ feature එක එන්නේ.
    මේ header files වල තියෙන්න පුලුවන්(strut,variable declaration, prototype) ඕන එකක් නමුත් ප්‍රධාන වශයෙන් තියෙන්නේ කොහොමද යම් කාර්යයක් කරන්න ඒක simply කරලා අපිට දෙනවා ,හිතන්න iostream ගැන ඒකේ කරන්නේ files system එකට access කරන්න ලේසි කරන්වා එතකොට program එකේ ඉදලා, filesystem එකට(hard drive) ඒවගේම memory එකට , printer මේවගේ resource වලට access කරන්න පුලුවන්. ඒකෙන් දීලා තියෙන keywords cout, cin වගේ use කරලා command කරන්න පුලුවන් මොකක්ද කරන්න ඕනේ කියලා එතකොට program එක මේ commands predefine කරලා තියෙන්නේ මේ headers වල මේ command එක ලැබුනම මොකක්ද කරන්නේ කියලා, etc.....

    <iostream>
    ඉහළ එක බලන්න, <> brackets දාන්න standard libraries වල බලන්නයි මේ files තියෙන්නේ
    compiler එකේ files තියෙන folder එකේ. angel brackets දන්න නැත්නම් ඒක search කරන්ණේ දෙන ලද header file එක මේ program එකේ main .cpp file එක තියෙන directory එකේ.

    int main()
    මේකෙන් තමයි program එකේ ආරම්භය සටහන් කරන්නේ වැඩියේ මොකුත් කරන්නේ නමුත් int දාලා තිබ්බෙත් return කරන්න ඕනේ 0 ඒකෙන් මේ program එකේ code ඉවර වුනාම operating system එකට message එකක් යවනවා මේ program එක වහන්නයි යන්නේ කියලා. එතකොට OS එකට පුලුවන් මේ program එක වෙනුවෙන් allocate කරලා තියෙන memory එකේ පිරිලා තියෙන data clean කරන්න.
     
    • Like
    Reactions: Ritash9001