Why does the following code works?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 2
Reputation: aagajaba is an unknown quantity at this point 
Solved Threads: 0
aagajaba aagajaba is offline Offline
Newbie Poster

Why does the following code works?

 
0
  #1
22 Days Ago
The following code works, it prints hello world.
Also, depending on the second value, it either gives segmentation fault or doesn't gives segmentation fault.
Can someone please explain me what is happening internally.
  1. int main = ( cout << "Hello world!\n", 195 );
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 323
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 33
Clinton Portis's Avatar
Clinton Portis Clinton Portis is online now Online
Posting Whiz
 
0
  #2
22 Days Ago
I am really not sure what is going on here. You are attempting to use a main() function like I've never seen it before. Therefore, this method you are using is more than likely unconventional.

To me, it seems like you are declaring an 'int' type variable called main (which I think is a c++ reserved keyword) and then attempting to initialize the poor int var with a bunch of non-int related gobbledygook.

I am not sure if you are trying to inline the main() function, or attempting to pass in a couple of arguments to the main() function (in which case you could consider using the argc argv[] command-line arguments) or if you are just trying to use a couple of default arguments (which I'm not sure if it is allowed for main()).

Anyway, the rest of the world is coding main() like this:
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. cout << "Hello world!\n";
  7. cin.get();
  8.  
  9. return 0;
  10. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: aagajaba is an unknown quantity at this point 
Solved Threads: 0
aagajaba aagajaba is offline Offline
Newbie Poster
 
0
  #3
22 Days Ago
Even I code main as the way everyone else does.
I just wanted to know why and how this code works.
I found it as the motto of a top coder contestant.
Even i am totallly confused as to why this code works.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 323
Reputation: Clinton Portis is an unknown quantity at this point 
Solved Threads: 33
Clinton Portis's Avatar
Clinton Portis Clinton Portis is online now Online
Posting Whiz
 
0
  #4
22 Days Ago
I am lost as well.. without the assignment operator, it would make more sense because it would appear to use a constructor to initialize the main() object, just like one could initialize variable like this:
  1. int x(10), y(20);
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC