Thread: doubt in C--(reducing the length of code)

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    16

    doubt in C--(reducing the length of code)

    In gcc, the 'int' in main function is optional and 'return 0' is also optional...that leaves a warning but gets complied and gives output.But in one of my prg it doesnt give out put but gives error i wanna know why..So what is the condition on which we are supposed to return the value 0 to main()...

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Always.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    read FAQ Why int main()

    ssharish2005

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So let me get this straight...

    You're worried that your code is too long, so you think that removing "return 0;" is going to save the day?

    That must be some good weed you've got.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Sometimes, return 0; is the only bug-free line of code in the whole program

    Why would you want to eliminate a free line which prevents your code from being a TOTAL failure
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by quzah
    So let me get this straight...

    You're worried that your code is too long, so you think that removing "return 0;" is going to save the day?

    That must be some good weed you've got.


    Quzah.
    lol, hey its 1 line less

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    As someone else pointed out in a tag line.

    - Every program has at least one bug.
    - For any given program, it is possible to rewrite it in one less line.
    - Therefore, it is possible to reduce every program to a single line, which doesn't work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Start by deleting whitespace. This is what really makes the code unnecessarily long.
    Keep all names short - preferably one character long. (Bonus: it will become almost impossible to make typographical errors too!)
    Put as much as you can on one line. Using up a whole line just to do c++; is such a waste.

    There may be other suggestions, but if you start out like that you'll quickly discover that you are writing nice short and efficient code.

  9. #9
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Macros really help too. Not only for readability but sometimes they run faster because there's less code in your functions. You can also do common things like #define i if to shorten the built-in operators of the language.

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I seriously don't see why you'd want to reduce the length of code...

    You may want to do it more efficiently. But other than that, I see no reason to make it shorter, other then if you're concerned about readability, which shouldn't be a problem if you comment/document/make appropriate headers, it shouldn't matter how long it is. And taking out white space and lengthy variable names also would just reduce readability, therefore conflicting with what you're trying to do in the first place.

    I like the macros idea, though. Macros are your friend.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by anon
    Start by deleting whitespace. This is what really makes the code unnecessarily long.
    Keep all names short - preferably one character long. (Bonus: it will become almost impossible to make typographical errors too!)
    Put as much as you can on one line. Using up a whole line just to do c++; is such a waste.

    There may be other suggestions, but if you start out like that you'll quickly discover that you are writing nice short and efficient code.
    Please tell me you are not serious. This is really bad advice.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Less lines != faster/more efficient

    That is a old moronic way of thinking.

    Better algorithms, profiling, tweaks here at bottle necks. That will make your code better.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    anon is probably just trolling. Ironically, Mir does this a lot too and can't seem to identify other troll posts.

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by Wraithan
    Less lines != faster/more efficient
    It does in HTML.

    But that is a special case.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jverkoey
    Macros really help too. Not only for readability but sometimes they run faster because there's less code in your functions. You can also do common things like #define i if to shorten the built-in operators of the language.
    Macros don't reduce the number of actual lines. They provide text subsitution. So when it's compiled, it's not going to really save you any space. It may make your source file smaller, but it's not generally going to be a space saver.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM