a simple c program not responding as expected

Discussion in 'C' started by awnish, Oct 6, 2015.

  1. awnish

    awnish New Member

    Joined:
    Oct 6, 2015
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    pune
    Code:
    #include<stdio.h>
    
    int main()
    {
    	void print();
    }
    
    void print()
    {
    	printf("\n in the function \n ");
    }
    IN THE ABOVE CODE
    1. if i write void in front of print function call in main it is being compiled successfully but their is no output
    2. When i omit void from it then their is a warning generated
    simple_function.c:8:6: warning: conflicting types for ‘print’ [enabled by default]
    void print()
    ^
    simple_function.c:5:2: note: previous implicit declaration of ‘print’ was here
    print();
    ^
     
    shabbir likes this.
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    When you don't prototype a function, C sometimes (and in your case does) give you an "automatic" prototype, which is "int print();" You then go on to "redefine" print as returning a void, hence the warning.

    The reason you get no output when you put a void in front of the print in main is that this changes the statement from a function call to a function prototype.

    Solution: Move the print function above main, or prototype it correctly.
     
  3. awnish

    awnish New Member

    Joined:
    Oct 6, 2015
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    pune
    Go it :) thanks a lot
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice