My First C++ endeavour Day 1
So, recently I decided to learn C++ and found a nice tutorial on the basics of C++ so, below, you can see my progress. I will be posting these semi-regularly.
fairly basic I know...
then i did this
in this few lines i told the program to compute 5+1+2=? a being 5 b being 2, then i told it to use a, add one to it, then subtract the result of that to variable b. then i told it to display the result in a simple text line.
so, after a bit, i got onto strings and stuff, and this is an example of string that i typed up
a simple string code, moving on!
thats all for now, this blog will be updated throughout the day with new experiences for me
Code:
// my first program in C++
#include <isostream>;
using namespace std;
int main ()
{
cout << "Hello World!";
cout << "I'm a C++ program!";
cout << "Want to be friends?";
return 0;
}
then i did this
Code:
//basic variable code and stuff
#include <isostream>;
using namespace std;
int main ()
{
int a, b;
int result;
a = 5;
b = 2;
a = a + 1;
result = a - b;
cout << result;
return 0;
}
so, after a bit, i got onto strings and stuff, and this is an example of string that i typed up
Code:
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring = "This is the first string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}
thats all for now, this blog will be updated throughout the day with new experiences for me

Total Comments 14
Comments
-
Posted 02-01-2012 at 08:34 PM by Samuel Edwards
-
Posted 02-01-2012 at 08:41 PM by Scruce
Updated 02-01-2012 at 08:43 PM by Scruce -
Posted 02-01-2012 at 11:51 PM by Izack
-
Posted 02-02-2012 at 12:07 AM by mojoey
-
Posted 02-02-2012 at 03:42 AM by PeriapsisPrograde
-
'This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.'
thats what the tutorial saidPosted 02-02-2012 at 03:55 AM by mojoey
-
Hmm, they're encouraged in Java...I'm not really used to C.
Edit: Yeah, all apps require a main method to run, that's a common convention. I don't think there's anything about them having to return anything, though. I can't think of an obvious reason main() would need to return anything, anyway. As it is, if you declare a method to return an integer [int main()] and it always returns zero, it doesn't need to return anything. void main() means it doesn't return anything, so no return statement is needed. But whatever, it's still best to follow the tutorial.
For what it's worth, all of my Java programs begin with a " public static void main() "Posted 02-02-2012 at 04:00 AM by Izack
Updated 02-02-2012 at 04:06 AM by Izack -
Posted 02-02-2012 at 04:14 AM by mojoey
-
No, the "}" at the end terminates it. the 'return' line means that the function will return whatever is after it, in this case a zero. Usually you'd see some variable being manipulated and returned. Say in your program up there, you could do this:
and then you'd be able to specify somewhere else:Code:int myMethod () { int a, b; int result; a = 5; b = 2; a = a + 1; result = a - b; return result; }
Do you see what I mean here?Code:void main() { int i = myMethod(); cout << i; }Posted 02-02-2012 at 04:25 AM by Izack
-
The "return some_int_value" called from 'main' doesn't directly terminate the process (nor the closing parenthesis of the 'main' function), but returns execution to the CRT code (added by the linker) which called the 'main' function at the beginning, after initializing all the internal C/C++ runtime stuff.Quote:
The returned value is used as of the program, which is passed by CRT to either 'ExitProcess' WinAPI function in Windows, '_exit' kernel call in Linux/Unix, '0x4C' function of '0x21' interrupt in DOS, or other system function that terminates the process. The status code can be then used by calling program or script (batch) to decide what to do next (for example if the called program was non-interactive). Returning 0 usually means that the program finished its execution without errors (where errors are not unhandled exceptions, but program's internal errors, for example like "no argument passed", "file not found", etc).
At least Windows, DOS, and Unix/Linux use integer exit status codes, and the same such argument for program termination functions, so that's why the 'main' is of int type.Posted 02-02-2012 at 06:48 PM by orb
-
I guess some clarifications are needed. First, while 'void main()' may be acceped by some compilers, it's explicitly forbidden by the C standard. Plus, besides the fact that main() may be declared with zero or two arguments (or even more, depending on the platform), it is otherwise like any other function.
Conceptually, the main function is called by the OS when the program is started, thus returning from it is the same as exiting the program (the same as calling exit()). The closing bracked ('}') is simply a token marking the end of the function definition in the source code, and is not the same as returning from it. This may be a bit confusing, since, if the code compiles, the compiler must add a 'return 0' at the end of the function. *
The value returned from the main function is actually used and does have a meaning in some contexts. A 0 means that everything went well, and any other value implies that something went wrong. In some cases, the value is used to tell exactly what made the program fail. xargs, for example, is a command-line utility used in Unix-like systems which returns different values depending on the specific error.
This value is not actually used by the user (unless you're in the command-line or using a debugger), but mostly by other programs or scripts. For example, a script may use it to decide if a command needs to be retried, or even if it can be retried.
In Java, the main function does not return anything since the program implicitly returns 0 when all threads die. This means a Java app automatically quits when main() ends and you did not meddled with multithreading. However, System.exit() may be called directly if you actually need to return an exit code.
Two wiki articles which may be useful: and
* But even if it works, please don't do this
EDIT:
EDIT2: In the first two examples, you typed 'isostream' instead of 'iostream'Posted 02-02-2012 at 07:14 PM by MeDiCS
Updated 02-02-2012 at 07:20 PM by MeDiCS (obs) -
Posted 02-02-2012 at 08:12 PM by Izack
-
Posted 02-02-2012 at 11:04 PM by MeDiCS
-
Posted 02-03-2012 at 01:26 AM by Izack






