Jump to content

int main() or void main().!

- - - - -

  • This topic is locked This topic is locked
16 replies to this topic

#1
UbuntuX

UbuntuX

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
alright some people wonder what's the different between these two ,syntax


int main()

{


return x;

}


void main()

{


return ;

}

the int main() or void main() part is known as function header,
and the int or void represents as return value,if that's int ,it means the function returns integer type of value ,else if it's void ,that is not return any value ,so you can just ignore the return keyword at end of use alone.

and the main part is known the function name and variables,pointers,arrays,or anything inside the parenthesis are known as parameters so you pass arguments to those parameters.

consider the main part ,that's the main function of the cpp any file and whenever the compiler compiles anything ,it finds that first also the software run from the main function,it doesn't matter how much of codes put above it ,when you return something to main function ,it will pass to the OS.!
and if that's 0 means the program return a signal to tell OS ,the program stopped,so that's how the OS knows the program stops or not.

you can also use give commands from outside to the program by using parameters of main function

int main(int size, char* pName[])

{

cout << pName << endl;

system("pause");

return 0;

}

so the size variable is the size of pointer char variable's string ,remember the pName is a pointer char ,it means you need to declare it as an array also in this case the pointer can directly take a string and IT WILL NOT TAKE AN ADDRESS.!!

now in DOS prompt type any string with the program's name so you can see it in the program.
example
C:\ProgramName.exe "Code Call"
remember the SIZE Is not the number of characters.!!
IT'S NUMBER OF WORDS IN THE STRING THAT U TYPE.!!! :w00t:
IF U TYPE ANYTHING WITHIN DOUBLE QUOTATIONS THEN THAT WILL ALSO BE CONSIDERED AS
ONE WORD
example
"code call" / codecall ==> 1
"code call" forum / codecall forum =>2

Edited by UbuntuX, 08 August 2010 - 07:32 AM.


#2
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
If I am not mistaken, void main() has been officially declared a bad habit, and a wrong syntax: it was never in C++, nor C.

Edited by jwxie518, 07 August 2010 - 02:11 PM.


#3
ferovac

ferovac

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
now here is a thread that should be deleted and never spoken of again

and its two stars next to the word char

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

UbuntuX said:

so the size variable is the size of pointer char variable's string ,remember the Array is a pointer char ,it means you don't need to declare it as an array also in this case the pointer can directly take a string and IT WILL NOT TAKE AN ADDRESS.!!

I never heard of Array type in C.

In C++, you main must always be int and return 0 on success, but I think you can omit return statement.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript

Quote

alright some people wonder what's the different between these two ,syntax

int main()
{

return x;
}

void main()
{

return ;
}
the int main() or void main() part is known as function header,
and the int or void represents as return value,if that's int ,it means the function returns integer type of value ,else if it's void ,that is not return any value ,so you can just ignore the return keyword at end of use alone.
void main() is an invention of the Devil. See below.

Quote

and the main part is known the function name and variables,pointers,arrays,or anything inside the parenthesis are known as parameters so you pass arguments to those parameters.
Correct.

Quote

consider the main part ,that's the main function of the cpp any file and whenever the compiler compiles anything ,it finds that first also the software run from the main function,it doesn't matter how much of codes put above it ,when you return something to main function ,it will pass to the OS.!
Not quite. The OS actually calls the C runtime library, which then calls your program. That's how you get the argc and argv variables. (By the way, it's char **argv. An array of strings, not just one string.) This is why you shouldn't declare your main's return type as void. The runtime library is expecting an integer return value. If it gets nothing...boom. Some compilers let this slide by and secretly modify your program to return 0 behind your back. Don't rely on this.

Quote

and if that's 0 means the program return a signal to tell OS ,the program stopped,so that's how the OS knows the program stops or not.
No. It's the same as returning an int from a function. The OS knows when the program stops; all the return value does is tell the OS and any programs calling your program what you're returning. For example, most programs return 0 on success. Other values usually indicate an error, and the number of the error gives the specific information. A 1 could be "File not found," 2 could be "Access denied," and so on. Whatever value you return is up to you.

