5 Replies - 1484 Views - Last Post: 22 December 2010 - 10:35 AM Rate Topic: -----

#1 KG3UNFORGN   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 19-October 10

Question about the int main

Posted 01 November 2010 - 01:49 PM

Hey it it's KG3

I have a question, about int main.

When a person does int main in the start of the code after the header, since int is a datatype and main i know it is a key word so it will not work as a var, but how come you have to type in int again.Does the program knows alredy that the var is a int datatype? I am new to programming, and none of the books i am reading are explaining why.
here is what i am talking about, i dont have a complier on me so i am gonna just type it out.
#include "iostream"
using namespace std;

        int main() { //the datatype is already right here in this statment

              int n;  //why do you need to type in this dataype?



Is This A Good Question/Topic? 0
  • +

Replies To: Question about the int main

#2 McSick   User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Question about the int main

Posted 01 November 2010 - 02:01 PM

View PostKG3UNFORGN, on 01 November 2010 - 12:49 PM, said:

Hey it it's KG3

I have a question, about int main.

When a person does int main in the start of the code after the header, since int is a datatype and main i know it is a key word so it will not work as a var, but how come you have to type in int again.Does the program knows alredy that the var is a int datatype? I am new to programming, and none of the books i am reading are explaining why.
here is what i am talking about, i dont have a complier on me so i am gonna just type it out.
#include "iostream"
using namespace std;

        int main() { //the datatype is already right here in this statment

              int n;  //why do you need to type in this dataype?



n is a random variable that will be used in main. n does not have to be int, it could be any data type such float, double, ect. ect. int before main is what the main function returns which is just an interger.
Was This Post Helpful? 1
  • +
  • -

#3 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Question about the int main

Posted 01 November 2010 - 02:01 PM

i have no clue what your thinking but i can explain. 'int' before 'main' tells the compiler the "return type". this way there are no type errors when a value is returned from a function. the 'int' before 'n' tells the compiler what type 'n' is so that there are no type errors concerning 'n'. take this for instance
char str[100];
int n=30;
str = str + n - "my name is jake";


dose this make sense? it shouldn't, how can i add 30 to a string and subtract "my name is jake" from what ever that is? the compiler would recognize that these operator's do not exist for string and an integer. this is where your compiler tells you you have some type error or something like "'+' operator dose not have overload operator+(char*,int)". it allows the compiler to make sure that you don't make silly mistakes like that.
Was This Post Helpful? 1
  • +
  • -

#4 ZOMBIE!!!   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 211
  • Joined: 28-October 09

Re: Question about the int main

Posted 01 November 2010 - 02:02 PM

The int in front of main is a return value. You learn about these when you get into functions. The int in front of the n is declaring that n is a variable, of int type. Int stands for integer and it is used for storing numbers.

so here is a little program to show you:
#include <iostream>
using namespace std;

int main()//The computer knows this is the start of the program, the int is your return value
{
    
    int Response = 0;// This makes an integer equal to 0, meant for storing numbers
    cout << "Enter a number: \n";
    cin >> Response; // storing your number into Response

    cout << "\n" << Response; // the "\n" is an endline character which basically moves to the next line, then it shows your response

    return 0; //this is where the int before main comes in to play. This exits the program
}



Of course this program doesn't have error handling, so don't enter things like characters, it is just for demonstrational purposes.

Hope that helps,
ZOMBIE!!!

*EDIT*
In the time it took for me to type this up there were some replies, sorry for any redundancy

This post has been edited by ZOMBIE!!!: 01 November 2010 - 02:03 PM

Was This Post Helpful? 1
  • +
  • -

#5 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Question about the int main

Posted 01 November 2010 - 02:48 PM

To put it simply, the function return type only specifies what the function should return, in this case, an integer, more specifically, the main should return 0 for success, other value for failure. You can define whatever you want within the function itself (within reason).

take a look
// generic function layout
return type  function name  ( parameter list )
{
    // function body
}

// generic variable declaration
data type  variable name 

This post has been edited by Aphex19: 01 November 2010 - 02:49 PM

Was This Post Helpful? 1
  • +
  • -

#6 KG3UNFORGN   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 19-October 10

Re: Question about the int main

Posted 22 December 2010 - 10:35 AM

Thanks guys for clearing that up for me....sorry for the very late reply
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1