new to "C" return question

This is a discussion on new to "C" return question within the C Programming forums, part of the General Programming Boards category; ok first thanks for helping. I'm new to programming in general and I decided I'd start with C. I found ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    13

    new to "C" return question

    ok first thanks for helping.

    I'm new to programming in general and I decided I'd start with C. I found a pretty sweet tutorial and I'm plugging along but I have a question.

    I see in the tutorial that at the end of an "if" block the tutorial has the command "return 0;"

    I have written a program below just to kind of get my feet wet and I omitted the "return" line intentionally. It compiles and runs just fine so my question is what does the return line do, do I really need it and how do I use it and it's output later on for better coding?

    Thanks again


    #include <stdio.h>

    int x, y, z;

    main()
    {

    /*get some data here*/
    printf("\nEnter a number: ");
    scanf("%d", &x);


    printf("\n Now enter another number: ");
    scanf("%d", &y);
    printf("\nYou entered %d, and %d\n", x, y);

    /*test values here*/
    if (x == y)
    printf("%d is equal to %d\n",x,y);
    else if (x > y)
    printf("%d is greater than %d\n",x,y);
    else
    printf("%d is less than %d\n",x,y);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you look a little bit down the page, you'd see this thread about return.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    You've also gotten yourself into some bad habits.
    You want to get rid of the implicit main.
    And also use code tags around code you post. Like:
    [code]int x, y, z; /* Etc */[/code]
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    I'll do that with the code from now on. I haven't been on a board of any kind in ages so my board etiquette probably could use some work as well.

    implicit main, if I read your link properly basically it said that you need to define what type of data your function carries? is that correct or am I not understanding things.

    what should I have done in the code above? I want to learn and develop "good" coding habits and I want to move onto C++ and Objective C after I have some skills in C so I really appreciate the feedback

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    It means you must select the return type for the function. And for main it's really easy, because it always returns int.
    Also, if you're looking to use C++, I would suggest that you really start with C++ right away instead of going C and then C++. The reason is that many things that are good in C are taboo in C++. C++ has an entirely different way of thinking too.

    In the code above, you should use int main, the global variables should really be local variables and the "z" variable could be deleted since it isn't used.
    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.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    yeah but I'm going to use that z variable later when I expand on the code and start using logical operators. to try and do cooler stuff

    I know this is a little off topic but I read your void main() link in your signature and I'm a little lost by all of it, I also read your scanf link also and I know that's way above my head,

    just as a good practice should you always have your main function be on type int? should you use int type for your functions most of the time?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    If you want the short story, just use int main() or int main(void) and always return an integer from main().

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by demuro1 View Post
    just as a good practice should you always have your main function be on type int? should you use int type for your functions most of the time?
    Main should always return int. So says the standard, so that's all there is to it.
    For your own functions, what you return is up to you (each function is usually different). If you don't need to return anything, you can use void.
    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
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Elysia
    Main should always return int. So says the standard, so that's all there is to it.
    No it doesn't. It says the implementation is free to do others.

    Quote Originally Posted by &#167; 5.1.2.2.1.1 of the C99 standard / homepages.tesco.net/J.deBoynePollard/FGA/legality-of-void-main.html
    It shall be defined

    • with a return type of int and
      • with no parameters [...] or
      • with two parameters [...] or equivalent;
      or
    • in some other implementation-defined manner.
    Last edited by zacs7; 09-09-2008 at 03:28 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Oh? I feel a possible clash of the standards.
    Microsoft clearly referred to "void main" as a Microsoft extension.

    EDIT:
    Ah yes, even though it might "allow" non-int main, it clearly specified that what is returned to the OS then is undefined:
    Quote Originally Posted by &#167; 5.1.2.2.3.1, C Standard
    If the return type is not compatible with int, the termination status returned to the host environment is unspecified.
    To make matters worse, void main is disallowed in C++:
    http://homepages.tesco.net/J.deBoyne...void-main.html
    But unfortunately legal in C. Something they really should change.
    Last edited by Elysia; 09-09-2008 at 03:30 AM.
    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.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It very well maybe in C++ (since C++ does not provide this "loophole").

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    What does "returning int" mean? In my program the name of main was "int main(void)" (just because I had been told to put that). But I didn't use any integers in my main, let alone "return" them (I don't know what returning means anyway =/). I just used floats. Nothing seems to be wrong with the program when I run it, but is there something I should know about "returning int"?

    Thanks for the help,

    Jake

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you return something, it is more like "response by return" in mail correspondence than returning books to a library or some such.

    In the case of int main() you tell the compiler "my main function will return an integer value". Convention is that you return 0 (or EXIT_SUCCESS - which is zero) as the return value from a "successful" application.

    --
    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.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    When you type "return 0;", you are returning an int. 0 is a number that has type int, and the return returns it from the function.

    Returning a value from a function means that the caller will be able to use the function as part of an expression.
    Code:
    int get_42(void)
    {
        return 42;
    }
    
    int get_10(void)
    {
        return 10;
    }
    
    int main()
    {
        int sum_of_two_nums;
    
        sum_of_two_nums = get_42() + get_10();
    
        printf ( "The sum of 42 and 10 is: &#37;d\n" , sum_of_two_nums ) ;
    
        return 0 ; /* Returning 0 from main() means the program has ended successfully */
    }

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    ok so I have another question now on returning numbers.

    I have gotten this little program to run as I'd like it to but now I'm trying to do more with the code. basically I'm trying to make everything run as a function so all I do is call functions.

    originally input() was part of main(). not only that but the printf() lines at the end of mult() and div() were incorporated in main() as well. Everything was fine until I tried to take the content that is in input() out of main(). the first section of code works perfectly. the second section is what I'm trying to make work in the same way. Can someone explain to me where my mistake is?

    Thanks a million




    Code:
    /*
    	Learning the ropes
    	This Works!!!!
     */
    
    #include <stdio.h>
    
    int x,y;
    float z,r;
    
    int mult(int x,int y);
    float div(int x,int y);
    
    
    int main()
    {
    	printf("\nPlease enter a number: ");
    	scanf("%d",&x);
    	printf("\nPlease enter a second number: ");
    	scanf("%d",&y);
    	
    	
    	mult(x,y);
    	
    	printf("\nand");
    	
    	div(x,y);
    	
    	printf("\nHave a nice Day!!!\n");
    		
    	return 0;
    }
    
    
    int mult(int x,int y)
    {
    	z=x*y;
    	
    	printf("\n%d times %d equals %.2f",x,y,z);
    	
    	return 0;
    }
    
    
    float div(int x,int y)
    {
    	z=x/y;
    	r=x%y;
    	r=r/y;
    	z=z+r;
    	
    	printf("\n%d divided by %d equals %.2f",x,y,z);
    	
    	return 0;
    }
    Code:
    /*
    	Learning the ropes
    	got it to work now trying to add functions instead of just
     */
    
    #include <stdio.h>
    
    int x,y;
    float z,r;
    
    int mult(int x,int y);
    float div(int x,int y);
    float input(int x, int y);
    
    
    int main()
    {
    	input(x,y);
    	
    	mult(x,y);
    	
    	printf("\nand");
    	
    	div(x,y);
    	
    	
    	printf("\nHave a nice Day!!!\n");
    	
    	
    	
    	
    	
    	return 0;
    }
    
    
    float input(int x, int y)
    {
    	printf("\nPlease enter a number: ");
    	scanf("%d",&x);
    	printf("\nPlease enter a second number: ");
    	scanf("%d",&y);
    	
    	return x,y;
    }
    	
    
    
    int mult(int x,int y)
    {
    	z=x*y;
    	
    	printf("\n%d times %d equals %.2f",x,y,z);
    	
    	return 0;
    }
    
    float div(int x,int y)
    {
    	z=x/y;
    	r=x%y;
    	r=r/y;
    	z=z+r;
    	
    	printf("\n%d divided by %d equals %.2f",x,y,z);
    	
    	return 0;
    }

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

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 01:22 PM
  4. Class Bank(i have a question)
    By kuwait in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2003, 08:40 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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