CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    425

    output confusion

    The output of the following code is 123 Genesis.I could not get it how?
    Code:
    int fun(int num)
        { 
            return printf(" %d ", num); 
        }
    
        void main()
        {
            printf(" Genesis ", fun(123), " & Genesis");
        }

    Regards,
    Ankush Mehta

    "The Child is the father of the Man."
    William Wordsworth

  2. #2
    Join Date
    Feb 2003
    Location
    India
    Posts
    232

    Re: output confusion

    printf(" Genesis ", fun(123), " & Genesis");
    Format of printf:

    int printf( const char *format [, argument]... );

    format is the format control. If this format control includes %d, %s %c etc., th printf uses the remaining arguments to replace values accordingly. If no such thing is found, then the arguments are ignored.

    your printf passes fun(123) are argument. Since it is an argument it is resolved before printf and hence 123 gets printed first. then the current printf is executed, which prints the Genesis. Other arguments are ignored.
    Sarve Bhavantu Sukheenah,
    Sarve Santu Niramayah,
    Sarve Bhadrani Pashyantu,
    Ma Kashchit dukh bhag bhavet.

  3. #3
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    Re: output confusion

    Unless I've misunderstood your question I see nothing majorly wrong with your code and does indeed print 123 Genesis.

    However, a point to note is that main should be declared as having a return type of int :

    Code:
    int main()
    {
         ...
         return 0;
    }
    Regards
    I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)

  4. #4
    Join Date
    Jun 2005
    Location
    Chennai , India
    Posts
    1,375

    Thumbs up Re: output confusion

    Quote Originally Posted by erankushmehta
    The output of the following code is 123 Genesis.I could not get it how?
    Code:
    int fun(int num)
        { 
            return printf(" %d ", num); 
        }
    
        void main()
        {
            printf(" Genesis ", fun(123), " & Genesis");
        }
    Well dude .. its working perfectly dude... whats the error u r getting ?
    i dont think there must be.. but what is the output ?
    It takes seconds for rating…that actually compensates the minutes taken for giving answers
    The biggest guru-mantra is: Never share your secrets with anybody. It will destroy you.
    Regards, Be generous->Rate people
    Jayender!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured