Funcion prototype before its called.

Home Forums C-programming.. Funcion prototype before its called.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2231 Reply
    Humble
    Keymaster

    There is always a thought that, the functon protoype has to be there before the function is called. Otherwise, function have to be defined before its getting called.. but, it is not *that* true.. That said, if the function is called without declaring it , the compiler assume it returns int .. If the function actually returns “int” , it can be later to the call statement..

    Lets watch this:

    [hchiramm@node]$ cat func.c 
    #include <stdio.h>
    
    int *func1 (int);
    
    int main () {
    	int j=100;
    	printf ("Main: ");
    	func1(j);
    	return 0;
    }
    
    int *func1( int k)
    {
    	int *p = &k;
    	printf ("%d is passed to me", k);
    	return p;
    
    }
    
    [hchiramm@node]$ 
    
    
    If you compile this program everything is fine. 
    
    cc -Wall -g    func.c   -o func
    [hchiramm@humbles-lap c-blog]$ ./func 
    Main: 100 is passed to me
    
    
    How-ever :
    
    
    
    [root@node ]cat func1.c
    
    #include <stdio.h>
    
    /* int *func1 (int); */
    
    int main () {
    	int j=100;
    	printf ("Main: ");
    	func1(j);
    	return 0;
    }
    
    int func1( int k)
    {
    	printf ("%d is passed to me", k);
    	return k;
    
    }
    
    [hchiramm@node]$ 
    cc -Wall -g    func1.c   -o func1
    func1.c: In function ‘main’:
    func1.c:8:2: warning: implicit declaration of function ‘func1’ [-Wimplicit-function-declaration]
      func1(j);
      ^
    [hchiramm@node]$$ ./func1
    Main: 100 is passed to me
    
    
    You have a "Warning" about the "implicit declaration", but you are not blocked..
    
    Now lets change the same program to return a different data type than int in the called function as in first program.
    
    
    #include <stdio.h>
    
    /*int *func1 (int); */
    
    int main () {
    	int j=100;
    	printf ("Main: ");
    	func1(j);
    	return 0;
    }
    
    int *func1( int k)
    {
    	int *p = &k;
    	printf ("%d is passed to me", k);
    	return p;
    
    
    
    
    
    
    cc -Wall -g    func1.c   -o func1
    func1.c: In function ‘main’:
    func1.c:8:2: warning: implicit declaration of function ‘func1’ [-Wimplicit-function-declaration]
      func1(j);
      ^
    func1.c: At top level:
    func1.c:13:6: error: conflicting types for ‘func1’
     int *func1( int k)
          ^
    func1.c:8:2: note: previous implicit declaration of ‘func1’ was here
      func1(j);
      ^
    func1.c: In function ‘func1’:
    func1.c:16:2: warning: return makes pointer from integer without a cast [enabled by default]
      return k;
      ^
    make: *** [func1] Error 1
    
    
    Thats it..
Viewing 1 post (of 1 total)
Reply To: Funcion prototype before its called.
Your information: