Thread: If a function is declared as int, how come I get this?

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    4

    If a function is declared as int, how come I get this?

    Hi,

    This might come across as a stupid question, but please do bear with me.

    In the following statement:
    Code:
    int funcname(void)
    I thought that the function is of type int, and has no arguments or it is void of any arguments. Since it's an "int" type of function, I thought it would not return anything apart from an integer.

    But that does not seem to be the case below:

    Code:
    #include <stdio.h>
    
    int how (void);
    
    int main (void) 
    {
        printf("This is the main function.\n");
        how();
        return printf("this is the \"return\" in main function.\n");
    }
    
    int how (void) 
    {
        printf("This is a function called \"how\".\n");
        return printf("This \"return\" is from the \"how\" function.\n");
    }
    When I compile this, I get the following:

    Code:
    me@linux-5mcf:~> gcc how.c -o how
    me@linux-5mcf:~> ./how
    This is the main function.
    This is a function called "how".
    This "return" is from the "how" function.
    this is the "return" in main function.
    I read somewhere that main is always declared as an integer function. So, I thought of writing another function, and declaring it as int, and then including the printf statement in the return. But that too seems to work.

    Is it possible, that both the main and the "how" functions are returning the success code of the return printf....statement? Or is there some other reason?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The returned value of printf is the number of characters printed, so when used in a situation like this where a value is needed, that's the value you get.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    4
    Quote Originally Posted by tabstop View Post
    The returned value of printf is the number of characters printed, so when used in a situation like this where a value is needed, that's the value you get.
    Hi tabstop,
    Thank you for taking time to answer my stupid question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Function not declared', while it clearly is...
    By MKGirlism in forum C Programming
    Replies: 9
    Last Post: 01-17-2017, 06:41 AM
  2. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  3. using variables declared in another function
    By Mutantturkey in forum C Programming
    Replies: 11
    Last Post: 03-14-2010, 01:26 PM
  4. Replies: 7
    Last Post: 04-11-2009, 09:44 AM
  5. Replies: 3
    Last Post: 10-12-2006, 06:58 AM

Tags for this Thread