Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Short Code 1

Why code 10 lines if you can code 1? Test your c++ short-cut knowledge here

Postby Marss++ on Tue Aug 19, 2008 4:08 pm

That was already mentioned by antiRTFM


Oppsy... Didn't see that.


As for the DOS and all that stuff. I was talking more specifically written C++ code in the compiler. Although what you are saying is perfectly true.
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Shortest code

Postby Zul'Head on Thu Mar 19, 2009 10:02 am

It can't be any shorter than that...

Code: Select all
int main() {}
Zul'Head
 
Posts: 4
Joined: Thu Nov 27, 2008 5:32 pm

Re: Short Code 1

Postby KING-T on Wed Apr 29, 2009 7:21 am

Code: Select all
#include <iostream>
int main()
{
return 0;
}
KING-T
 
Posts: 3
Joined: Wed Apr 29, 2009 7:17 am

Re: Short Code 1

Postby InfiniteLoop on Sat May 02, 2009 1:46 pm

KING-T wrote:
Code: Select all
#include <iostream>
int main()
{
return 0;
}


Including iostream is used for functions like cout and cin, and since they are not used, you do not need to include it.

You do not need to return a value for your program to run, therefore, you do not need a return statement.

Therefore, your program is reduced to

Code: Select all
int main(){}


Which, as others stated earlier, is the shortest possible program you can have.
InfiniteLoop
 
Posts: 11
Joined: Sun Apr 26, 2009 1:03 pm

Re: Short Code 1

Postby R3D_-_N3CK on Sat May 02, 2009 5:51 pm

Code: Select all
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    cout<<"Hi Bye";
    cin.get();
}
R3D_-_N3CK
 
Posts: 5
Joined: Sat May 02, 2009 3:13 pm

Re:

Postby GrimSoul on Mon Jul 27, 2009 4:51 pm

antiRTFM wrote:Yes.

Quote from the standard c++ draft (3.6.1 5):

...If control reaches the end of main without
encountering a return statement, the effect is that of executing
return 0;

Not with my compiler =) if the function expects something to be returned ( not void ) you must return something. Likewise if something is returned to a function that expects nothing to be returned ( void ) i get a compiler error.

Im using dev-C++ by bloodshed , so your shortest code is non standard
Do not underestimate my post count.

Image
GrimSoul
 
Posts: 8
Joined: Thu Jul 23, 2009 7:27 pm

Re: Re:

Postby C++ on Sat Aug 08, 2009 6:17 am

GrimSoul wrote:
antiRTFM wrote:Yes.

Quote from the standard c++ draft (3.6.1 5):

...If control reaches the end of main without
encountering a return statement, the effect is that of executing
return 0;

Not with my compiler =) if the function expects something to be returned ( not void ) you must return something. Likewise if something is returned to a function that expects nothing to be returned ( void ) i get a compiler error.

Im using dev-C++ by bloodshed , so your shortest code is non standard


You misinterpret the standard. If there is no explicit return statement, then main implicitly returns 0. Therefore the main definition below adheres to the standard. If a compiler gives a warning or error when you don't return something from main, keeping in mind the main definition below, then it most likely does not follow the standard or you need to explicitly compile with an option that tells it to follow the C++ standard.

Code: Select all
int main() {}
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Short Code 1

Postby no1important on Sun Aug 09, 2009 12:16 pm

Hasn't any one got this? It compiles and runs in Dev C++.

CODE:

main(){}
no1important
 
Posts: 1
Joined: Sun Aug 09, 2009 12:05 pm

Re: Short Code 1

Postby C++ on Sun Aug 09, 2009 3:19 pm

no1important wrote:Hasn't any one got this? It compiles and runs in Dev C++.

CODE:

main(){}


Only compilers that are told to compile non standard code allow this.
The standard does not allow it.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Short Code 1

Postby antiRTFM on Sun Aug 16, 2009 7:59 pm

GrimSoul:

1) you are correct about any other function you create, but the main() function is different
2) Dev-C++ is far from being an adequate reference/proof of the c++ standard
User avatar
antiRTFM
Administrator
 
Posts: 456
Joined: Sun Apr 13, 2008 9:10 am

Re: Short Code 1

Postby cyberspy on Tue Sep 15, 2009 1:02 pm

LOL
Code: Select all
void main(){}
cyberspy
 
Posts: 12
Joined: Sun Sep 13, 2009 4:14 am

Previous

Return to Short code

Who is online

Users browsing this forum: No registered users and 0 guests