if statement, i need help

This is a discussion on if statement, i need help within the C Programming forums, part of the General Programming Boards category; hi all pliz someone help me with this phrase, i really dont understand anything even with the book....

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    Question if statement, i need help

    hi all pliz someone help me with this phrase, i really dont understand anything even with the book.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    If condition
    if (x)
    do_something;
    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.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Code:
    if(something) {
       //do something
       //e.g. x = x +1;
    
    }
    Suppose you want to check if a number is equal to 10 during the running of a program, and if that number is 10, to exit:

    Code:
    if(n == 10) {
      return (0);
    }
    It's a simple logical statement, something you do everyday, if something then I'll do this, if not, then I'll do that.

    Etc.

    Edit, here is a tutorial on the matter:

    http://www.cprogramming.com/tutorial/c/lesson2.html
    Last edited by JFonseka; 04-15-2008 at 04:54 AM.
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Don't use exit in if statements, if possible.
    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
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by Elysia View Post
    Don't use exit in if statements, if possible.

    And why not?
    =========================================
    Everytime you segfault, you murder some part of the world

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Because you will prematurely exit a program.
    It's better to return an error from functions to let them handle the error as appropriate, and if they can't handle it, they will return an error, etc, until main which will exit gracefully.

    Exit will also cause any memory or cleanup that needs to be made unmade.
    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.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I see, but once a program exits though, it's not running in the task manager anymore, so it can't be using any memory.
    =========================================
    Everytime you segfault, you murder some part of the world

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    But the application needs to clean up before exiting. If you do exit, it has no chance to do so.
    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.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Clean up what exactly? I was under the impression that once a program exits abnormally even if stray values are left in memory since they aren't being used by the program they are free to be overwritten.

    When you run a program, like a game or something you can see the jump on taskmanager like several hundred megabyte's depending on what type of game it is, and then if it causes an illegal operation and shutsdown all that memory is freed and the RAM meter goes right down again.
    =========================================
    Everytime you segfault, you murder some part of the world

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JFonseka View Post
    I see, but once a program exits though, it's not running in the task manager anymore, so it can't be using any memory.
    Yes, this has been debated before:
    1. Memory allocations ARE cleaned up by all major commercial OS's when the application exits. Small OS's, such as RTOS's and such may not even support processes as such, and may certainly not do any cleanup - it's up to the developer to make sure no memory leaks happen.

    The same applies to file-handles, semaphores, shared memory and similar: Windows, Linux etc will close files and clean up any open system resources.

    2. There are other resources that may NOT be cleaned up: temporary files created by the application, global resources that specifically are noted to NOT be cleaned up by the OS [can't think of one right now, but at least some OS's have those].

    Arbitrary calls to exit is a bad strategy for error handling, mostly because it makes the assumption that nothing else will be important to do if this function is failing - which is probably OK in a small program, but in bigger programs, the possibility of something going horribly wrong because of a random exit is increasing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Ah...my bad. Well it's a good thing Elysia brought this up because I was doing an assignment and I did exactly that and the specification was to handle errors appropriately
    =========================================
    Everytime you segfault, you murder some part of the world

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by JFonseka View Post
    Clean up what exactly? I was under the impression that once a program exits abnormally even if stray values are left in memory since they aren't being used by the program they are free to be overwritten.

    When you run a program, like a game or something you can see the jump on taskmanager like several hundred megabyte's depending on what type of game it is, and then if it causes an illegal operation and shutsdown all that memory is freed and the RAM meter goes right down again.
    Files may not be closed. Proper information may not be saved. Proper data into registry may not be saved (remember: many programs saves settings only at exit!). State of databases may not be closed correctly (database has no chance to do termination work), etc, etc, etc.

    In C++, it's even worse because destructors won't necessarily be run. That means all that cleanup code won't be run.
    In short, it's a very bad idea.
    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.

  13. #13
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by Elysia View Post
    Files may not be closed. Proper information may not be saved. Proper data into registry may not be saved (remember: many programs saves settings only at exit!). State of databases may not be closed correctly (database has no chance to do termination work), etc, etc, etc.

    In C++, it's even worse because destructors won't necessarily be run. That means all that cleanup code won't be run.
    In short, it's a very bad idea.
    So what's the best way to 'exit' then without actually using the function exit?

    Plain old return?
    =========================================
    Everytime you segfault, you murder some part of the world

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Yep, return and error checking.
    (Or, alternatively, in C++, throwing.)
    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.

  15. #15
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Thanks
    =========================================
    Everytime you segfault, you murder some part of the world

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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