Thread: Return value...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    14

    Return value...

    Im sorry for bothering anyone who is gonna answer this,its a stupid question.But when i was doing a little program in C in the Code;;Blocks compiler i came across the while loop,and i finished the program,but when i build and run it,it just gives me a slide show of my 'printf',without stoping.So i remember when ive programmed in C++ IDE too return 0.So i did that and it actualy worked.How is that possible.Is it becuse the compiler runs the C and C++ ?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    I am not sure exactly what you are saying but my understanding is that everything in C is pretty much a function (I like to call them subroutines) and every function must return a value, even if its ZERO. Main is a function just like any other function. Thats my understanding, I am sure I will be corrected if I am wrong.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Filster View Post
    Im sorry for bothering anyone who is gonna answer this,its a stupid question.But when i was doing a little program in C in the Code;;Blocks compiler i came across the while loop,and i finished the program,but when i build and run it,it just gives me a slide show of my 'printf',without stoping.So i remember when ive programmed in C++ IDE too return 0.So i did that and it actualy worked.How is that possible.Is it becuse the compiler runs the C and C++ ?
    It doesn't have to do with the compiler. It has to do with your code. We can't help unless you post the code you used.

    Quote Originally Posted by Linux Trojan View Post
    I am not sure exactly what you are saying but my understanding is that everything in C is pretty much a function (I like to call them subroutines) and every function must return a value, even if its ZERO. Main is a function just like any other function. Thats my understanding, I am sure I will be corrected if I am wrong.
    main is indeed a function and per the standard returns an int to the OS. Note, to be pedantic, every function does not need to return a parameter, hence the void function declaration.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    No,i ment,is there a diffrent function i can use too stop the slide show,besides returning a value...?

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Please post the portion of the code so I understand what you want. You can escape a loop by using the break; command or you could set your condition causing the loop to evaluate to false.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Filster View Post
    Im sorry for bothering anyone who is gonna answer this,its a stupid question.But when i was doing a little program in C in the Code;;Blocks compiler i came across the while loop,and i finished the program,but when i build and run it,it just gives me a slide show of my 'printf',without stoping.So i remember when ive programmed in C++ IDE too return 0.So i did that and it actualy worked.How is that possible.Is it becuse the compiler runs the C and C++ ?
    Code::Blocks is NOT a compiler... it is an IDE (Integrated Developer's Environment) that uses any compiler you hook up to it...

    So, what compiler are we talking about?

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I am pretty sure the OP made an infinite loop that he then put a return(0) statement in to break out of the loop. There is some serious lack of knowledge on this one.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Linux Trojan View Post
    I am not sure exactly what you are saying but my understanding is that everything in C is pretty much a function (I like to call them subroutines) and every function must return a value, even if its ZERO. Main is a function just like any other function. Thats my understanding, I am sure I will be corrected if I am wrong.
    Wrong... not all functions have to return a value...
    Code:
    void add(int a, int b)
      { printf("%d", a +  b); }
    ...No return value.

    It does however turn out that main() has to return an int... as in int main (void) ... for a C program to work correctly.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Filster View Post
    No,i ment,is there a diffrent function i can use too stop the slide show,besides returning a value...?
    Go here... Read ... do the examples and exercises ... LEARN C...

    Teach Yourself C in 21 Days -- Table of Contents

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    Tater, thats the book I am using. I am disappointed with it. It doesnt even mention STRTOK. Can you believe it?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Linux Trojan View Post
    Tater, thats the book I am using. I am disappointed with it. It doesnt even mention STRTOK. Can you believe it?
    I have an old similar type of book (Sam's Teach Yourself C in 24 Hours 2nd ed) that I was not disappointed with. Introductory books specifically avoid trying to cram too much information in at once. The idea is to get a handle on the basics, and maybe get your feet wet with some glimpses of more advanced functions. If they tried to teach everything at once, the book would be huge, and the reader would likely lose interest (or be overwhelmed).

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Linux Trojan View Post
    Tater, thats the book I am using. I am disappointed with it. It doesnt even mention STRTOK. Can you believe it?
    And exactly what is it with you and strtok()?

    Really... do they make medication for that?

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    35
    Quote Originally Posted by CommonTater View Post
    And exactly what is it with you and strtok()?

    Really... do they make medication for that?
    Several years ago I wrote my own faxing routine. I used GTK libraries and wrote everything in C. I couldnt get the program done without strtok and yet my book didint talk about it so I was clueless. I went up to a linux users group meeting and met a consultant there who is an expert on C. He looked at my code and filled in the part that needed strtok. Its been burned into my memory ever since.

    I think I need to find a more mature book on C "C for scientists and engineers" or something.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Linux Trojan View Post
    Several years ago I wrote my own faxing routine. I used GTK libraries and wrote everything in C. I couldnt get the program done without strtok and yet my book didint talk about it so I was clueless. I went up to a linux users group meeting and met a consultant there who is an expert on C. He looked at my code and filled in the part that needed strtok. Its been burned into my memory ever since.

    I think I need to find a more mature book on C "C for scientists and engineers" or something.
    Advanced study can't hurt... but at the same time why are you stuck if somebody doesn't tell you something? Experiment, play with stuff, write code... You can learn on your own, you know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  2. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. Replies: 4
    Last Post: 07-15-2005, 04:10 PM
  5. Return Return Error
    By javacvb in forum C++ Programming
    Replies: 8
    Last Post: 12-16-2003, 04:17 PM