// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
This short code is simply printing out the "hello world"
so why is it using the data type int besides the main? isn't string more suitable in this case?
and what does the () mean ? i also saw [] in codes. what exactly is the purpose of it?
sorry, i just want to clarify to have a better understanding. Thanks.
I'd recommend you read through a tutorial, like the one on this site. http://www.cplusplus.com/doc/tutorial/
You will cover the meaning of the parentheses (which are the function argument list - in this case, there are no arguments to the main function) and the array subscript operator (the []) as you work through learning the syntax of C++.
You can't expect to understand everything when you have just started to learn a programming language.
The program starts executing from the 'main' function. The 'main' function has to be defined as
int main()
There are some more options possible here, but that should not be of concern to you at this stage. For now, just remember to write 'int main()', at least until you are comfortable with functions.
Various operators like () and [] will certainly be explained in any tutorial. You have to follow the tutorial in order, don't skip lessons, and eventually you will find out what those operators do.
main is a method , int is the return type of that function . So you are missing at the end of the method a "return 0;" . The "()" is the parameter of the function , that means that if we call this function from another place in the code , we can send it some information to threat. Usually the parameters of the main function are (int argc , char * argv[]) , so we can parse arguments from outside the program when we launch it . The {} is the scope of the function , that is where you write the code . The [] is an operator for arrays to access an element or can be overloaded by a class to access a certain data encapsulated , but it also tells the compiler the size of a c-array , like this :
int a[10]; , means I have reserved space in memory aligned for 10 integers.