Thread: Return 0; or Return 1;

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by brewbuck View Post
    If it seemed rude that was not my intention.
    I didn't find your post rude, just vague (though you clarified in the follow-up).

  2. #17
    Registered User Triggercut's Avatar
    Join Date
    Apr 2016
    Posts
    4
    https://www.youtube.com/watch?v=SWZf...85DE8440AA6B83

    This answered my question for the most part. What do you guys think?

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    It's okay I guess. He puts things some ways that I personally wouldn't.

    1) The point about namespaces is weird. He explains "using namespace std;" by saying something like it includes the standard library, which is pretty wrong. #include did that, not the using namespace line.

    Namespaces are a lot like boxes of stuff for code. It keeps objects that I coded distinct from objects that you coded which would be named the same thing. The standard library lives in a box called "std". Meaning, you can pretty much make your own thing called cout or endl and it would work totally fine. If it sounds useful, that's because it can be, and people use this in the real world.

    In fact, if you removed the using namespace line and then compiled the code, you would get an interesting error on line 5:
    Code:
    hello.cpp: In function 'int main()':
    hello.cpp:5:5: error: 'cout' was not declared in this scope
         cout << "Hello, world!" << endl;
    Like I said, the standard library lives in a box called "std", so your compiler does not know what cout is. One different way to write it that works is like this:
    Code:
    std::cout << "Hello, world!" << std::endl;
    A lot of people found this cumbersome, so you can just tell the compiler to look in the std box for things it doesn't know about, by writing "using namespace std;". Simple programs can get away with this because they are usually only going to be using standard objects.

    2) I really wouldn't say that main "works on" an int where he explains return values. That will sound confusing when you learn about command line arguments. But this topic has been such a bug-a-boo for you, that I feel awful about criticizing it. However bad it is, the video actually got you to understand I guess.
    Last edited by whiteflags; 04-14-2016 at 08:56 PM.

  4. #19
    Registered User
    Join Date
    Apr 2016
    Posts
    1
    In some codes there is no effect of writing "return 0" or not writing it.
    Can you please explain it ?


    Code:
     #include <iostream>using namespace std;
    int main()
    {
    	int m,n;
    	cout<<"Enter 2 positive integers: ";
    	cin>> m >> n;
    	if (m%n) cout<< m << " is not divisible by "<< n<<endl;
    	else cout<< m<< " is divisible by "<<n<<endl;
    }

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by algorism View Post

    Also, C++ (and C99 or higher) will automatically return 0 at the end of main if you leave out the return statement.
    Read the thread next time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return pointer to a vector of objects as a function return
    By Adaptron in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2016, 09:23 AM
  2. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  3. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Replies: 4
    Last Post: 07-15-2005, 04:10 PM

Tags for this Thread