Quote

you can also use give commands from outside to the program by using parameters of main function

int main(int size, char* Array)
{
return 0;
}
You don't pass them directly to your program. You hand them off to the C runtime library, which does some parsing, splits up the argument strings, sets argc to the number of arguments you have, and argv to point to an array of strings containing your arguments. It's not one string as you've indicated in your code. An array of strings.

Quote

so the size variable is the size of pointer char variable's string ,remember the Array is a pointer char ,it means you don't need to declare it as an array also in this case
No. You've declared the variable names against standard convention. It's typically
int main(int argc, char **argv)

Quote

the pointer can directly take a string and IT WILL NOT TAKE AN ADDRESS.!!
Noooo. When you do something like:
char *blah = "Hello World!";
What you're actually doing is setting blah to point to a region of memory that contains the string "Hello World!"

Quote

now in DOS prompt type any string with the program's name so you can see it in the program.
Um...sure...
sudo rm -rf /

#6
UbuntuX

UbuntuX

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
^^
you just ruined my thread by putting craps.
all you have done is putting bla bla bla :P
it's boring to read infact.
:sleep:
I didn't expect judgments ,if you know how to do that just leave and keep it for other people.
I wrote this for learners and I can sure this works 100% .!
and tested on Visual Studio 2010.!

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You post knowledge for others to read, but don't care if it is factual (that others will help explain things, as it's a forum)? Nice.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
UbuntuX

UbuntuX

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
well ,as I said ,it's correct 100%.
he/she tried to insert his/her ideas.not Correct most of parts.

#9
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
Isn't this more of a tutorial-shouldn't it be submitted as one?

#10
UbuntuX

UbuntuX

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
This is Swap method .!!

[ATTACH]3251[/ATTACH]

:w00t:
now you see ,I am not wrong.
you just need to keep practicing and find new methods more.

-Thanks :sleep:

Attached Files



#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Incorrect information that compilers are tolerant of is still incorrect information. Also, just because you tested it with VS2010 doesn't mean that other compilers won't consider it an error and refuse to compile it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
UbuntuX

UbuntuX

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
there are no incorrect information ,it's lack of knowledge of readers ,due to that reason they think ,writer is incorrect.
keep practicing.so you are fine lolz.
u may tested on Dev but if does work well on VS then you can't tell ,it's incorrect.
VS is more updated and already supported to new C++0x features unlike old Dev with MinGW compiler.
people use non-Microsoft products tend to humiliate Microsoft users.that's nature ,who care. lolz
infact the criticizer of my posts misused most of parts and copy pasted portions of big stetments and review it and try to insert bla bla bla .

He just copied and pasted JUST this portion

Quote

,remember the pName is a pointer char ,it means you don't need to declare it as an array also pointer can directly take a string and IT WILL NOT TAKE AN ADDRESS.!!"
Noooo. When you do something like:
Code:
char *blah = "Hello World!";
What you're actually doing is setting blah to point to a region of memory that contains the string "Hello World!"

BUt it should be.!!


Quote

,remember the pName is a pointer char ,it means you don't need to declare it as an array also "in this case the pointer can directly take a string and IT WILL NOT TAKE AN ADDRESS.!!"
Noooo. When you do something like:
Code:
char *blah = "Hello World!";
What you're actually doing is setting blah to point to a region of memory that contains the string "Hello World!"

newbie people don't know
what's region of memory. so that's why simply told in simple manner ,
infact you are incorrect ,it also considers the null terminator (\0) ,so it goes like this way

char *blah = "Hello World!";
What you're actually doing is setting blah to point to a region of memory that contains the string "Hello World!\0"
NOT just "Hello World!" SO U ARE INCORRECT.! lolz not me

try to correct your mistakes before correcting other people's already corrected posts.
Goodluck :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users