Thread: Please debug this program

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    20

    Please debug this program

    Code:
    main()
    {
    printf("%d",i);
    sum();
    printf("%d",i);
    }
    
    static int i;
    
    sum(void);
    {
    ++i;
    }
    Please tell me if this program runs or not
    If error occurs, what should be the error and the reason ?

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The program compiles and runs without any error.

    Are you sure you can trust me? Read the homework policy and use your compiler to find out if I told you the truth.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    9
    Quote Originally Posted by TheDeveloper View Post
    Please tell me if this program runs or not
    If error occurs, what should be the error and the reason ?

    thanks
    You mean errorS?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If this is all code there is, then it may compile, but should not necessarily do so.
    You suffer firstly from "default int". All functions should return something. Not nothing.
    Secondly, since the definition of sum appears below main, it should have a prototype (every function should have one, really).
    Thirdly, there is a stray ; after the function definition of sum.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    If this is all code there is, then it may compile
    It certainly won't since i was not declared before the first two points where it is used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    9
    Also "i" isn't declared and there is no value assigned to it. Plus even if you do all that the program probably won't do what you liked it to do...

    edit: damn I'm slow

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, i is declared. On global level, which means it will be initialized to 0, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    9
    Quote Originally Posted by TheDeveloper View Post
    Code:
    static int i;
    Oh yes, I wasn't looking for the declaration of a variable used in main, on the lines after it's used

    Ok then, since it's global the second part of my post doesn't apply too

    ...but it should be before main anyway
    Last edited by mMarko; 01-07-2009 at 05:10 AM.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Including a certain header file would help too.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Please debug this program\b\b\b\b\b\b\b\b\b\b\bunblock my toilet.
    Er, no.
    Get a compiler, and use it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 01:54 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM