Results 1 to 13 of 13
  1. #1
    djzikou's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Sfax
    Posts
    12
    Reputation
    10
    Thanks
    4
    My Mood
    Devilish

    Lightbulb C++ Basics For Beginners - Structure of a program

    The best way to learn a programming language is by writing programs. Typically, the first program beginners write is a program called "Hello World", which simply prints "Hello World" to your computer screen. Although it is very simple, it contains all the fundamental components C++ programs have:

    Code:
    // my first program in C++
    #include <iostream>
    
    int main()
    {
      std::cout << "Hello World!";
    }
    Hello World!

    The left panel above shows the C++ code for this program. The right panel shows the result when the program is executed by a computer. The grey numbers to the left of the panels are line numbers to make discussing programs and researching errors easier. They are not part of the program.

    Let's examine this program line by line:

    Line 1: // my first program in C++
    Lines beginning with two slash signs (//) are comments by the programmer and have no effect on the behavior of the program. Programmers use them to include short explanations or observations concerning the code or program. In this case, it is a brief introductory description of the program.

    Line 2: #include <iostream>
    Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In this case, the directive #include <iostream>, instructs the preprocessor to include a section of standard C++ code, known as header iostream, that allows to perform standard input and output operations, such as writing the output of this program (Hello World) to the screen.

    Line 3: A blank line.
    Blank lines have no effect on a program. They simply improve readability.

    Line 4: int main ()
    This line initiates the declaration of a function. Essentially, a function is a group of code statements which are given a name: in this case, this gives the name "main" to the group of code statements that follow. Functions will be discussed in detail in a later chapter, but essentially, their definition is introduced with a succession of a type (int), a name (main) and a pair of parentheses (()), optionally including parameters.

    The function named main is a special function in all C++ programs; it is the function called when the program is run. The execution of all C++ programs begins with the main function regardless of where the function is actually located within the code.

    Lines 5 and 7: { and }
    The open brace ({) at line 5 indicates the beginning of main's function definition, and the closing brace (}) at line 7, indicates its end. Everything between these braces is the function's body that defines what happens when main is called. All functions use braces to indicate the beginning and end of their definitions.

    Line 6: std::cout << "Hello World!";
    This line is a C++ statement. A statement is an expression that can actually produce some effect. It is the meat of a program, specifying its actual behavior. Statements are executed in the same order that they appear within a function's body.

    This statement has three parts: First, std::cout, which identifies the standard character output device (usually, this is the computer screen). Second, the insertion operator (<<), which indicates that what follows is inserted into std::cout. Finally, a sentence within quotes ("Hello world!"), is the content inserted into the standard output.

    Notice that the statement ends with a semicolon (;). This character marks the end of the statement, just as the period ends a sentence in English. All C++ statements must end with a semicolon character. One of the most common syntax errors in C++ is forgetting to end a statement with a semicolon.

    You may have noticed that not all the lines of this program perform actions when the code is executed. There is a line containing a comment (beginning with //). There is a line with a directive for the preprocessor (beginning with #). There is a line that defines a function (in this case, the main function). And, finally, a line with a statements ending with a semicolon (the insertion into cout), which was within the block delimited by the braces ( { } ) of the main function.

    The program has been structured in different lines and properly indented, in order to make it easier to understand for the humans reading it. But C++ does not have strict rules on indentation or on how to split instructions in different lines. For example, instead of


    Code:
    int main ()
    {
      std::cout << " Hello World!";
    }
    We could have written:

    Code:
    int main () { std::cout << "Hello World!"; }
    all in a single line, and this would have had exactly the same meaning as the preceding code.

    In C++, the separation between statements is specified with an ending semicolon (;), with the separation into different lines not mattering at all for this purpose. Many statements can be written in a single line, or each statement can be in its own line. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it, but has no effect on the actual behavior of the program.

    Now, let's add an additional statement to our first program:


    Code:
    // my second program in C++
    #include <iostream>
    
    int main ()
    {
      std::cout << "Hello World! ";
      std::cout << "I'm a C++ program";
    }
    Hello World! I'm a C++ program

    In this case, the program performed two insertions into std::cout in two different statements. Once again, the separation in different lines of code simply gives greater readability to the program, since main could have been perfectly valid defined in this way:

    Code:
    int main () { std::cout << " Hello World! "; std::cout << " I'm a C++ program "; }
    The source code could have also been divided into more code lines instead:

    Code:
    int main ()
    {
      std::cout <<
        "Hello World!";
      std::cout
        << "I'm a C++ program";
    }
    And the result would again have been exactly the same as in the previous examples.

    Preprocessor directives (those that begin by #) are out of this general rule since they are not statements. They are lines read and processed by the preprocessor before proper compilation begins. Preprocessor directives must be specified in their own line and, because they are not statements, do not have to end with a semicolon (;).




    • Comments


    As noted above, comments do not affect the operation of the program; however, they provide an important tool to document directly within the source code what the program does and how it operates.

    C++ supports two ways of commenting code:


    Code:
    // line comment
    /* block comment */
    The first of them, known as line comment, discards everything from where the pair of slash signs (//) are found up to the end of that same line. The second one, known as block comment, discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including multiple lines.

    Let's add comments to our second program:


    Code:
    /* my second program in C++
       with more comments */
    
    #include <iostream>
    
    int main ()
    {
      std::cout << "Hello World! ";     // prints Hello World!
      std::cout << "I'm a C++ program"; // prints I'm a C++ program
    }
    Hello World! I'm a C++ program

    If comments are included within the source code of a program without using the comment characters combinations //, /* or */, the compiler takes them as if they were C++ expressions, most likely causing the compilation to fail with one, or several, error messages.




    • Using namespace std


    If you have seen C++ code before, you may have seen cout being used instead of std::cout. Both name the same object: the first one uses its unqualified name (cout), while the second qualifies it directly within the namespace std (as std::cout).

    cout is part of the standard library, and all the elements in the standard C++ library are declared within what is a called a namespace: the namespace std.

    In order to refer to the elements in the std namespace a program shall either qualify each and every use of elements of the library (as we have done by prefixing cout with std::), or introduce visibility of its components. The most typical way to introduce visibility of these components is by means of using declarations:


    Code:
     
    using namespace std;
    The above declaration allows all elements in the std namespace to be accessed in an unqualified manner (without the std:: prefix).

    With this in mind, the last example can be rewritten to make unqualified uses of cout as:


    Code:
    // my second program in C++
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      cout << "Hello World! ";
      cout << "I'm a C++ program";
    }
    Hello World! I'm a C++ program

    Both ways of accessing the elements of the std namespace (explicit qualification and using declarations) are valid in C++ and produce the exact same behavior. For simplicity, and to improve readability, the examples in these tutorials will more often use this latter approach with using declarations, although note that explicit qualification is the only way to guarantee that name collisions never happen.

    Namespaces are explained in more detail in a later lesson.

  2. The Following 2 Users Say Thank You to djzikou For This Useful Post:

    Misery Jr. (02-26-2014),Ticherhaz (02-26-2014)

  3. #2
    Ticherhaz's Avatar
    Join Date
    Aug 2013
    Gender
    male
    Location
    Malaysia
    Posts
    6,564
    Reputation
    1376
    Thanks
    1,297
    My Mood
    Inspired
    this is the 1st part right? need more
    Anything can PM me. I'm from Malaysia.

  4. #3
    djzikou's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Sfax
    Posts
    12
    Reputation
    10
    Thanks
    4
    My Mood
    Devilish
    Quote Originally Posted by Zuhrain View Post
    this is the 1st part right? need more
    This is the second lesson, i'm going to do more of course to cover all the basics of c++ so that we can move to some intermediate to advanced hot stuff

  5. #4
    Ticherhaz's Avatar
    Join Date
    Aug 2013
    Gender
    male
    Location
    Malaysia
    Posts
    6,564
    Reputation
    1376
    Thanks
    1,297
    My Mood
    Inspired
    Quote Originally Posted by djzikou View Post
    This is the second lesson, i'm going to do more of course to cover all the basics of c++ so that we can move to some intermediate to advanced hot stuff
    yes! indeed!
    Anything can PM me. I'm from Malaysia.

  6. #5
    Misery Jr.'s Avatar
    Join Date
    Aug 2012
    Gender
    male
    Location
    Teamspeak
    Posts
    1,215
    Reputation
    247
    Thanks
    267
    My Mood
    Happy
    Thanks for the tutorial!

  7. #6
    Ticherhaz's Avatar
    Join Date
    Aug 2013
    Gender
    male
    Location
    Malaysia
    Posts
    6,564
    Reputation
    1376
    Thanks
    1,297
    My Mood
    Inspired
    Quote Originally Posted by djzikou View Post
    This is the second lesson, i'm going to do more of course to cover all the basics of c++ so that we can move to some intermediate to advanced hot stuff
    wait the minute, we must use what program?
    Anything can PM me. I'm from Malaysia.

  8. #7
    djzikou's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Sfax
    Posts
    12
    Reputation
    10
    Thanks
    4
    My Mood
    Devilish
    Quote Originally Posted by Zuhrain View Post
    wait the minute, we must use what program?
    See the introduction to the tutorial : C++ Basics For Beginners - Tools we need "Compilers"

  9. #8
    Ticherhaz's Avatar
    Join Date
    Aug 2013
    Gender
    male
    Location
    Malaysia
    Posts
    6,564
    Reputation
    1376
    Thanks
    1,297
    My Mood
    Inspired
    Quote Originally Posted by djzikou View Post
    See the introduction to the tutorial : C++ Basics For Beginners - Tools we need "Compilers"
    yeah.. I'm newbie.. what is compilers??
    Anything can PM me. I'm from Malaysia.

  10. #9
    djzikou's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Sfax
    Posts
    12
    Reputation
    10
    Thanks
    4
    My Mood
    Devilish
    Quote Originally Posted by Zuhrain View Post
    yeah.. I'm newbie.. what is compilers??
    it's the thread before that one prefix : "Tutorial" title : "C++ Basics For Beginners - Tools we need "Compilers" "

  11. #10
    djzikou's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Sfax
    Posts
    12
    Reputation
    10
    Thanks
    4
    My Mood
    Devilish
    Quote Originally Posted by Jacareverde View Post
    Thanks for the tutorial!
    You're welcome wish it helps

  12. #11
    Harava's Avatar
    Join Date
    Sep 2013
    Gender
    male
    Posts
    114
    Reputation
    10
    Thanks
    2,989
    Just a sidenote, though main() (or WinMain) is called every time a program starts, its actually not the entry point function on windows.
    mainCRTStartup and WinMainCRTStartup are the real entry functions on windows. This is because of the standard C library msvc ships with.

    Code:
    int main()
    {
    
        return 0;
    }
    The above code will generate a 31KB PE with msvc. This is because the compiler will cram the C runtime in the application.
    Now here is how you make main() the real entry point and tell msvc to forget about the standard library:

    Code:
    #pragma comment(linker, "/DEFAULTLIB:kernel32.lib")
    #pragma comment(linker, "/ENTRY:main")
    
    #include <windows.h>
    
    char Hello[] = "Hello there!\n";
    DWORD dwChars;
    
    int main()
    {
        HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
        HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
        WriteConsoleA(hConOut, Hello, sizeof(Hello)-1, &dwChars, NULL);
        ReadConsoleA(hConIn, Hello, 1, &dwChars, NULL);
        return 0;
    }
    Above code resulted in a 3KB executable, much better. I used kernel32 as the defaultlib, because all the functions for my little hello world are in there. To have no library at all, you can pass "/NODEFAULTLIB" to the linker. Keep in mind though that the C runtime is not usable now. So no printf, no malloc and so on.
    Recent releases:
    CSPHv3.2




    Code:
    00F38C0E     B8 0610F300    MOV EAX, 00F31006
    00F38C13     C700 208CF300  MOV DWORD PTR DS:[EAX], 00F38C20
    00F38C19     EB FF          JMP SHORT 00F38C1A
    00F38C1B     90             NOP
    00F38C1C     0000           ADD BYTE PTR DS:[EAX],AL
    00F38C1E     0000           ADD BYTE PTR DS:[EAX],AL
    00F38C20     58             POP EAX
    00F38C21    ^EB EB          JMP SHORT 00F38C0E
    Can't see me calling, you hatin'?

  13. #12
    djzikou's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Sfax
    Posts
    12
    Reputation
    10
    Thanks
    4
    My Mood
    Devilish
    Quote Originally Posted by Harava View Post
    Just a sidenote, though main() (or WinMain) is called every time a program starts, its actually not the entry point function on windows.
    mainCRTStartup and WinMainCRTStartup are the real entry functions on windows. This is because of the standard C library msvc ships with.

    Code:
    int main()
    {
    
        return 0;
    }
    The above code will generate a 31KB PE with msvc. This is because the compiler will cram the C runtime in the application.
    Now here is how you make main() the real entry point and tell msvc to forget about the standard library:

    Code:
    #pragma comment(linker, "/DEFAULTLIB:kernel32.lib")
    #pragma comment(linker, "/ENTRY:main")
    
    #include <windows.h>
    
    char Hello[] = "Hello there!\n";
    DWORD dwChars;
    
    int main()
    {
        HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
        HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
        WriteConsoleA(hConOut, Hello, sizeof(Hello)-1, &dwChars, NULL);
        ReadConsoleA(hConIn, Hello, 1, &dwChars, NULL);
        return 0;
    }
    Above code resulted in a 3KB executable, much better. I used kernel32 as the defaultlib, because all the functions for my little hello world are in there. To have no library at all, you can pass "/NODEFAULTLIB" to the linker. Keep in mind though that the C runtime is not usable now. So no printf, no malloc and so on.
    I understand your point of view and i agree with you on that, but take a look at the first script and the second script and tell me which one is easier to remember especially that i adress beginners!!

  14. #13
    resonation's Avatar
    Join Date
    Dec 2014
    Gender
    male
    Location
    LA
    Posts
    14
    Reputation
    10
    Thanks
    1
    The best code is always clean code. If my spaghetti code takes 10 seconds to run and your clean code takes 12, IMO yours is better.

Similar Threads

  1. [Tutorial] C++ Basics For Beginners - Tools we need "Compilers"
    By djzikou in forum C++/C Programming
    Replies: 0
    Last Post: 02-26-2014, 10:33 PM
  2. programming for beginners
    By yosimba2000 in forum Programming Tutorials
    Replies: 9
    Last Post: 05-21-2011, 06:54 PM
  3. [ For Win XP 32 bit ] Program to update the structures/class?
    By ASM in forum Combat Arms Coding Help & Discussion
    Replies: 3
    Last Post: 05-14-2011, 09:45 PM
  4. best c++ tutorial for beginners!
    By WacKer in forum C++/C Programming
    Replies: 31
    Last Post: 12-05-2009, 06:30 AM
  5. Replies: 3
    Last Post: 01-28-2009, 09:16 AM

Tags for this Thread