944,113 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 380
  • C++ RSS
Nov 7th, 2009
0

Why does the following code works?

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  1. int main = ( cout << "Hello world!\n", 195 );
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aagajaba is offline Offline
2 posts
since Nov 2009
Nov 7th, 2009
0
Re: Why does the following code works?
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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 7th, 2009
0
Re: Why does the following code works?
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aagajaba is offline Offline
2 posts
since Nov 2009
Nov 7th, 2009
0
Re: Why does the following code works?
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:
C++ Syntax (Toggle Plain Text)
  1. int x(10), y(20);
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC