Please debug this program

This is a discussion on Please debug this program within the C Programming forums, part of the General Programming Boards category; Code: main() { printf("%d",i); sum(); printf("%d",i); } static int i; sum(void); { ++i; } Please tell me if 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
    20,962
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,962
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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 04: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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06:49 PM
  2. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 12:54 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21